Class: Kafo::Migrations
- Inherits:
-
Object
- Object
- Kafo::Migrations
- Defined in:
- lib/kafo/migrations.rb
Instance Attribute Summary collapse
-
#migrations ⇒ Object
readonly
Returns the value of attribute migrations.
Instance Method Summary collapse
- #add_migration(name, &block) ⇒ Object
- #applied ⇒ Object
-
#initialize(migrations_dir) ⇒ Migrations
constructor
A new instance of Migrations.
- #load_migrations ⇒ Object
- #run(scenario, answers) ⇒ Object
- #store_applied ⇒ Object
Constructor Details
#initialize(migrations_dir) ⇒ Migrations
Returns a new instance of Migrations.
9 10 11 12 13 14 |
# File 'lib/kafo/migrations.rb', line 9 def initialize(migrations_dir) @migrations_dir = migrations_dir @migrations = {} @applied_file = File.join(@migrations_dir, '.applied') load_migrations end |
Instance Attribute Details
#migrations ⇒ Object (readonly)
Returns the value of attribute migrations.
7 8 9 |
# File 'lib/kafo/migrations.rb', line 7 def migrations @migrations end |
Instance Method Details
#add_migration(name, &block) ⇒ Object
30 31 32 |
# File 'lib/kafo/migrations.rb', line 30 def add_migration(name, &block) @migrations[name] = block end |
#applied ⇒ Object
16 17 18 |
# File 'lib/kafo/migrations.rb', line 16 def applied @applied ||= load_applied end |
#load_migrations ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/kafo/migrations.rb', line 20 def load_migrations Dir.glob(@migrations_dir + "/*.rb").each do |file| next if applied.include?(File.basename(file)) KafoConfigure.logger.debug "Loading migration #{file}" migration = File.read(file) migration_block = proc { instance_eval(migration, file, 1) } add_migration(file, &migration_block) end end |
#run(scenario, answers) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/kafo/migrations.rb', line 34 def run(scenario, answers) @migrations.keys.sort.each do |name| KafoConfigure.logger.debug "Executing migration #{name}" migration = @migrations[name] scenario, answers = Kafo::MigrationContext.execute(scenario, answers, &migration) applied << File.basename(name.to_s) end return scenario, answers end |
#store_applied ⇒ Object
44 45 46 |
# File 'lib/kafo/migrations.rb', line 44 def store_applied File.open(@applied_file, 'w') { |f| f.write(applied.to_yaml) } end |