sauronnikko
sauronnikko

Reputation: 5435

Rails 4: Where to put data migration

I need to fix some data inconsistencies in our database, and I'm not so sure if it's considered a bad practice putting that code as a migration (in db/migrations). Is there a better, more elegant way? Thanks

Upvotes: 1

Views: 124

Answers (1)

blotto
blotto

Reputation: 3407

If it is a one-off execution ( for example backfilling a new field's default value, or changing a data-type), I suggest db/migrations for two reasons :

  1. you dont want that code to be executed a second time.
  2. you can script a rolled back if necessary ( e.g. self.down)

Alternatively, if the script is auditing your data systematically, and new data could potentially have these inconstencies. A rake task would be desirable so you can periodically execute the script.

Upvotes: 1

Related Questions