Class: Reactor::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/reactor/tools/migrator.rb

Overview

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)

Instance Method Summary collapse

Constructor Details

#initialize(migrations_path, target_version = nil) ⇒ Migrator

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

Used by a rake task.



80
81
82
83
84
85
# File 'lib/reactor/tools/migrator.rb', line 80

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

Instance Method Details

#applied?(version) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/reactor/tools/migrator.rb', line 115

def applied?(version)
  @versioner.applied?(version)
end

#current_versionObject



119
120
121
# File 'lib/reactor/tools/migrator.rb', line 119

def current_version
  @versioner.current_version
end

#downObject



101
102
103
104
105
106
# File 'lib/reactor/tools/migrator.rb', line 101

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

#migrateObject

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



89
90
91
92
# File 'lib/reactor/tools/migrator.rb', line 89

def migrate
  return up if @target_version.to_i > current_version.to_i
  return down
end

#migrationsObject



108
109
110
111
112
113
# File 'lib/reactor/tools/migrator.rb', line 108

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



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/reactor/tools/migrator.rb', line 123

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

#upObject



94
95
96
97
98
99
# File 'lib/reactor/tools/migrator.rb', line 94

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