class Sequel::IntegerMigrator

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.

Constants

DEFAULT_SCHEMA_COLUMN
DEFAULT_SCHEMA_TABLE
Error

Attributes

current[R]

The current version for this migrator

direction[R]

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

migrations[R]

The migrations used by this migrator

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 523
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

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

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

  @direction = current < target ? :up : :down
  @migrations = get_migrations
end

Public Instance Methods

is_current?() click to toggle source

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

# File lib/sequel/extensions/migration.rb, line 550
def is_current?
  current_migration_version == target
end
run() click to toggle source

Apply all migrations on the database

# File lib/sequel/extensions/migration.rb, line 555
def run
  migrations.zip(version_numbers).each do |m, v|
    t = Time.now
    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', Time.now - t)} seconds")
  end
  
  target
end

Private Instance Methods

current_migration_version() click to toggle source

Gets the current migration version stored in the database. If no version number is stored, 0 is returned.

# File lib/sequel/extensions/migration.rb, line 573
def current_migration_version
  ds.get(column) || 0
end
get_migration_files() click to toggle source

Returns any found migration files in the supplied directory.

# File lib/sequel/extensions/migration.rb, line 578
def get_migration_files
  files = []
  Dir.new(directory).each do |file|
    next unless MIGRATION_FILE_PATTERN.match(file)
    version = migration_version_from_file(file)
    if version >= 20000101
      raise Migrator::Error, "Migration number too large, must use TimestampMigrator: #{file}"
    end
    raise(Error, "Duplicate migration version: #{version}") if files[version]
    files[version] = File.join(directory, file)
  end
  1.upto(files.length - 1){|i| raise(Error, "Missing migration version: #{i}") unless files[i]} unless @allow_missing_migration_files
  files
end
get_migrations() click to toggle source

Returns a list of migration classes filtered for the migration range and ordered according to the migration direction.

# File lib/sequel/extensions/migration.rb, line 595
def get_migrations
  remove_migration_classes

  # load migration files
  version_numbers.each{|n| load_migration_file(files[n])}
  
  # get migration classes
  Migration.descendants
end
latest_migration_version() click to toggle source

Returns the latest version available in the specified directory.

# File lib/sequel/extensions/migration.rb, line 606
def latest_migration_version
  l = files.last
  l ? migration_version_from_file(File.basename(l)) : nil
end
schema_dataset() click to toggle source

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

# File lib/sequel/extensions/migration.rb, line 613
def schema_dataset
  c = column
  ds = db.from(table)
  db.create_table?(table){Integer c, :default=>0, :null=>false}
  unless ds.columns.include?(c)
    db.alter_table(table){add_column c, Integer, :default=>0, :null=>false}
  end
  ds.insert(c=>0) if ds.empty?
  raise(Error, "More than 1 row in migrator table") if ds.count > 1
  ds
end
set_migration_version(version) click to toggle source

Sets the current migration version stored in the database.

# File lib/sequel/extensions/migration.rb, line 626
def set_migration_version(version)
  ds.update(column=>version)
end
up?() click to toggle source

Whether or not this is an up migration

# File lib/sequel/extensions/migration.rb, line 631
def up?
  direction == :up
end
version_numbers() click to toggle source

An array of numbers corresponding to the migrations, so that each number in the array is the migration version that will be in affect after the migration is run.

# File lib/sequel/extensions/migration.rb, line 638
def version_numbers
  @version_numbers ||= begin
    versions = files.
      compact.
      map{|f| migration_version_from_file(File.basename(f))}.
      select{|v| up? ? (v > current && v <= target) : (v <= current && v > target)}.
      sort
    versions.reverse! unless up?
    versions
  end
end