在 Rails 中将数据与架构迁移一起迁移,并保持其最新状态。
This gem offers a solution for safely writing code in migrations, ensuring no regressions or data corruption in production.
In some cases, Rails migrations affect not only the database schema but also the data itself. However, the code used to modify data during migrations may become outdated and fail. While there are techniques, like defining model classes within migrations or using raw SQL, these methods are not foolproof and can't always ensure data integrity on the migration run.
Beyond outdated code issues, data migrations in production can sometimes lead to data corruption. How can this be prevented? This gem addresses the problem by offering a safeguard at no additional cost.
This gem essentially encourages writing tests for data migrations by allowing developers to write the data-related code in separate methods. By testing the data migration logic independently, it helps prevent problems related to outdated migrations or data corruption.
If the gem purpose is still not clear please check out this blog post.
Add this line to your application's Gemfile:
gem 'migration_data'And then execute:
$ bundleOr install it yourself as:
$ gem install migration_dataIn your migration define a #data method:
…ruby if Rails.env.test? MigrationData.config.skip = true end
## 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:
```ruby
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
endThe helper to load migrations require_migration is defined in the migration_data/testing. So you should to require it to have access to this convenient require extension.
git checkout -b my-new-feature)git commit -am 'Add some feature')git push origin my-new-feature)暂无开放 Issues,或尚未同步最近议题。