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
end
def data
User.create!(name: 'Andrey', email: '[email protected]')
end
def rollback
User.find_by(name: 'Andrey', email: '[email protected]').destroy
end
end
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
.
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 { described_class.new.data }.to_not raise_exception
end
end
describe '#rollback' do
before do
described_class.new.data
end
it 'works' do
expect { described_class.new.rollback }.to_not raise_exception
end
end
end
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.
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.
- Fork it ( http://github.com/ka8725/migration_data/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request