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

Class Method Summary collapse

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



704
705
706
707
708
709
# File 'lib/sequel/extensions/migration.rb', line 704

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



698
699
700
# File 'lib/sequel/extensions/migration.rb', line 698

def applied_migrations
  @applied_migrations
end

#migration_tuplesObject (readonly)

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



701
702
703
# File 'lib/sequel/extensions/migration.rb', line 701

def migration_tuples
  @migration_tuples
end

Class Method Details

.run_single(db, path, opts = OPTS) ⇒ Object

Apply the migration in the given file path. See Migrator.run for the available options. Additionally, this method supports the :direction option for whether to run the migration up (default) or down.



714
715
716
# File 'lib/sequel/extensions/migration.rb', line 714

def self.run_single(db, path, opts=OPTS)
  new(db, File.dirname(path), opts).run_single(path, opts[:direction] || :up)
end

Instance Method Details

#is_current?Boolean

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

Returns:

  • (Boolean)


720
721
722
# File 'lib/sequel/extensions/migration.rb', line 720

def is_current?
  migration_tuples.empty?
end

#runObject

Apply all migration tuples on the database



725
726
727
728
729
730
# File 'lib/sequel/extensions/migration.rb', line 725

def run
  migration_tuples.each do |m, f, direction|
    apply_migration(m, f, direction)
  end
  nil
end

#run_single(path, direction) ⇒ Object

Apply single migration tuple at the given path with the given direction on the database.



734
735
736
737
738
739
740
741
742
743
# File 'lib/sequel/extensions/migration.rb', line 734

def run_single(path, direction)
  migration = load_migration_file(path)
  file_name = File.basename(path)
  already_applied = applied_migrations.include?(file_name.downcase)

  return if direction == :up ? already_applied : !already_applied

  apply_migration(migration, file_name, direction)
  nil
end