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

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 515
def initialize(db, directory, opts=OPTS)
  super
  @current = opts[:current] || current_migration_version
  raise(Error, "No current version available") unless current

  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 version available, probably because no migration files found or filenames don't follow the migration filename convention") unless target

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

  @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 543
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 548
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

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 566
def current_migration_version
  ds.get(column) || 0
end
default_schema_column() click to toggle source

The default column storing schema version.

# File lib/sequel/extensions/migration.rb, line 571
def default_schema_column
  :version
end
default_schema_table() click to toggle source

The default table storing schema version.

# File lib/sequel/extensions/migration.rb, line 576
def default_schema_table
  :schema_info
end
get_migration_files() click to toggle source

Returns any found migration files in the supplied directory.

# File lib/sequel/extensions/migration.rb, line 581
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 598
def get_migrations
  version_numbers.map{|n| load_migration_file(files[n])}
end
latest_migration_version() click to toggle source

Returns the latest version available in the specified directory.

# File lib/sequel/extensions/migration.rb, line 603
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 610
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 623
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 628
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 635
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