Class: Migu::Migrator
- Inherits:
-
Object
- Object
- Migu::Migrator
- Defined in:
- lib/migu/migrator.rb
Instance Attribute Summary collapse
-
#migrations ⇒ Object
readonly
Returns the value of attribute migrations.
Instance Method Summary collapse
-
#initialize(migrations = Migu::Migration.subclasses) ⇒ Migrator
constructor
A new instance of Migrator.
- #migrate ⇒ Object
- #rollback ⇒ Object
- #state ⇒ Object
Constructor Details
#initialize(migrations = Migu::Migration.subclasses) ⇒ Migrator
5 6 7 |
# File 'lib/migu/migrator.rb', line 5 def initialize(migrations = Migu::Migration.subclasses) @migrations = migrations.sort { |a, b| a.time <=> b.time } end |
Instance Attribute Details
#migrations ⇒ Object (readonly)
Returns the value of attribute migrations.
3 4 5 |
# File 'lib/migu/migrator.rb', line 3 def migrations @migrations end |
Instance Method Details
#migrate ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/migu/migrator.rb', line 13 def migrate Migu.configuration.before_hook.call migrations.each do |migration| unless state.find(migration.name) puts "==== Migrate #{migration.name} ====" migration.new.up state.insert(migration.name, migration.time.to_s) end end Migu.configuration.after_hook.call end |
#rollback ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/migu/migrator.rb', line 25 def rollback Migu.configuration.before_hook.call if state.migrated.empty? puts "==== No migrations to rollback ====" return end migration = migrations.find { |migration| migration.name == state.migrated.last[1] } puts "==== Rollback #{migration.name} ====" migration.new.down state.delete(migration.name) Migu.configuration.after_hook.call end |
#state ⇒ Object
9 10 11 |
# File 'lib/migu/migrator.rb', line 9 def state Migu::State.new end |