1

Rails 4 supports postgres' array data type, and we want to start taking advantage of the array types in postgres , how can I migrate an existing ActiveRecord serialized array field to the new data type?

1 Answer 1

2

serialize stores things inside the database as YAML documents in a text column. YAML isn't the easiest format to work with inside a database so the most straightforward approach would be to:

  1. Add an array column to hold the arrays.
  2. Loop through each record in Rails, let ActiveRecord unserialize the YAML, copy the Ruby arrays from the serialize column to the real array column from 1.
  3. Remove the serialize declaration from your model.
  4. Drop the old column that serialize used.
  5. Rename the column from 1.
  6. Set the newly renamed column to NOT NULL if desired.

You could parse the YAML inside the database using regex and string functions but that would depend on what sorts of things you're storing in your YAML.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.