class Sequel::Migrator
Part of the migration extension.
Sequel::Migrator.apply(DB, ‘.’, 5, 1)
To migrate the database from version 1 to version 5:
Sequel::Migrator.apply(DB, ‘.’, 4)
For example, to migrate the database to version 4:
Sequel::Migrator.apply(DB, ‘.’, 0)
For example, to migrate the database all the way down:
Sequel::Migrator.apply(DB, ‘.’)
For example, to migrate the database to the latest version:
migration directory.
specified, the database is migrated to the latest version available in the
database, the version is considered to be 0. If no target version is
of the current migration version. If no migration version is stored in the
schema_migrations for timestamped migrations). in the database to keep track
automatically creates a table (schema_info for integer migrations and
no current version is supplied, it is read from the database. The migrator
instance, the directory of migration files and the target version. If
To apply a migrator, the apply method must be invoked with the database
to which to migrate).
if you want to specify the version from which to migrate in addition to the version
You can apply migrations using the Migrator API, as well (this is necessary
directory, and the -M switch specifies the version to which to migrate.
using the -m and -M switches. The -m switch specifies the migration
Migrations are generally run via the sequel command line tool,
subclass or one Sequel.migration call.
The migration files should contain either one Migration
or duplicate migration files.
while the IntegerMigrator is more strict and raises exceptions for missing
as well as migrations with the same timestamp,
The TimestampMigrator can handle migrations that are run out of order
uses the TimestampMigrator to migrate, otherwise it uses the IntegerMigrator.
If any migration filenames use timestamps as version numbers, Sequel
1273257248_add_data_column.rb
1273253850_create_sessions.rb
You can also use timestamps as version numbers:
002_add_data_column.rb
001_create_sessions.rb
For example, the following files are considered migration files:
<version>_<title>.rb
following pattern:
specified directory. The migration files should be named using the
The Migrator class performs migrations based on migration files in a
def self.apply(db, directory, target = nil, current = nil)
def self.apply(db, directory, target = nil, current = nil) run(db, directory, :target => target, :current => current) end
def self.migrator_class(directory)
if the version number appears to be a unix time integer for a year
Choose the Migrator subclass to use. Uses the TimestampMigrator
def self.migrator_class(directory) if self.equal?(Migrator) Dir.new(directory).each do |file| next unless MIGRATION_FILE_PATTERN.match(file) return TimestampMigrator if file.split(MIGRATION_SPLITTER, 2).first.to_i > MINIMUM_TIMESTAMP end IntegerMigrator else self end end
def self.run(db, directory, opts={})
Sequel::Migrator.run(DB, "app1/migrations", :column=> :app2_version)
Sequel::Migrator.run(DB, "migrations", :target=>15, :current=>10)
Sequel::Migrator.run(DB, "migrations")
Examples:
* :target :: The target version to which to migrate. If not given, migrates to the maximum version.
* :table :: The table containing the schema version (default: :schema_info).
using the :table and :column options.
* :current :: The current version of the database. If not given, it is retrieved from the database
* :column :: The column in the :table argument storing the migration version (default: :version).
Migrates the supplied database using the migration files in the the specified directory. Options:
def self.run(db, directory, opts={}) migrator_class(directory).new(db, directory, opts).run end
def checked_transaction(migration, &block)
If transactions should be used for the migration, yield to the block
def checked_transaction(migration, &block) if @use_transactions == false yield elsif migration.use_transactions || @use_transactions db.transaction(&block) else yield end end
def initialize(db, directory, opts={})
def initialize(db, directory, opts={}) raise(Error, "Must supply a valid migration path") unless File.directory?(directory) @db = db @directory = directory @files = get_migration_files schema, table = @db.send(:schema_and_table, opts[:table] || self.class.const_get(:DEFAULT_SCHEMA_TABLE)) @table = schema ? Sequel::SQL::QualifiedIdentifier.new(schema, table) : table @column = opts[:column] || self.class.const_get(:DEFAULT_SCHEMA_COLUMN) @ds = schema_dataset @use_transactions = opts[:use_transactions] end
def migration_version_from_file(filename)
def migration_version_from_file(filename) filename.split(MIGRATION_SPLITTER, 2).first.to_i end
def remove_migration_classes
Remove all migration classes. Done by the migrator to ensure that
def remove_migration_classes # Remove class definitions Migration.descendants.each do |c| Object.send(:remove_const, c.to_s) rescue nil end Migration.descendants.clear # remove any defined migration classes end