Class: Sequel::IntegerMigrator

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

Overview

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.

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) ⇒ IntegerMigrator

Set up all state for the migrator instance

Raises:



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
# File 'lib/sequel/extensions/migration.rb', line 525

def initialize(db, directory, opts=OPTS)
  super
  @current = opts[:current] || current_migration_version

  latest_version = latest_migration_version
  @target = if opts[:target]
    opts[:target]
  elsif opts[:relative]
    @current + opts[:relative]
  else
    latest_version
  end

  raise(Error, "No target and/or latest version available, probably because no migration files found or filenames don't follow the migration filename convention") unless target && latest_version

  if @target > latest_version
    @target = latest_version
  elsif @target < 0
    @target = 0
  end

  @direction = current < target ? :up : :down

  if @direction == :down && @current >= @files.length && !@allow_missing_migration_files
    raise Migrator::Error, "Missing migration version(s) needed to migrate down to target version (current: #{current}, target: #{target})"
  end

  @migrations = get_migrations
end

Instance Attribute Details

#currentObject (readonly)

The current version for this migrator



516
517
518
# File 'lib/sequel/extensions/migration.rb', line 516

def current
  @current
end

#directionObject (readonly)

The direction of the migrator, either :up or :down



519
520
521
# File 'lib/sequel/extensions/migration.rb', line 519

def direction
  @direction
end

#migrationsObject (readonly)

The migrations used by this migrator



522
523
524
# File 'lib/sequel/extensions/migration.rb', line 522

def migrations
  @migrations
end

Instance Method Details

#is_current?Boolean

The integer migrator is current if the current version is the same as the target version.

Returns:

  • (Boolean)


556
557
558
# File 'lib/sequel/extensions/migration.rb', line 556

def is_current?
  current_migration_version == target
end

#runObject

Apply all migrations on the database



561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/sequel/extensions/migration.rb', line 561

def run
  migrations.zip(version_numbers).each do |m, v|
    timer = Sequel.start_timer
    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', Sequel.elapsed_seconds_since(timer))} seconds")
  end
  
  target
end