Class: Sequel::TimestampMigrator

Inherits:
Migrator show all
Defined in:
lib/sequel/extensions/migration.rb

Overview

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.

Constant Summary collapse

DEFAULT_SCHEMA_COLUMN =
:filename
DEFAULT_SCHEMA_TABLE =
:schema_migrations
Error =
Migrator::Error

Constants inherited from Migrator

Migrator::MIGRATION_FILE_PATTERN, Migrator::MIGRATION_SPLITTER, Migrator::MINIMUM_TIMESTAMP

Instance Attribute Summary collapse

Attributes inherited from Migrator

#column, #db, #directory, #ds, #files, #table, #target

Instance Method Summary collapse

Methods inherited from Migrator

apply, run

Constructor Details

#initialize(db, directory, opts = {}) ⇒ TimestampMigrator

Set up all state for the migrator instance



540
541
542
543
544
545
# File 'lib/sequel/extensions/migration.rb', line 540

def initialize(db, directory, opts={})
  super
  @target = opts[:target]
  @applied_migrations = get_applied_migrations
  @migration_tuples = get_migration_tuples
end

Instance Attribute Details

#applied_migrationsObject (readonly)

Array of strings of applied migration filenames



534
535
536
# File 'lib/sequel/extensions/migration.rb', line 534

def applied_migrations
  @applied_migrations
end

#migration_tuplesObject (readonly)

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



537
538
539
# File 'lib/sequel/extensions/migration.rb', line 537

def migration_tuples
  @migration_tuples
end

Instance Method Details

#runObject

Apply all migration tuples on the database



548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/sequel/extensions/migration.rb', line 548

def run
  migration_tuples.each do |m, f, direction|
    t = Time.now
    db.log_info("Begin applying migration #{f}, direction: #{direction}")
    db.transaction 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