alphagov/EFG — pull request #96 by jabley (2012-11-26 19:51:54+00:00)
Title: Add a data migration mechanism

Using regular migrations to modify data is problematic, particularly
when supporting a live system. Schema migrations are run at deploy time,
so if they take a long time, then deployments take longer, potentially
allowing people to see the system in an inconsistent state or taking
longer than we would like our deployments to take.

This change introduces an separate means of defining data migrations,
along with making them safe so that data migrations can be scheduled to
to run regularly, but each individual migration will only run if it
hasn't already run in that environment.

The assumption of data migrations is that they'll generally only be run
on production databases and the choice of when to run them will be
controlled manually (although as mentioned, there is the possibility of
running them via cron during a maintenance window). There's no attempt
to check the version of the database schema present when running a data
migration; it's expected that that will be done manually.

A data migration follows a similar structure to a regular migration.

Create a file in db/data_migrations/ with a name _.rb

A data migration is instance_evaled, so you don't need to inherit any
classes in the file, just write ruby code and feel free to refer to any
model objects you need.

(Shamelessly nicked from whitehall)