| Class | Sequel::Migration |
| In: |
lib/sequel_core/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.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. 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_core/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_core/migration.rb, line 68
68: def self.descendants
69: @descendants ||= []
70: end
Adds the new migration class to the list of Migration descendants.
# File lib/sequel_core/migration.rb, line 73
73: def self.inherited(base)
74: descendants << base
75: end