2.4.4 Extract content migration from migration classes
Related to the audit.
Ruby on Rails migrations are supposed to allow modifying the structure of the database, not its content. So, when content migration is required, there is no built-in mechanism to handle it.
To answer the content migration scenario, the Garden Party application uses raw Ruby code inside its migration to insert or update existing data. This code is written directly in the migration file, which is later required in a spec to test its behaviour.
This leads to a lot of responsibilities in a migration file, and a tight coupling between the
ActiveRecord::Migration
code and its test.
Well, I have mixed feelings about this one:
-
Migrations can also be used to add or modify data. This is useful in an existing database that can't be destroyed and recreated, such as a production database.
- It is not recommended to use models directly in migrations, as models will evolve and maybe disappear, which lead to broken migrations, so plain Ruby code should be used (with all the methods provided by the migrations framework/ActiveRecord). Moving the data manipulation out of them will make access to the migration context harder
Given these points, the discussion is open.