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

migrant

> 编程语言
开源

为 Rails 提供更轻松的模式管理,以补充您的域模型。

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

工具介绍

为 Rails 提供更轻松的模式管理,以补充您的域模型。

Migrant

Summary

Migrant gives you a clean DSL to describe your model schema (somewhat similar to DataMapper). It generates your migrations for you so you can spend more time describing your domain model cleanly and less time managing your database layer.

You'll also get a handy .mock method to instantiate a filled-in model for testing or debugging.

Getting Started

In your Gemfile:

  gem "migrant"

Usage

Start by creating some models with the structure you need:

  > rails generate migrant:model business
ruby
  class Business  :presence  # You can add your validations in here too to keep DRY
      description :string                           # Passing a symbol works like it does in add_column
      timestamps                                    # Gets you a created_at, and updated_at

      # Use an array to specifiy multiple validations
      secret_code 5521,    :validates => [:uniqueness, :numericality]
    end
  end

Now, to get your database up to date simply run:

  > rake db:upgrade

  Wrote db/migrate/20101028192913_create_businesses.rb...
  Wrote db/migrate/20101028192916_create_users.rb...

OR, if you'd prefer to look over the migrations yourself first, run:

  > rails generate migrations

Result:

r
  irb(main):001:0> Business
  => Business(id: integer, user_id: integer, name: string, website: string, address: text, date_established: datetime)

  irb(main):002:0> Awesome!!!!
  NoMethodError: undefined method `Awesome!!!!' for main:Object

By default, your database structure will be cloned to your test environment. If you don't want this to happen automatically, simply specify an environment variable directly:

  > rake db:upgrade RAILS_ENV=development

Serialization

Keeping track of your serialized attributes can be done in the Migrant DSL (v1.3+), here's some examples:

ruby
  class Business  OpenStruct.new("Manager" => "Joe")
 
      # Default serialization storage (hash)
      locations  :serialized
    end
  end

These will call ActiveRecord::Base.serialize for you so don't do it again yourself! The mock generated would appear as:

  irb(main):002:0> my_business = Business.mock
  => #, 
       locations: {}>

Want more examples?

Check out the test models in spec/support/models.rb

Model Generator

  > rails generate migrant:model business name:string website:text

The model generator works as per the default ActiveRecord one, i.e. you can specify fields to be included in the model. However, a migration is not generated immediately, but the structure block in the model is automatically filled out for you.

Simply run rake db:upgrade or rails generate migrations to get the required migrations when you're ready.

What will happen seamlessly

  • Creating tables or adding columns (as appropriate)
  • Adding indexes (happens on foreign keys automatically)
  • Validations (ActiveRecord 3)
  • Changing column types
  • Rollbacks for all the above

Currently unsupported

  • Migrations through plugins (Rails 5+)

Compatibility

  • Ruby 2.2 or greater
  • Rails 4.2 through to Rails 5.1

Note: Really old Ruby versions (1.8) and Rails (3.2+) are supported on v1.4

Getting a mock of your model

  > rails console

  irb(main):002:0> my_business = Business.mock
  => #

  irb(main):003:0> my_business.user
  => #

Help

Be sure to check out the Github Wiki, or give me a shout on Twitter: @101pascal

Maintability / Usability concerns

  • You don't have to define a structure on every model, Migrant ignores models with no definitions
  • You can remove the structure definitions later and nothing bad will happen (besides losing automigration for those fields)
  • If you have a model with relations but no columns, you can still have migrations generated by adding "no_structure" or define a blank structure block.
  • It's probably a good idea to review the generated migrations before committing to SCM, just to check there's nothing left out.

Roadmap / Planned features

  • Rake task to consolidate a given set of migrations (a lot of people like to do this once in a while to keep migration levels sane)
  • Fabricator/Factory integration/seperation - Need to assess how useful this is, then optimize or kill.

License

The gem is available as open source under the terms of the MIT License.

Development

Please be sure to install all the development dependencies via Bundler, then to run tests do:

  > bundle exec rake 

Simplecov reports will be generated for each run. If it's not at 100% line coverage, something's wrong!

Issues· 4 开放

查看全部 Issues在 GitHub 打开

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

> 标签

Ruby

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

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言