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
    # 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, :text, :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.

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.



49
50
51
# File 'lib/sequel_core/migration.rb', line 49

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.



82
83
84
# File 'lib/sequel_core/migration.rb', line 82

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.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sequel_core/migration.rb', line 55

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.



68
69
70
# File 'lib/sequel_core/migration.rb', line 68

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

.inherited(base) ⇒ Object

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



73
74
75
# File 'lib/sequel_core/migration.rb', line 73

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

Instance Method Details

#downObject

The default down action does nothing



78
79
# File 'lib/sequel_core/migration.rb', line 78

def down
end

#upObject

The default up action does nothing



87
88
# File 'lib/sequel_core/migration.rb', line 87

def up
end