Class: Sequel::Migration

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

### Methods

apply descendants down inherited method_missing new up

### Public Class methods

apply(db, direction)

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

[Source]

        # File lib/sequel_core/migration.rb, line 55
    55:     def self.apply(db, direction)
    56:       obj = new(db)
    57:       case direction
    58:       when :up
    59:         obj.up
    60:       when :down
    61:         obj.down
    62:       else
    63:         raise ArgumentError, "Invalid migration direction specified (#{direction.inspect})"
    64:       end
    65:     end

descendants()

Returns the list of Migration descendants.

[Source]

        # File lib/sequel_core/migration.rb, line 68
    68:     def self.descendants
    69:       @descendants ||= []
    70:     end

inherited(base)

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

[Source]

        # File lib/sequel_core/migration.rb, line 73
    73:     def self.inherited(base)
    74:       descendants << base
    75:     end

new(db)

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

[Source]

        # File lib/sequel_core/migration.rb, line 49
    49:     def initialize(db)
    50:       @db = db
    51:     end

### Public Instance methods

down()

The default down action does nothing

[Source]

        # File lib/sequel_core/migration.rb, line 78
    78:     def down
    79:     end

method_missing(method_sym, *args, &block)

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

[Source]

        # File lib/sequel_core/migration.rb, line 82
    82:     def method_missing(method_sym, *args, &block)
    83:       @db.send(method_sym, *args, &block)
    84:     end

up()

The default up action does nothing

[Source]

        # File lib/sequel_core/migration.rb, line 87
    87:     def up
    88:     end

[Validate]