Class: Sequel::Migration

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


53
54
55
# File 'lib/sequel/lib/sequel/extensions/migration.rb', line 53

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.


86
87
88
# File 'lib/sequel/lib/sequel/extensions/migration.rb', line 86

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.


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sequel/lib/sequel/extensions/migration.rb', line 59

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.


72
73
74
# File 'lib/sequel/lib/sequel/extensions/migration.rb', line 72

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

.inherited(base) ⇒ Object

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


77
78
79
# File 'lib/sequel/lib/sequel/extensions/migration.rb', line 77

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

Instance Method Details

#downObject

The default down action does nothing


82
83
# File 'lib/sequel/lib/sequel/extensions/migration.rb', line 82

def down
end

#upObject

The default up action does nothing


91
92
# File 'lib/sequel/lib/sequel/extensions/migration.rb', line 91

def up
end