Class: Sequel::Migration
Overview
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
varchar :session_id, :size => 32, :unique => true
:created_at
text :data
end
end
def down
execute 'DROP TABLE sessions'
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.open ('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.
Class Method Summary collapse
-
.apply(db, direction) ⇒ Object
Applies the migration to the supplied database in the specified direction.
-
.descendants ⇒ Object
Returns the list of Migration descendants.
-
.inherited(base) ⇒ Object
Adds the new migration class to the list of Migration descendants.
Instance Method Summary collapse
-
#down ⇒ Object
The default down action does nothing.
-
#initialize(db) ⇒ Migration
constructor
Creates a new instance of the migration and sets the @db attribute.
-
#method_missing(method_sym, *args, &block) ⇒ Object
Intercepts method calls intended for the database and sends them along.
-
#up ⇒ Object
The default up action does nothing.
Constructor Details
#initialize(db) ⇒ Migration
Creates a new instance of the migration and sets the @db attribute.
31 32 33 |
# File 'lib/sequel_core/migration.rb', line 31 def initialize(db) @db = db end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *args, &block) ⇒ Object
Intercepts method calls intended for the database and sends them along.
64 65 66 |
# File 'lib/sequel_core/migration.rb', line 64 def method_missing(method_sym, *args, &block) @db.send(method_sym, *args, &block) end |
Class Method Details
.apply(db, direction) ⇒ Object
Applies the migration to the supplied database in the specified direction.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/sequel_core/migration.rb', line 37 def self.apply(db, direction) obj = new(db) case direction when :up obj.up when :down obj.down else raise ArgumentError, "Invalid migration direction specified (#{direction.inspect})" end end |
.descendants ⇒ Object
Returns the list of Migration descendants.
50 51 52 |
# File 'lib/sequel_core/migration.rb', line 50 def self.descendants @descendants ||= [] end |
.inherited(base) ⇒ Object
Adds the new migration class to the list of Migration descendants.
55 56 57 |
# File 'lib/sequel_core/migration.rb', line 55 def self.inherited(base) descendants << base end |