Class: Reactor::Migrator
- Inherits:
-
Object
- Object
- Reactor::Migrator
- 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
- #applied?(version) ⇒ Boolean
- #current_version ⇒ Object
- #down ⇒ Object
-
#initialize(migrations_path, target_version = nil) ⇒ Migrator
constructor
Constructor takes two parameters migrations_path (relative path of migration files) and target_version (an integer or nil).
-
#migrate ⇒ Object
Runs the migrations in proper direction (up or down) Ouputs current version when done.
- #migrations ⇒ Object
- #run(rem_migrations, direction) ⇒ Object
- #up ⇒ Object
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.
74 75 76 77 78 79 |
# File 'lib/reactor/tools/migrator.rb', line 74 def initialize(migrations_path, target_version = nil) @migrations_path = migrations_path @target_version = target_version.to_i unless target_version.nil? @target_version = 99_999_999_999_999 if target_version.nil? @versioner = Versioner.instance end |
Instance Method Details
#applied?(version) ⇒ Boolean
110 111 112 |
# File 'lib/reactor/tools/migrator.rb', line 110 def applied?(version) @versioner.applied?(version) end |
#current_version ⇒ Object
114 115 116 |
# File 'lib/reactor/tools/migrator.rb', line 114 def current_version @versioner.current_version end |
#down ⇒ Object
96 97 98 99 100 101 |
# File 'lib/reactor/tools/migrator.rb', line 96 def down rem_migrations = migrations.reject do |version, _name, _file| version.to_i <= @target_version.to_i or !applied?(version) end run(rem_migrations.reverse, :down) end |
#migrate ⇒ Object
Runs the migrations in proper direction (up or down) Ouputs current version when done
83 84 85 86 87 |
# File 'lib/reactor/tools/migrator.rb', line 83 def migrate return up if @target_version.to_i > current_version.to_i down end |
#migrations ⇒ Object
103 104 105 106 107 108 |
# File 'lib/reactor/tools/migrator.rb', line 103 def migrations 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
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/reactor/tools/migrator.rb', line 118 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}: #{migration.name} (#{migration.filename})" migration.load_migration and migration.run or raise "Migrating #{direction}: #{migration.name} (#{migration.filename}) failed" end ensure puts "At version: " + @versioner.current_version.to_s puts "WARNING: Could not store applied migrations!" unless @versioner.store end end |
#up ⇒ Object
89 90 91 92 93 94 |
# File 'lib/reactor/tools/migrator.rb', line 89 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 |