A repository of tips and tricks (in both English and French) curated by Mirego’s engineering team.
  • rails
  • ruby

Running data & db migrations

When running both data and db migrations using the following command:

bundle exec rake db:migrate:with_data

one has to be careful since pending db and data migrations are both fetched, then ordered by timestamps.

This means that if you have a pending data migration to create or modify records for a specific model, but you also have a pending db migration set to run after and affecting the model's schema, the migrations might crash since the model will be offsync with its schema.

Ex:

model:

class Robot < ApplicationRecord
  mount_uploader_with_limit :header_thumbnail
end

pending db_migration with timestamp 20190401

rename_column :robots, :header_image, :header_thumbnail

pending data_migration with timestamp 20190331

Robot.create!(name: 'foo')

Is this case, the data migration will fail. When creating the Robot instance, the model will attempt to load the uploader using the header_thumbnail attribute name, before it is changed from header_image by the db migration.