Class: Sequel::Migration

Inherits:
Object show all
Defined in:
lib/sequel_core/migration.rb

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
    timestamp :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)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ Migration

Creates a new instance of the migration and sets the @db attribute.



27
28
29
# File 'lib/sequel_core/migration.rb', line 27

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.



60
61
62
# File 'lib/sequel_core/migration.rb', line 60

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.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sequel_core/migration.rb', line 33

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.



46
47
48
# File 'lib/sequel_core/migration.rb', line 46

def self.descendants
  @descendants ||= []
end

.inherited(base) ⇒ Object

Adds the new migration class to the list of Migration descendants.



51
52
53
# File 'lib/sequel_core/migration.rb', line 51

def self.inherited(base)
  descendants << base
end

Instance Method Details

#down ⇒ Object

The default down action does nothing



56
57
# File 'lib/sequel_core/migration.rb', line 56

def down
end

#up ⇒ Object

The default up action does nothing



65
66
# File 'lib/sequel_core/migration.rb', line 65

def up
end