百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

migration_data

> 后端框架
开源

在 Rails 中将数据与架构迁移一起迁移,并保持其最新状态。

367 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

在 Rails 中将数据与架构迁移一起迁移,并保持其最新状态。

MigrationData

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.

Installation

Add this line to your application's Gemfile:

gem 'migration_data'

And then execute:

$ bundle

Or install it yourself as:

$ gem install migration_data

Usage

In 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
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 convenient require extension.

Contributing

  1. Fork it ( http://github.com/ka8725/migration_data/fork )
  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

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubydatadata-migrationmigrationsrails

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架