class Sequel::IntegerMigrator
The default migrator, recommended in most cases. Uses a simple
incrementing version number starting with 1, where missing or duplicate
migration file versions are not allowed. Part of the
migration
extension.
Constants
- DEFAULT_SCHEMA_COLUMN
- DEFAULT_SCHEMA_TABLE
- Error
Attributes
The current version for this migrator
The direction of the migrator, either :up or :down
The migrations used by this migrator
Public Class Methods
Set up all state for the migrator instance
# File lib/sequel/extensions/migration.rb, line 505 def initialize(db, directory, opts=OPTS) super @target = opts[:target] || latest_migration_version @current = opts[:current] || current_migration_version raise(Error, "No current version available") unless current raise(Error, "No target version available, probably because no migration files found or filenames don't follow the migration filename convention") unless target @direction = current < target ? :up : :down @migrations = get_migrations end
Public Instance Methods
The integer migrator is current if the current version is the same as the target version.
# File lib/sequel/extensions/migration.rb, line 518 def is_current? current_migration_version == target end
Apply all migrations on the database
# File lib/sequel/extensions/migration.rb, line 523 def run migrations.zip(version_numbers).each do |m, v| t = Time.now db.log_info("Begin applying migration version #{v}, direction: #{direction}") checked_transaction(m) do m.apply(db, direction) set_migration_version(up? ? v : v-1) end db.log_info("Finished applying migration version #{v}, direction: #{direction}, took #{sprintf('%0.6f', Time.now - t)} seconds") end target end
Private Instance Methods
Gets the current migration version stored in the database. If no version number is stored, 0 is returned.
# File lib/sequel/extensions/migration.rb, line 541 def current_migration_version ds.get(column) || 0 end
Returns any found migration files in the supplied directory.
# File lib/sequel/extensions/migration.rb, line 546 def get_migration_files files = [] Dir.new(directory).each do |file| next unless MIGRATION_FILE_PATTERN.match(file) version = migration_version_from_file(file) if version >= 20000101 raise Migrator::Error, "Migration number too large, must use TimestampMigrator: #{file}" end raise(Error, "Duplicate migration version: #{version}") if files[version] files[version] = File.join(directory, file) end 1.upto(files.length - 1){|i| raise(Error, "Missing migration version: #{i}") unless files[i]} unless @allow_missing_migration_files files end
Returns a list of migration classes filtered for the migration range and ordered according to the migration direction.
# File lib/sequel/extensions/migration.rb, line 563 def get_migrations remove_migration_classes # load migration files version_numbers.each{|n| load(files[n])} # get migration classes Migration.descendants end
Returns the latest version available in the specified directory.
# File lib/sequel/extensions/migration.rb, line 574 def latest_migration_version l = files.last l ? migration_version_from_file(File.basename(l)) : nil end
Returns the dataset for the schema_info table. If no such table exists, it is automatically created.
# File lib/sequel/extensions/migration.rb, line 581 def schema_dataset c = column ds = db.from(table) db.create_table?(table){Integer c, :default=>0, :null=>false} unless ds.columns.include?(c) db.alter_table(table){add_column c, Integer, :default=>0, :null=>false} end ds.insert(c=>0) if ds.empty? raise(Error, "More than 1 row in migrator table") if ds.count > 1 ds end
Sets the current migration version stored in the database.
# File lib/sequel/extensions/migration.rb, line 594 def set_migration_version(version) ds.update(column=>version) end
Whether or not this is an up migration
# File lib/sequel/extensions/migration.rb, line 599 def up? direction == :up end
An array of numbers corresponding to the migrations, so that each number in the array is the migration version that will be in affect after the migration is run.
# File lib/sequel/extensions/migration.rb, line 606 def version_numbers versions = files. compact. map{|f| migration_version_from_file(File.basename(f))}. select{|v| up? ? (v > current && v <= target) : (v <= current && v > target)}. sort versions.reverse! unless up? versions end