Sequel::Plugins::
InlineMigrations module
A plugin for Sequel::Model that allows migrations for the model to be defined directly in the class declaration. It uses the inline_schema
plugin internally, and will add it for you if necessary.
Example
Define a base (abstract) model class:
# lib/acme/model.rb module Acme Model = Class.new( Sequel::Model ) Model.def_Model( Acme ) class Model plugin :inline_schema plugin :inline_migrations end end
Defining a model class with two migrations:
# lib/acme/vendor.rb require 'acme/model' class Acme::Vendor < Acme::Model( :vendor ) # The schema should always be kept up-to-date. I.e., it should be # modified along with each migration to reflect the state of the table # after the migration is applied. set_schema do primary_key :id String :name String :contact timestamp :created_at, :null => false timestamp :updated_at add_index :name end # Similar to Sequel's TimeStampMigrator, inline migrations have a symbolic # name, which is how they're tracked in the migrations table, and how # they're ordered when they're applied. The second argument is a human-readable # description that can be used for automated change control descriptions or # other tooling. migration( '20110228_1115_add_timestamps', "Add timestamp fields" ) do change do alter_table do add_column :created_at, :timestamp, :null => false add_column :updated_at, :timestamp end update( :created_at => :now[] ) end end migration( '20110303_1751_index_name', "Add an index to the name field" ) do change do alter_table do add_index :name end end end end
Apply pending migrations.
# bin/migrate require 'acme/model' require 'acme/vendor' # ... puts "Creating new tables, applying any pending migrations..." Acme::Model.migrate
Notable Model Methods
See Sequel::Plugins::InlineSchema::ClassMethods
for documentation for the methods the plugin adds to your model class/es.
-
migration
– define a migration -
migrate
– create any missing tables for the receiving model and any subclasses, then run any unapplied migrations.
Inline migrations also have model hook methods:
-
before_migration
-
after_migration
There’s also a method that will return a configured Sequel::Plugins::InlineMigrations::Migrator
in case you want to inspect what will happen when you call migrate:
-
migrator