Skip to content

Data Migrations

Saray Cabrera Padrón edited this page Apr 3, 2020 · 6 revisions

In OBS codebase we differentiate migrations that only affect the database structure from migrations that modify the already existing data.

The first type are the common Rails migrations we all know.

The second type are what we call data migrations and they are occasionally needed to modify data that is already in the database. Something that shouldn't be mixed with the evolution of the database structure, so they are located in a different directory and are executed in a different way. We use the gem data-migrate to handle this kind of migrations.

Table comparing both:

Migration Data Migration
Location: db/migrations/ db/data/
Generating a new migration file: rails g migration AddSomethingToSomewhere rails g data_migration ModifySomething
Run the migration: rake db:migrate rake data:migrate
Last version tracking on: db/structure.sql db/data_schema.rb

Testing Data Migrations

It is a good practice to carefully test data migrations before applying them on production databases. This is possible by adding rspec tests under spec/db/data/.

The test should ...

  • require the data migration file,
  • include the flag type: :migration,
  • test the up method.
Clone this wiki locally