Class: Kafo::Migrations

Inherits:
Object
  • Object
show all
Defined in:
lib/kafo/migrations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#migrationsObject (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

#appliedObject



16
17
18
# File 'lib/kafo/migrations.rb', line 16

def applied
  @applied ||= load_applied
end

#load_migrationsObject



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_appliedObject



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