class Sequel::Migrator
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, 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.
Constants
- MIGRATION_FILE_PATTERN
- MUTEX
Mutex used around migration file loading
Attributes
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)
The database related to this migrator
The directory for this migrator's files
The dataset for this migrator, representing the schema_info
table for integer migrations and the schema_migrations
table for timestamp migrations
All migration files in this migrator's directory
The table to use to hold the applied migration data (defaults to :schema_info for integer migrations and :schema_migrations for timestamp migrations)
The target version for this migrator
Public Class Methods
Wrapper for run
, maintaining backwards API compatibility
# File lib/sequel/extensions/migration.rb 370 def self.apply(db, directory, target = nil, current = nil) 371 run(db, directory, :target => target, :current => current) 372 end
Raise a NotCurrentError
unless the migrator is current, takes the same arguments as run.
# File lib/sequel/extensions/migration.rb 376 def self.check_current(*args) 377 raise(NotCurrentError, 'migrator is not current') unless is_current?(*args) 378 end
Return whether the migrator is current (i.e. it does not need to make any changes). Takes the same arguments as run.
# File lib/sequel/extensions/migration.rb 382 def self.is_current?(db, directory, opts=OPTS) 383 migrator_class(directory).new(db, directory, opts).is_current? 384 end
Choose the Migrator
subclass to use. Uses the TimestampMigrator
if the version number is greater than 20000101, otherwise uses the IntegerMigrator
.
# File lib/sequel/extensions/migration.rb 408 def self.migrator_class(directory) 409 if self.equal?(Migrator) 410 raise(Error, "Must supply a valid migration path") unless File.directory?(directory) 411 Dir.new(directory).each do |file| 412 next unless MIGRATION_FILE_PATTERN.match(file) 413 return TimestampMigrator if file.split('_', 2).first.to_i > 20000101 414 end 415 IntegerMigrator 416 else 417 self 418 end 419 end
Setup the state for the migrator
# File lib/sequel/extensions/migration.rb 447 def initialize(db, directory, opts=OPTS) 448 raise(Error, "Must supply a valid migration path") unless File.directory?(directory) 449 @db = db 450 @directory = directory 451 @allow_missing_migration_files = opts[:allow_missing_migration_files] 452 @files = get_migration_files 453 schema, table = @db.send(:schema_and_table, opts[:table] || default_schema_table) 454 @table = schema ? Sequel::SQL::QualifiedIdentifier.new(schema, table) : table 455 @column = opts[:column] || default_schema_column 456 @ds = schema_dataset 457 @use_transactions = opts[:use_transactions] 458 end
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.
- :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)
# File lib/sequel/extensions/migration.rb 402 def self.run(db, directory, opts=OPTS) 403 migrator_class(directory).new(db, directory, opts).run 404 end
Private Instance Methods
If transactions should be used for the migration, yield to the block inside a transaction. Otherwise, just yield to the block.
# File lib/sequel/extensions/migration.rb 464 def checked_transaction(migration, &block) 465 use_trans = if @use_transactions.nil? 466 if migration.use_transactions.nil? 467 @db.supports_transactional_ddl? 468 else 469 migration.use_transactions 470 end 471 else 472 @use_transactions 473 end 474 475 if use_trans 476 db.transaction(&block) 477 else 478 yield 479 end 480 end
Load the migration file, raising an exception if the file does not define a single migration.
# File lib/sequel/extensions/migration.rb 484 def load_migration_file(file) 485 MUTEX.synchronize do 486 n = Migration.descendants.length 487 load(file) 488 raise Error, "Migration file #{file.inspect} not containing a single migration detected" unless n + 1 == Migration.descendants.length 489 c = Migration.descendants.pop 490 if c.is_a?(Class) && !c.name.to_s.empty? && Object.const_defined?(c.name) 491 Object.send(:remove_const, c.name) 492 end 493 c 494 end 495 end
Return the integer migration version based on the filename.
# File lib/sequel/extensions/migration.rb 498 def migration_version_from_file(filename) 499 filename.split('_', 2).first.to_i 500 end