Easier schema management for Rails that complements your domain model.
Easier schema management for Rails that complements your domain model.
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.
In your Gemfile:
gem "migrant"Start by creating some models with the structure you need:
> rails generate migrant:model business 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
endNow, 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 migrationsResult:
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:ObjectBy 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=developmentKeeping track of your serialized attributes can be done in the Migrant DSL (v1.3+), here's some examples:
class Business OpenStruct.new("Manager" => "Joe")
# Default serialization storage (hash)
locations :serialized
end
endThese 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: {}>Check out the test models in spec/support/models.rb
> rails generate migrant:model business name:string website:textThe 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.
Note: Really old Ruby versions (1.8) and Rails (3.2+) are supported on v1.4
> rails console
irb(main):002:0> my_business = Business.mock
=> #
irb(main):003:0> my_business.user
=> #Be sure to check out the Github Wiki, or give me a shout on Twitter: @101pascal
The gem is available as open source under the terms of the MIT License.
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!
No open issues yet, or sync has not completed.