Class: Sequel::TimestampMigrator

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

Overview

The migrator used if any migration file version is greater than 20000101. 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

Error =
Migrator::Error

Constants inherited from Migrator

Migrator::MIGRATION_FILE_PATTERN, Migrator::MUTEX

Instance Attribute Summary collapse

Attributes inherited from Migrator

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

Instance Method Summary collapse

Methods inherited from Migrator

apply, check_current, is_current?, migrator_class, run

Constructor Details

#initialize(db, directory, opts = OPTS) ⇒ TimestampMigrator

Set up all state for the migrator instance



665
666
667
668
669
670
# File 'lib/sequel/extensions/migration.rb', line 665

def initialize(db, directory, opts=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



659
660
661
# File 'lib/sequel/extensions/migration.rb', line 659

def applied_migrations
  @applied_migrations
end

#migration_tuplesObject (readonly)

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



662
663
664
# File 'lib/sequel/extensions/migration.rb', line 662

def migration_tuples
  @migration_tuples
end

Instance Method Details

#is_current?Boolean

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

Returns:

  • (Boolean)


674
675
676
# File 'lib/sequel/extensions/migration.rb', line 674

def is_current?
  migration_tuples.empty?
end

#runObject

Apply all migration tuples on the database



679
680
681
682
683
684
685
686
687
688
689
690
691
# File 'lib/sequel/extensions/migration.rb', line 679

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.where(column=>fi).delete
    end
    db.log_info("Finished applying migration #{f}, direction: #{direction}, took #{sprintf('%0.6f', Time.now - t)} seconds")
  end
  nil
end