class Reactor::Migrator

Migrator is responsible for running migrations.

You should not use this class directly! Use rake cm:migrate instead.

Migrating to a specific version is possible by specifing VERSION environment variable: rake cm:migrate VERSION=0 Depending on your current version migrations will be run up (target version > current version) or down (target version < current version)

MIND THE FACT, that you land at the version nearest to target_version (possibly target version itself)

Public Class Methods

new(migrations_path, target_version=nil) click to toggle source

Constructor takes two parameters migrations_path (relative path of migration files) and target_version (an integer or nil).

Used by a rake task.

# File lib/reactor/tools/migrator.rb, line 79
def initialize(migrations_path, target_version=nil)
  @migrations_path = migrations_path
  @target_version = target_version.to_i unless target_version.nil?
  @target_version = 99999999999999 if target_version.nil?
  @versioner = Versioner.instance
end

Public Instance Methods

applied?(version) click to toggle source
# File lib/reactor/tools/migrator.rb, line 114
def applied?(version)
  @versioner.applied?(version)
end
current_version() click to toggle source
# File lib/reactor/tools/migrator.rb, line 118
def current_version
  @versioner.current_version
end
down() click to toggle source
# File lib/reactor/tools/migrator.rb, line 100
def down
  rem_migrations = migrations.reject do |version, name, file|
    version.to_i <= @target_version.to_i or not applied?(version)
  end
  run(rem_migrations.reverse, :down)
end
migrate() click to toggle source

Runs the migrations in proper direction (up or down) Ouputs current version when done

# File lib/reactor/tools/migrator.rb, line 88
def migrate
  return up if @target_version.to_i > current_version.to_i
  return down
end
migrations() click to toggle source
# File lib/reactor/tools/migrator.rb, line 107
def migrations
  files = Dir["#{@migrations_path}/[0-9]*_*.rb"].sort.collect do |file|
    version, name = file.scan(/([0-9]+)_([_a-z0-9]*).rb/).first
    [version, name, file]
  end
end
run(rem_migrations, direction) click to toggle source
# File lib/reactor/tools/migrator.rb, line 122
def run(rem_migrations, direction)
  begin
    rem_migrations.each do |version, name, file|
      migration = MigrationProxy.new(@versioner, name.camelize, version, direction, file)
      puts "Migrating #{direction.to_s}: #{migration.name} (#{migration.filename})"
      migration.load_migration and migration.run or raise "Migrating #{direction.to_s}: #{migration.name} (#{migration.filename}) failed"
    end
  ensure
    puts "At version: " + @versioner.current_version.to_s
    puts "WARNING: Could not store applied migrations!" if not @versioner.store
  end
end
up() click to toggle source
# File lib/reactor/tools/migrator.rb, line 93
def up
  rem_migrations = migrations.reject do |version, name, file|
    version.to_i > @target_version.to_i or applied?(version)
  end
  run(rem_migrations, :up)
end