Class | Sequel::Migration |
In: |
lib/sequel/extensions/migration.rb
lib/sequel/deprecated_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.
# File lib/sequel/deprecated_migration.rb, line 9 9: def self.apply(db, direction) 10: Deprecation.deprecate('Sequel::Migration', "require 'sequel/extensions/migration' first") 11: require 'sequel/extensions/migration' 12: apply(db, direction) 13: end
Applies the migration to the supplied database in the specified direction.
# File lib/sequel/extensions/migration.rb, line 55 55: def self.apply(db, direction) 56: obj = new(db) 57: case direction 58: when :up 59: obj.up 60: when :down 61: obj.down 62: else 63: raise ArgumentError, "Invalid migration direction specified (#{direction.inspect})" 64: end 65: end
Returns the list of Migration descendants.
# File lib/sequel/extensions/migration.rb, line 68 68: def self.descendants 69: @descendants ||= [] 70: end
# File lib/sequel/deprecated_migration.rb, line 15 15: def self.descendants 16: Deprecation.deprecate('Sequel::Migration', "require 'sequel/extensions/migration' first") 17: require 'sequel/extensions/migration' 18: @descendants ||= [] 19: end
# File lib/sequel/deprecated_migration.rb, line 21 21: def self.inherited(base) 22: Deprecation.deprecate('Sequel::Migration', "require 'sequel/extensions/migration' first") 23: require 'sequel/extensions/migration' 24: descendants << base 25: end
Adds the new migration class to the list of Migration descendants.
# File lib/sequel/extensions/migration.rb, line 73 73: def self.inherited(base) 74: descendants << base 75: end
# File lib/sequel/deprecated_migration.rb, line 3 3: def initialize(db) 4: Deprecation.deprecate('Sequel::Migration', "require 'sequel/extensions/migration' first") 5: require 'sequel/extensions/migration' 6: @db = db 7: end
# File lib/sequel/deprecated_migration.rb, line 27 27: def down 28: Deprecation.deprecate('Sequel::Migration', "require 'sequel/extensions/migration' first") 29: require 'sequel/extensions/migration' 30: end
# File lib/sequel/deprecated_migration.rb, line 32 32: def method_missing(method_sym, *args, &block) 33: Deprecation.deprecate('Sequel::Migration', "require 'sequel/extensions/migration' first") 34: require 'sequel/extensions/migration' 35: @db.send(method_sym, *args, &block) 36: end
Intercepts method calls intended for the database and sends them along.
# File lib/sequel/extensions/migration.rb, line 82 82: def method_missing(method_sym, *args, &block) 83: @db.send(method_sym, *args, &block) 84: end