Module Sequel::Migrator
In: lib/sequel/extensions/migration.rb
lib/sequel/deprecated_migration.rb

The Migrator module performs migrations based on migration files in a specified directory. The migration files should be named using the following pattern (in similar fashion to ActiveRecord migrations):

  <version>_<title>.rb

For example, the following files are considered migration files:

  001_create_sessions.rb
  002_add_data_column.rb
  ...

The migration files should contain one or more migration classes based on Sequel::Migration.

Migrations are generally run via the sequel command line tool, using the -m and -M switches. The -m switch specifies the migration directory, and the -M switch specifies the version to which to migrate.

You can apply migrations using the Migrator API, as well (this is necessary if you want to specify the version from which to migrate in addition to the version to which to migrate). To apply a migration, the apply method must be invoked with the database instance, the directory of migration files and the target version. If no current version is supplied, it is read from the database. The migrator automatically creates a schema_info table in the database to keep track of the current migration version. If no migration version is stored in the database, the version is considered to be 0. If no target version is specified, the database is migrated to the latest version available in the migration directory.

For example, to migrate the database to the latest version:

  Sequel::Migrator.apply(DB, '.')

To migrate the database from version 1 to version 5:

  Sequel::Migrator.apply(DB, '.', 5, 1)

Methods

Constants

MIGRATION_FILE_PATTERN = /\A\d+_.+\.rb\z/.freeze

Public Class methods

Migrates the supplied database in the specified directory from the current version to the target version. If no current version is supplied, it is extracted from a schema_info table. The schema_info table is automatically created and maintained by the apply function.

[Source]

     # File lib/sequel/extensions/migration.rb, line 136
136:     def self.apply(db, directory, target = nil, current = nil)
137:       # determine current and target version and direction
138:       current ||= get_current_migration_version(db)
139:       target ||= latest_migration_version(directory)
140:       raise Error, "No current version available" if current.nil?
141:       raise Error, "No target version available" if target.nil?
142: 
143:       direction = current < target ? :up : :down
144:       
145:       classes = migration_classes(directory, target, current, direction)
146:       
147:       db.transaction do
148:         classes.each {|c| c.apply(db, direction)}
149:         set_current_migration_version(db, target)
150:       end
151:       
152:       target
153:     end

[Source]

    # File lib/sequel/deprecated_migration.rb, line 45
45:     def self.apply(db, directory, target = nil, current = nil)
46:       Deprecation.deprecate('Sequel::Migrator', "require 'sequel/extensions/migration' first")
47:       require 'sequel/extensions/migration'
48:       apply(db, directory, target, current)
49:     end

[Source]

    # File lib/sequel/deprecated_migration.rb, line 51
51:     def self.get_current_migration_version(db)
52:       Deprecation.deprecate('Sequel::Migrator', "require 'sequel/extensions/migration' first")
53:       require 'sequel/extensions/migration'
54:       r = schema_info_dataset(db).first
55:       r ? r[:version] : 0
56:     end

Gets the current migration version stored in the database. If no version number is stored, 0 is returned.

[Source]

     # File lib/sequel/extensions/migration.rb, line 157
157:     def self.get_current_migration_version(db)
158:       r = schema_info_dataset(db).first
159:       r ? r[:version] : 0
160:     end

Returns the latest version available in the specified directory.

[Source]

     # File lib/sequel/extensions/migration.rb, line 163
163:     def self.latest_migration_version(directory)
164:       l = migration_files(directory).last
165:       l ? File.basename(l).to_i : nil
166:     end

[Source]

    # File lib/sequel/deprecated_migration.rb, line 58
58:     def self.latest_migration_version(directory)
59:       Deprecation.deprecate('Sequel::Migrator', "require 'sequel/extensions/migration' first")
60:       require 'sequel/extensions/migration'
61:       l = migration_files(directory).last
62:       l ? File.basename(l).to_i : nil
63:     end

Returns a list of migration classes filtered for the migration range and ordered according to the migration direction.

[Source]

     # File lib/sequel/extensions/migration.rb, line 170
170:     def self.migration_classes(directory, target, current, direction)
171:       range = direction == :up ?
172:         (current + 1)..target : (target + 1)..current
173: 
174:       # Remove class definitions
175:       Migration.descendants.each do |c|
176:         Object.send(:remove_const, c.to_s) rescue nil
177:       end
178:       Migration.descendants.clear # remove any defined migration classes
179: 
180:       # load migration files
181:       migration_files(directory, range).each {|fn| load(fn)}
182:       
183:       # get migration classes
184:       classes = Migration.descendants
185:       classes.reverse! if direction == :down
186:       classes
187:     end

[Source]

    # File lib/sequel/deprecated_migration.rb, line 65
65:     def self.migration_classes(directory, target, current, direction)
66:       Deprecation.deprecate('Sequel::Migrator', "require 'sequel/extensions/migration' first")
67:       require 'sequel/extensions/migration'
68:       migration_classes(directory, target, current, direction)
69:     end

Returns any found migration files in the supplied directory.

[Source]

     # File lib/sequel/extensions/migration.rb, line 190
190:     def self.migration_files(directory, range = nil)
191:       files = []
192:       Dir.new(directory).each do |file|
193:         files[file.to_i] = File.join(directory, file) if MIGRATION_FILE_PATTERN.match(file)
194:       end
195:       filtered = range ? files[range] : files
196:       filtered ? filtered.compact : []
197:     end

[Source]

    # File lib/sequel/deprecated_migration.rb, line 71
71:     def self.migration_files(directory, range = nil)
72:       Deprecation.deprecate('Sequel::Migrator', "require 'sequel/extensions/migration' first")
73:       require 'sequel/extensions/migration'
74:       migration_files(directory, range)
75:     end

Returns the dataset for the schema_info table. If no such table exists, it is automatically created.

[Source]

     # File lib/sequel/extensions/migration.rb, line 201
201:     def self.schema_info_dataset(db)
202:       db.create_table(:schema_info) {integer :version} unless db.table_exists?(:schema_info)
203:       db[:schema_info]
204:     end

[Source]

    # File lib/sequel/deprecated_migration.rb, line 77
77:     def self.schema_info_dataset(db)
78:       Deprecation.deprecate('Sequel::Migrator', "require 'sequel/extensions/migration' first")
79:       require 'sequel/extensions/migration'
80:       db.create_table(:schema_info) {integer :version} unless db.table_exists?(:schema_info)
81:       db[:schema_info]
82:     end

Sets the current migration version stored in the database.

[Source]

     # File lib/sequel/extensions/migration.rb, line 207
207:     def self.set_current_migration_version(db, version)
208:       dataset = schema_info_dataset(db)
209:       dataset.send(dataset.first ? :update : :<<, :version => version)
210:     end

[Source]

    # File lib/sequel/deprecated_migration.rb, line 84
84:     def self.set_current_migration_version(db, version)
85:       Deprecation.deprecate('Sequel::Migrator', "require 'sequel/extensions/migration' first")
86:       require 'sequel/extensions/migration'
87:       dataset = schema_info_dataset(db)
88:       dataset.send(dataset.first ? :update : :<<, :version => version)
89:     end

[Validate]