Class: GroongaClientModel::Migrator

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

Defined Under Namespace

Classes: Definition

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(search_paths) ⇒ Migrator

Returns a new instance of Migrator.



34
35
36
37
38
39
40
41
# File 'lib/groonga_client_model/migrator.rb', line 34

def initialize(search_paths)
  @output = nil
  @search_paths = Array(search_paths)
  ensure_versions
  ensure_loaded_versions
  @current_version = @loaded_versions.last
  @target_version = nil
end

Instance Attribute Details

#current_versionObject (readonly)

Returns the value of attribute current_version.



32
33
34
# File 'lib/groonga_client_model/migrator.rb', line 32

def current_version
  @current_version
end

#outputObject

Returns the value of attribute output.



31
32
33
# File 'lib/groonga_client_model/migrator.rb', line 31

def output
  @output
end

Class Method Details

.default_search_pathObject



26
27
28
# File 'lib/groonga_client_model/migrator.rb', line 26

def default_search_path
  "db/groonga/migrate"
end

.next_migration_number(number) ⇒ Object



22
23
24
# File 'lib/groonga_client_model/migrator.rb', line 22

def next_migration_number(number)
  [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % number].max
end

Instance Method Details

#eachObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/groonga_client_model/migrator.rb', line 88

def each
  return to_enum(:each) unless block_given?

  current_version = @current_version || 0
  if forward?
    sorted_definitions.each do |definition|
      next if definition.version <= current_version
      next if @target_version and definition.version > @target_version
      yield(definition)
    end
  else
    sorted_definitions.reverse_each do |definition|
      next if definition.version > current_version
      next if @target_version and definition.version <= @target_version
      yield(definition)
    end
  end
end

#migrateObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/groonga_client_model/migrator.rb', line 62

def migrate
  is_forward = forward?
  each do |definition|
    Client.open do |client|
      migration = definition.create_migration(client)
      migration.output = @output
      report(definition) do
        if is_forward
          migration.up
          add_version(client, definition.version)
          @current_version = definition.version
        else
          migration.down
          delete_version(client, definition.version)
          previous_version_index = @versions.index(definition.version) - 1
          if previous_version_index < 0
            @current_version = nil
          else
            @current_version = @versions[previous_version_index]
          end
        end
      end
    end
  end
end

#step=(step) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/groonga_client_model/migrator.rb', line 47

def step=(step)
  if @current_version.nil?
    index = step - 1
  else
    index = @versions.index(@current_version)
    index += step
  end
  if index < 0
    version = 0
  else
    version = @versions[index]
  end
  self.target_version = version
end

#target_version=(version) ⇒ Object



43
44
45
# File 'lib/groonga_client_model/migrator.rb', line 43

def target_version=(version)
  @target_version = version
end