Class: Reactor::MigrationProxy

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

Overview

Class responsible for running a single migration, a helper for Migrator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(versioner, name, version, direction, filename) ⇒ MigrationProxy

Returns a new instance of MigrationProxy.



6
7
8
9
10
11
12
# File 'lib/reactor/tools/migrator.rb', line 6

def initialize(versioner, name, version, direction, filename)
  @versioner = versioner
  @name = name
  @version = version
  @filename = filename
  @direction = direction
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



55
56
57
# File 'lib/reactor/tools/migrator.rb', line 55

def filename
  @filename
end

#nameObject (readonly)

Returns the value of attribute name.



55
56
57
# File 'lib/reactor/tools/migrator.rb', line 55

def name
  @name
end

#versionObject (readonly)

Returns the value of attribute version.



55
56
57
# File 'lib/reactor/tools/migrator.rb', line 55

def version
  @version
end

Instance Method Details

#class_nameObject



51
52
53
# File 'lib/reactor/tools/migrator.rb', line 51

def class_name
  Kernel.const_get(@name)
end

#downObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/reactor/tools/migrator.rb', line 40

def down
  result = class_name.send(:down) and @versioner.remove(@version)
  if result
    class_name.contained.each do |version|
      puts "#{class_name} contains migration #{version}"
      @versioner.remove(version)
    end
  end
  result
end

#load_migrationObject



14
15
16
# File 'lib/reactor/tools/migrator.rb', line 14

def load_migration
  load @filename
end

#runObject



18
19
20
21
22
# File 'lib/reactor/tools/migrator.rb', line 18

def run
  return down if @direction.to_sym == :down

  up
end

#upObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/reactor/tools/migrator.rb', line 24

def up
  if @versioner.applied?(@version)
    puts "Migrating up: #{@name} (#{@filename}) already applied, skipping"
    true
  else
    result = class_name.send(:up) and @versioner.add(@version)
    if result
      class_name.contained.each do |version|
        puts "#{class_name} contains migration #{version}"
        # @versioner.add(version) # not neccesary!
      end
    end
    result
  end
end