0

I am currently re-designing a legacy (Rails) app data schema into a new Rails app. What I have to do is :

  • 1 Migration : create new fields in several tables, add new ones
  • 1 Rake task : dispatch legacy fields values into newly created tables fields
  • 1 Migration : delete unused fields, delete some legacy tables

Actually it works, but is it possible to "mix" these 2 migrations + rake task into one single script ? Is there really no other solution than running 3 scripts, one by one ? (the dispatching rake task takes up to 10 minutes).

I would really prefer to "shoot and forget" only one command line when I'll be deploying to production, moving from the old app to the new one.

gbarillot
  • 309
  • 1
  • 2
  • 13

3 Answers3

0

Depending on the complexity of the rake task you could do it all within the migration file. Just be very careful about redefining the models you need to access to avoid any new validations that will fail since those columns don't yet exist. Google for solutions on that.

An alternative would be to call the everything from a central rake task that handles this. I'm not sure I like that as much though.

One thing to keep in mind... what happens if the rake task fails to migrate some of the data. You need to verify it's all worked before running the migration that deletes the old fields.

Philip Hallstrom
  • 19,673
  • 2
  • 42
  • 46
  • You're right about the data verifications, I do it all along the rake task using exceptions and a flag. Finally, if this flag is not raised and no exception raised, I perform fields and tables deletions. – gbarillot Sep 06 '13 at 07:09
0

Ok, finally I just use one single rake task using raw SQL statments when needed :

dropped_fields = "ALTER TABLE `locations` DROP `name`, DROP.....;"
ActiveRecord::Base.connection.execute(dropped_fields)
gbarillot
  • 309
  • 1
  • 2
  • 13
0

I would suggest that you make three separate migrations namely

  • Add Fields
  • Update Data
  • Drop fields

The 2nd task is a "Data Only" migration that just does the necessary updates. You need to ensure that this task exits in the correct manner dependant upon success/fail as you are going to rely on ActiveRecords Migrations flow control to do the right thing if it fails/works.

You now need a single rake db:migrate command to perform during deployment

Steve Weet
  • 28,126
  • 11
  • 70
  • 86