Class: Sequel::Migrator

Inherits:
Object show all
Defined in:
lib/sequel/extensions/migration.rb

Overview

The Migrator class performs migrations based on migration files in a specified directory. The migration files should be named using the following pattern:

<version>_<title>.rb

For example, the following files are considered migration files:

001_create_sessions.rb
002_add_data_column.rb

You can also use timestamps as version numbers:

1273253850_create_sessions.rb
1273257248_add_data_column.rb

If any migration filenames use timestamps as version numbers, Sequel uses the TimestampMigrator to migrate, otherwise it uses the IntegerMigrator. The TimestampMigrator can handle migrations that are run out of order as well as migrations with the same timestamp, while the IntegerMigrator is more strict and raises exceptions for missing or duplicate migration files.

The migration files should contain either one Migration subclass or one Sequel.migration call.

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 migrator, 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 table (schema_info for integer migrations and schema_migrations for timestamped migrations). 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, or the target version specified is greater than the latest version available, 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.run(DB, '.')

For example, to migrate the database all the way down:

Sequel::Migrator.run(DB, '.', target: 0)

For example, to migrate the database to version 4:

Sequel::Migrator.run(DB, '.', target: 4)

To migrate the database from version 1 to version 5:

Sequel::Migrator.run(DB, '.', target: 5, current: 1)

Part of the migration extension.

Direct Known Subclasses

IntegerMigrator, TimestampMigrator

Defined Under Namespace

Classes: Error, NotCurrentError

Constant Summary collapse

MIGRATION_FILE_PATTERN =
/\A(\d+)_.+\.rb\z/i.freeze
MUTEX =

Mutex used around migration file loading

Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, directory, opts = OPTS) ⇒ Migrator

Setup the state for the migrator

Raises:



457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/sequel/extensions/migration.rb', line 457

def initialize(db, directory, opts=OPTS)
  raise(Error, "Must supply a valid migration path") unless File.directory?(directory)
  @db = db
  @directory = directory
  @allow_missing_migration_files = opts[:allow_missing_migration_files]
  @files = get_migration_files
  schema, table = @db.send(:schema_and_table, opts[:table] || default_schema_table)
  @table = schema ? Sequel::SQL::QualifiedIdentifier.new(schema, table) : table
  @column = opts[:column] || default_schema_column
  @ds = schema_dataset
  @use_transactions = opts[:use_transactions]
end

Instance Attribute Details

#columnObject (readonly)

The column to use to hold the migration version number for integer migrations or filename for timestamp migrations (defaults to :version for integer migrations and :filename for timestamp migrations)



434
435
436
# File 'lib/sequel/extensions/migration.rb', line 434

def column
  @column
end

#dbObject (readonly)

The database related to this migrator



437
438
439
# File 'lib/sequel/extensions/migration.rb', line 437

def db
  @db
end

#directoryObject (readonly)

The directory for this migrator’s files



440
441
442
# File 'lib/sequel/extensions/migration.rb', line 440

def directory
  @directory
end

#dsObject (readonly)

The dataset for this migrator, representing the schema_info table for integer migrations and the schema_migrations table for timestamp migrations



444
445
446
# File 'lib/sequel/extensions/migration.rb', line 444

def ds
  @ds
end

#filesObject (readonly)

All migration files in this migrator’s directory



447
448
449
# File 'lib/sequel/extensions/migration.rb', line 447

def files
  @files
end

#tableObject (readonly)

The table to use to hold the applied migration data (defaults to :schema_info for integer migrations and :schema_migrations for timestamp migrations)



451
452
453
# File 'lib/sequel/extensions/migration.rb', line 451

def table
  @table
end

#targetObject (readonly)

The target version for this migrator



454
455
456
# File 'lib/sequel/extensions/migration.rb', line 454

def target
  @target
end

Class Method Details

.apply(db, directory, target = nil, current = nil) ⇒ Object

Wrapper for run, maintaining backwards API compatibility



377
378
379
# File 'lib/sequel/extensions/migration.rb', line 377

def self.apply(db, directory, target = nil, current = nil)
  run(db, directory, :target => target, :current => current)
end

.check_current(*args) ⇒ Object

Raise a NotCurrentError unless the migrator is current, takes the same arguments as #run.

Raises:



383
384
385
# File 'lib/sequel/extensions/migration.rb', line 383

def self.check_current(*args)
  raise(NotCurrentError, 'current migration version does not match latest available version') unless is_current?(*args)
end

.is_current?(db, directory, opts = OPTS) ⇒ Boolean

Return whether the migrator is current (i.e. it does not need to make any changes). Takes the same arguments as #run.

Returns:

  • (Boolean)


389
390
391
# File 'lib/sequel/extensions/migration.rb', line 389

def self.is_current?(db, directory, opts=OPTS)
  migrator_class(directory).new(db, directory, opts).is_current?
end

.migrator_class(directory) ⇒ Object

Choose the Migrator subclass to use. Uses the TimestampMigrator if the version number is greater than 20000101, otherwise uses the IntegerMigrator.



418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/sequel/extensions/migration.rb', line 418

def self.migrator_class(directory)
  if self.equal?(Migrator)
    raise(Error, "Must supply a valid migration path") unless File.directory?(directory)
    Dir.new(directory).each do |file|
      next unless MIGRATION_FILE_PATTERN.match(file)
      return TimestampMigrator if file.split('_', 2).first.to_i > 20000101
    end
    IntegerMigrator
  else
    self
  end
end

.run(db, directory, opts = OPTS) ⇒ Object

Migrates the supplied database using the migration files in the specified directory. Options:

:allow_missing_migration_files

Don’t raise an error if there are missing migration files. It is very risky to use this option, since it can result in the database schema version number not matching the expected database schema.

:column

The column in the :table argument storing the migration version (default: :version).

:current

The current version of the database. If not given, it is retrieved from the database using the :table and :column options.

:relative

Run the given number of migrations, with a positive number being migrations to migrate up, and a negative number being migrations to migrate down (IntegerMigrator only).

:table

The table containing the schema version (default: :schema_info for integer migrations and :schema_migrations for timestamped migrations).

:target

The target version to which to migrate. If not given, migrates to the maximum version.

Examples:

Sequel::Migrator.run(DB, "migrations")
Sequel::Migrator.run(DB, "migrations", target: 15, current: 10)
Sequel::Migrator.run(DB, "app1/migrations", column: :app2_version)
Sequel::Migrator.run(DB, "app2/migrations", column: :app2_version, table: :schema_info2)


412
413
414
# File 'lib/sequel/extensions/migration.rb', line 412

def self.run(db, directory, opts=OPTS)
  migrator_class(directory).new(db, directory, opts).run
end