The solution to keep your Rails ActiveRecord migrations up to date
Latest commit 9a6eb71 Dec 9, 2016 @ka8725 committed with New version - 0.3.0
- added support Rails 5


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.

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.

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