The solution to keep your Rails ActiveRecord migrations up to date
Switch branches/tags
Nothing to show
Clone or download
Latest commit bc3c06a Oct 11, 2018


Build Status

This gem provides functionality to write any code in migrations safely without regression.

Sometimes we have to write some Rails code in the migrations and it's hard to keep them in a working state because models which are used there change too often. There are some techniques which help to avoid these pitfalls. For example, define model classes in the migrations or write raw SQL. But they don't help in 100% of all cases. This gem promises to solve this problem in a simple way.

Currently the gem supports Rails 4 and Rails 5.

If you still don't understand what this gem is for please check out this blog post.


Add this line to your application's Gemfile:

gem 'migration_data'

And then execute:

$ bundle

Or install it yourself as:

$ gem install migration_data


In your migration define a data method:

class CreateUsers < ActiveRecord::Migration
  def change
    # Database schema changes as usual

  def data
    User.create!(name: 'Andrey', email: '')

  def rollback
    User.find_by(name: 'Andrey', email: '').destroy

That's it. Now when you run migrations with the rake db:migrate command the data method will be run on up. When you rollback migrations with the rake db:rollback command the rollback method will be run on down.

Note: in some circumstances, the reset_column_information method should be called on a model which table is changed in the migration. Especially when you are certain that there should be present some column for a model but it's absent for some reason. Read more about this in the official Rails docs.

Testing migrations

To keep your migrations working don't forget to write tests for them. It's preferably to put the tests for migrations into spec/db/migrations folder, but actually it's up to you. Possible RSpec test (spec/db/migrations/create_user.rb) for the migration looks like this:

require 'spec_helper'
require 'migration_data/testing'
require_migration 'create_users'

describe CreateUsers do
  describe '#data' do
    it 'works' do
      expect { }.to_not raise_exception

  describe '#rollback' do
    before do

    it 'works' do
      expect { }.to_not raise_exception

The helper to load migrations require_migration is defined in the migration_data/testing. So you should to require it to have access to this convinient require extension.

Clean old migration

Use rake db:migrate:squash to remove all your old migrations and generate one migration with the current database schema. You don't have to run migrations after this because the generated migration will have the latest database version.


  1. Fork it ( )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request