Class: Sequel::Migrator
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, 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.
Direct Known Subclasses
Defined Under Namespace
Classes: Error
Constant Summary collapse
- MIGRATION_FILE_PATTERN =
/\A\d+_.+\.rb\z/i.freeze
- MIGRATION_SPLITTER =
'_'.freeze
- MINIMUM_TIMESTAMP =
20000101
Instance Attribute Summary collapse
-
#column ⇒ Object
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).
-
#db ⇒ Object
readonly
The database related to this migrator.
-
#directory ⇒ Object
readonly
The directory for this migrator’s files.
-
#ds ⇒ Object
readonly
The dataset for this migrator, representing the
schema_info
table for integer migrations and theschema_migrations
table for timestamp migrations. -
#files ⇒ Object
readonly
All migration files in this migrator’s directory.
-
#table ⇒ Object
readonly
The table to use to hold the applied migration data (defaults to :schema_info for integer migrations and :schema_migrations for timestamp migrations).
-
#target ⇒ Object
readonly
The target version for this migrator.
Class Method Summary collapse
-
.apply(db, directory, target = nil, current = nil) ⇒ Object
Wrapper for
run
, maintaining backwards API compatibility. -
.run(db, directory, opts = {}) ⇒ Object
Migrates the supplied database using the migration files in the the specified directory.
Instance Method Summary collapse
-
#initialize(db, directory, opts = {}) ⇒ Migrator
constructor
Setup the state for the migrator.
Constructor Details
#initialize(db, directory, opts = {}) ⇒ Migrator
Setup the state for the migrator
257 258 259 260 261 262 263 264 265 |
# File 'lib/sequel/extensions/migration.rb', line 257 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 @table = opts[:table] || self.class.const_get(:DEFAULT_SCHEMA_TABLE) @column = opts[:column] || self.class.const_get(:DEFAULT_SCHEMA_COLUMN) @ds = schema_dataset end |
Instance Attribute Details
#column ⇒ Object (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)
234 235 236 |
# File 'lib/sequel/extensions/migration.rb', line 234 def column @column end |
#db ⇒ Object (readonly)
The database related to this migrator
237 238 239 |
# File 'lib/sequel/extensions/migration.rb', line 237 def db @db end |
#directory ⇒ Object (readonly)
The directory for this migrator’s files
240 241 242 |
# File 'lib/sequel/extensions/migration.rb', line 240 def directory @directory end |
#ds ⇒ Object (readonly)
The dataset for this migrator, representing the schema_info
table for integer migrations and the schema_migrations
table for timestamp migrations
244 245 246 |
# File 'lib/sequel/extensions/migration.rb', line 244 def ds @ds end |
#files ⇒ Object (readonly)
All migration files in this migrator’s directory
247 248 249 |
# File 'lib/sequel/extensions/migration.rb', line 247 def files @files end |
#table ⇒ Object (readonly)
The table to use to hold the applied migration data (defaults to :schema_info for integer migrations and :schema_migrations for timestamp migrations)
251 252 253 |
# File 'lib/sequel/extensions/migration.rb', line 251 def table @table end |
#target ⇒ Object (readonly)
The target version for this migrator
254 255 256 |
# File 'lib/sequel/extensions/migration.rb', line 254 def target @target end |
Class Method Details
.apply(db, directory, target = nil, current = nil) ⇒ Object
Wrapper for run
, maintaining backwards API compatibility
199 200 201 |
# File 'lib/sequel/extensions/migration.rb', line 199 def self.apply(db, directory, target = nil, current = nil) run(db, directory, :target => target, :current => current) end |
.run(db, directory, opts = {}) ⇒ Object
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)
215 216 217 |
# File 'lib/sequel/extensions/migration.rb', line 215 def self.run(db, directory, opts={}) migrator_class(directory).new(db, directory, opts).run end |