Class: Sequel::Migration

Inherits:
Object show all
Defined in:
lib/sequel/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.



31
32
33
# File 'lib/sequel/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.



63
64
65
# File 'lib/sequel/migration.rb', line 63

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.



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sequel/migration.rb', line 50

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

.descendantsObject

Returns the list of Migration descendants.



41
42
43
# File 'lib/sequel/migration.rb', line 41

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

.inherited(base) ⇒ Object

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



36
37
38
# File 'lib/sequel/migration.rb', line 36

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

Instance Method Details

#downObject

:nodoc:



46
# File 'lib/sequel/migration.rb', line 46

def down; end

#upObject

:nodoc:



45
# File 'lib/sequel/migration.rb', line 45

def up; end