Parent

Class Index [+]

Quicksearch

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.apply(DB, '.')

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

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

For example, to migrate the database to version 4:

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

To migrate the database from version 1 to version 5:

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

Part of the migration extension.

Constants

MIGRATION_FILE_PATTERN
MIGRATION_SPLITTER
MINIMUM_TIMESTAMP

Attributes

column[R]

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)

db[R]

The database related to this migrator

directory[R]

The directory for this migrator’s files

ds[R]

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

files[R]

All migration files in this migrator’s directory

table[R]

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

target[R]

The target version for this migrator

Public Class Methods

apply(db, directory, target = nil, current = nil) click to toggle source

Wrapper for run, maintaining backwards API compatibility

     # File lib/sequel/extensions/migration.rb, line 349
349:     def self.apply(db, directory, target = nil, current = nil)
350:       run(db, directory, :target => target, :current => current)
351:     end
check_current(*args) click to toggle source

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

     # File lib/sequel/extensions/migration.rb, line 355
355:     def self.check_current(*args)
356:       raise(NotCurrentError, 'migrator is not current') unless is_current?(*args)
357:     end
is_current?(db, directory, opts={}) click to toggle source

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

     # File lib/sequel/extensions/migration.rb, line 361
361:     def self.is_current?(db, directory, opts={})
362:       migrator_class(directory).new(db, directory, opts).is_current?
363:     end
new(db, directory, opts={}) click to toggle source

Setup the state for the migrator

     # File lib/sequel/extensions/migration.rb, line 423
423:     def initialize(db, directory, opts={})
424:       raise(Error, "Must supply a valid migration path") unless File.directory?(directory)
425:       @db = db
426:       @directory = directory
427:       @files = get_migration_files
428:       schema, table = @db.send(:schema_and_table, opts[:table]  || self.class.const_get(:DEFAULT_SCHEMA_TABLE))
429:       @table = schema ? Sequel::SQL::QualifiedIdentifier.new(schema, table) : table
430:       @column = opts[:column] || self.class.const_get(:DEFAULT_SCHEMA_COLUMN)
431:       @ds = schema_dataset
432:       @use_transactions = opts[:use_transactions]
433:     end
run(db, directory, opts={}) click to toggle source

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

  • :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.

  • :table

    The table containing the schema version (default: :schema_info).

  • :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, line 377
377:     def self.run(db, directory, opts={})
378:       migrator_class(directory).new(db, directory, opts).run
379:     end

Private Class Methods

migrator_class(directory) click to toggle source

Choose the Migrator subclass to use. Uses the TimestampMigrator if the version number appears to be a unix time integer for a year after 2005, otherwise uses the IntegerMigrator.

     # File lib/sequel/extensions/migration.rb, line 384
384:     def self.migrator_class(directory)
385:       if self.equal?(Migrator)
386:         Dir.new(directory).each do |file|
387:           next unless MIGRATION_FILE_PATTERN.match(file)
388:           return TimestampMigrator if file.split(MIGRATION_SPLITTER, 2).first.to_i > MINIMUM_TIMESTAMP
389:         end
390:         IntegerMigrator
391:       else
392:         self
393:       end
394:     end

Private Instance Methods

checked_transaction(migration, &block) click to toggle source

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, line 439
439:     def checked_transaction(migration, &block)
440:       use_trans = if @use_transactions.nil?
441:         if migration.use_transactions.nil?
442:           @db.supports_transactional_ddl?
443:         else
444:           migration.use_transactions
445:         end
446:       else
447:         @use_transactions
448:       end
449: 
450:       if use_trans
451:         db.transaction(&block)
452:       else
453:         yield
454:       end
455:     end
migration_version_from_file(filename) click to toggle source

Return the integer migration version based on the filename.

     # File lib/sequel/extensions/migration.rb, line 468
468:     def migration_version_from_file(filename)
469:       filename.split(MIGRATION_SPLITTER, 2).first.to_i
470:     end
remove_migration_classes() click to toggle source

Remove all migration classes. Done by the migrator to ensure that the correct migration classes are picked up.

     # File lib/sequel/extensions/migration.rb, line 459
459:     def remove_migration_classes
460:       # Remove class definitions
461:       Migration.descendants.each do |c|
462:         Object.send(:remove_const, c.to_s) rescue nil
463:       end
464:       Migration.descendants.clear # remove any defined migration classes
465:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.