Class | Sequel::Migration |
In: |
lib/sequel/extensions/migration.rb
|
Parent: | Object |
The Migration class describes a database migration that can be reversed. The migration looks very similar to ActiveRecord (Rails) migrations, e.g.:
class CreateSessions < Sequel::Migration def up create_table :sessions do primary_key :id String :session_id, :size => 32, :unique => true DateTime :created_at text :data end end def down # You can use raw SQL if you need to self << 'DROP TABLE sessions' end end class AlterItems < Sequel::Migration def up alter_table :items do add_column :category, String, :default => 'ruby' end end def down alter_table :items do drop_column :category end end end
To apply a migration to a database, you can invoke the apply with the target database instance and the direction :up or :down, e.g.:
DB = Sequel.connect('sqlite://mydb') CreateSessions.apply(DB, :up)
See Sequel::Schema::Generator for the syntax to use for creating tables, and Sequel::Schema::AlterTableGenerator for the syntax to use when altering existing tables. Migrations act as a proxy for the database given in apply, so inside down and up, you can act as though self refers to the database. So you can use any of the Sequel::Database instance methods directly.
Applies the migration to the supplied database in the specified direction.
# File lib/sequel/extensions/migration.rb, line 59 59: def self.apply(db, direction) 60: obj = new(db) 61: case direction 62: when :up 63: obj.up 64: when :down 65: obj.down 66: else 67: raise ArgumentError, "Invalid migration direction specified (#{direction.inspect})" 68: end 69: end
Returns the list of Migration descendants.
# File lib/sequel/extensions/migration.rb, line 72 72: def self.descendants 73: @descendants ||= [] 74: end
Adds the new migration class to the list of Migration descendants.
# File lib/sequel/extensions/migration.rb, line 77 77: def self.inherited(base) 78: descendants << base 79: end