Class Sequel::Migration
In: lib/sequel/extensions/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.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.

Methods

apply   descendants   down   inherited   method_missing   new   up  

Public Class methods

Applies the migration to the supplied database in the specified direction.

[Source]

    # File lib/sequel/extensions/migration.rb, line 59
59:     def self.apply(db, direction)
60:       obj = new(db)
61:       case direction
62:       when :up
63:         obj.up
64:       when :down
65:         obj.down
66:       else
67:         raise ArgumentError, "Invalid migration direction specified (#{direction.inspect})"
68:       end
69:     end

Returns the list of Migration descendants.

[Source]

    # File lib/sequel/extensions/migration.rb, line 72
72:     def self.descendants
73:       @descendants ||= []
74:     end

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

[Source]

    # File lib/sequel/extensions/migration.rb, line 77
77:     def self.inherited(base)
78:       descendants << base
79:     end

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

[Source]

    # File lib/sequel/extensions/migration.rb, line 53
53:     def initialize(db)
54:       @db = db
55:     end

Public Instance methods

The default down action does nothing

[Source]

    # File lib/sequel/extensions/migration.rb, line 82
82:     def down
83:     end

Intercepts method calls intended for the database and sends them along.

[Source]

    # File lib/sequel/extensions/migration.rb, line 86
86:     def method_missing(method_sym, *args, &block)
87:       @db.send(method_sym, *args, &block)
88:     end

The default up action does nothing

[Source]

    # File lib/sequel/extensions/migration.rb, line 91
91:     def up
92:     end

[Validate]