class Sequel::TimestampMigrator

The migrator used if any migration file version appears to be a timestamp. Stores filenames of migration files, and can figure out which migrations have not been applied and apply them, even if earlier migrations are added after later migrations. If you plan to do that, the responsibility is on you to make sure the migrations don't conflict. Part of the migration extension.

Constants

DEFAULT_SCHEMA_COLUMN
DEFAULT_SCHEMA_TABLE
Error

Attributes

applied_migrations[R]

Array of strings of applied migration filenames

migration_tuples[R]

Get tuples of migrations, filenames, and actions for each migration

Public Class Methods

new(db, directory, opts=OPTS) click to toggle source

Set up all state for the migrator instance

Calls superclass method Sequel::Migrator.new
# File lib/sequel/extensions/migration.rb, line 635
def initialize(db, directory, opts=OPTS)
  super
  @target = opts[:target]
  @applied_migrations = get_applied_migrations
  @migration_tuples = get_migration_tuples
end

Public Instance Methods

is_current?() click to toggle source

The timestamp migrator is current if there are no migrations to apply in either direction.

# File lib/sequel/extensions/migration.rb, line 644
def is_current?
  migration_tuples.empty?
end
run() click to toggle source

Apply all migration tuples on the database

# File lib/sequel/extensions/migration.rb, line 649
def run
  migration_tuples.each do |m, f, direction|
    t = Time.now
    db.log_info("Begin applying migration #{f}, direction: #{direction}")
    checked_transaction(m) do
      m.apply(db, direction)
      fi = f.downcase
      direction == :up ? ds.insert(column=>fi) : ds.filter(column=>fi).delete
    end
    db.log_info("Finished applying migration #{f}, direction: #{direction}, took #{sprintf('%0.6f', Time.now - t)} seconds")
  end
  nil
end

Private Instance Methods

convert_from_schema_info() click to toggle source

Convert the schema_info table to the new schema_migrations table format, using the version of the schema_info table and the current migration files.

# File lib/sequel/extensions/migration.rb, line 667
def convert_from_schema_info
  v = db[IntegerMigrator::DEFAULT_SCHEMA_TABLE].get(IntegerMigrator::DEFAULT_SCHEMA_COLUMN)
  ds = db.from(table)
  files.each do |path|
    f = File.basename(path)
    if migration_version_from_file(f) <= v
      ds.insert(column=>f)
    end
  end
end
get_applied_migrations() click to toggle source

Returns filenames of all applied migrations

# File lib/sequel/extensions/migration.rb, line 679
def get_applied_migrations
  am = ds.select_order_map(column)
  missing_migration_files = am - files.map{|f| File.basename(f).downcase}
  raise(Error, "Applied migration files not in file system: #{missing_migration_files.join(', ')}") if missing_migration_files.length > 0 && !@allow_missing_migration_files
  am
end
get_migration_files() click to toggle source

Returns any migration files found in the migrator's directory.

# File lib/sequel/extensions/migration.rb, line 687
def get_migration_files
  files = []
  Dir.new(directory).each do |file|
    next unless MIGRATION_FILE_PATTERN.match(file)
    files << File.join(directory, file)
  end
  files.sort_by{|f| MIGRATION_FILE_PATTERN.match(File.basename(f))[1].to_i}
end
get_migration_tuples() click to toggle source

Returns tuples of migration, filename, and direction

# File lib/sequel/extensions/migration.rb, line 697
def get_migration_tuples
  remove_migration_classes
  up_mts = []
  down_mts = []
  ms = Migration.descendants
  files.each do |path|
    f = File.basename(path)
    fi = f.downcase
    if target
      if migration_version_from_file(f) > target
        if applied_migrations.include?(fi)
          load(path)
          down_mts << [ms.last, f, :down]
        end
      elsif !applied_migrations.include?(fi)
        load(path)
        up_mts << [ms.last, f, :up]
      end
    elsif !applied_migrations.include?(fi)
      load(path)
      up_mts << [ms.last, f, :up]
    end
  end
  up_mts + down_mts.reverse
end
schema_dataset() click to toggle source

Returns the dataset for the schema_migrations table. If no such table exists, it is automatically created.

# File lib/sequel/extensions/migration.rb, line 725
def schema_dataset
  c = column
  ds = db.from(table)
  if !db.table_exists?(table)
    db.create_table(table){String c, :primary_key=>true}
    if db.table_exists?(:schema_info) and vha = db[:schema_info].all and vha.length == 1 and
       vha.first.keys == [:version] and vha.first.values.first.is_a?(Integer)
      convert_from_schema_info
    end
  elsif !ds.columns.include?(c)
    raise(Error, "Migrator table #{table} does not contain column #{c}")
  end
  ds
end