Class: Germinator::Seeder

Inherits:
Base
  • Object
show all
Defined in:
lib/germinator/seeder.rb

Overview

A class to manage the seeding process.

Instance Method Summary collapse

Instance Method Details

#germinate(p = {}) ⇒ Object

Germinates the database by finding unseeded files in the germinate/ directory and attempting to execute their germinate method.

Parameters:

step: => A maximum number of seeds to germinate. nil or 0 will execute all unseeded files in the germinate directory. (default: nil)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/germinator/seeder.rb', line 19

def germinate p={}
  Base::confirm_database_table

  step = p.has_key?(:step) ? p[:step].to_i : nil
  step = nil if step==0

  i = 0
  _seeds = unseeded

  _seeds.keys.sort.each do |seed_name|
    germinate_by_seed_name seed_name
    i += 1
    break if step and i >= step
  end
end

#germinate_by_name(name) ⇒ Object

Executes a seed file’s germinate method using the name of the seed file. The germinate method is only executed if it has not been executed previously.



39
40
41
42
43
44
45
46
# File 'lib/germinator/seeder.rb', line 39

def germinate_by_name name
  Base::confirm_database_table
  include_seeds
  _seeds = seeds.select{ |seed_name, seed| seed.name == name }
  return if _seeds.size == 0
  seed_name, seed = _seeds.first
  germinate_by_seed_name seed_name
end

#germinate_by_seed_name(seed_name) ⇒ Object

Executes a seed file’s germinate method using the seed_name (version_name) of the seed file. The germinate method is only executed if it has not been executed previously.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/germinator/seeder.rb', line 65

def germinate_by_seed_name seed_name
  Base::confirm_database_table
  include_seeds
  _seeds = unseeded
  return unless _seeds.has_key?(seed_name)

  seed = _seeds[seed_name]

  puts "== #{seed}: GERMINATE ==========", 0
  begin

    seed_object = get_seed_object seed
    output_config seed_object
    seed_object.migrate :up

    add_seeded_version seed.version, seed.name, seed_object.response, seed_object.message, seed_object.config.to_hash
  rescue Germinator::Errors::InvalidSeedEnvironment => e
    puts e.message
    add_seeded_version seed.version, seed.name, seed_object.response, seed_object.message, seed_object.config.to_hash
  rescue Germinator::Errors::InvalidSeedModel => e
    puts e.message
    return if seed_object.config.stop_on_invalid_model
    add_seeded_version seed.version, seed.name, seed_object.response, seed_object.message, seed_object.config.to_hash
    puts "Moving on..."
  rescue Exception => e
    puts ""
    puts_error e
    puts ""
    puts "There was an error while executing the seeds.  Germination stopped!", 0
    puts ""
    raise e if !seed_object || seed_object.config.stop_on_error
    add_seeded_version seed.version, seed.name, seed_object.response, seed_object.message, seed_object.config.to_hash
  ensure
    puts "== #{seed}: END       ==========", 0
  end
end

#germinate_by_version(version) ⇒ Object

Executes a seed file’s germinate method using the version of the seed file. The germinate method is only executed if it has not been executed previously.



52
53
54
55
56
57
# File 'lib/germinator/seeder.rb', line 52

def germinate_by_version version
  _seeds = seeds.select{ |seed_name, seed| seed.version == version }
  return if _seeds.size == 0
  seed_name, seed = _seeds.first
  germinate_by_seed_name seed_name
end

#reseed(p = {}) ⇒ Object

Shrivels and then germinates the database by finding seeded files in the germinate/ directory and attempting to execute their shrivel and germinate methods. The germinate methods get called in order after all of the shrivel methods have been call.

Parameters:

step: => A maximum number of seeds to reseed. nil or 0 will execute all unseeded files in the germinate directory. (default: nil)



205
206
207
208
209
210
211
212
213
214
# File 'lib/germinator/seeder.rb', line 205

def reseed p={}
  step = p.has_key?(:step) ? p[:step].to_i : 1
  step = nil if step==0

  puts "Reseeding the database...", 0
  puts ""

  shrivel step: step
  germinate step: step
end

#shrivel(p = {}) ⇒ Object

Shrivels the database by finding seeded files in the germinate/ directory and attempting to execute their shrivel method.

Parameters:

step: => A maximum number of seeds to shrivel. nil or 0 will execute all unseeded files in the germinate directory. (default: 1)



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/germinator/seeder.rb', line 110

def shrivel p={}
  Base::confirm_database_table

  step = p.has_key?(:step) ? p[:step].to_i : 1
  step = nil if step==0

  i = 0
  _seeds = seeded

  _seeds.keys.sort.reverse.each do |seed_name|
    shrivel_by_seed_name seed_name
    i += 1
    break if step and i >= step
  end
end

#shrivel_by_name(name) ⇒ Object

Executes a seed file’s germinate method using the name of the seed file. The germinate method is only executed if it has not been executed previously.



131
132
133
134
135
136
# File 'lib/germinator/seeder.rb', line 131

def shrivel_by_name name
  _seeds = seeds.select{ |seed_name, seed| seed.name == name }
  return if _seeds.size == 0
  seed_name, seed = _seeds.first
  shrivel_by_seed_name seed_name
end

#shrivel_by_seed_name(seed_name) ⇒ Object

Executes a seed file’s germinate method using the seed_name (version_name) of the seed file. The germinate method is only executed if it has not been executed previously.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/germinator/seeder.rb', line 156

def shrivel_by_seed_name seed_name
  Base::confirm_database_table
  include_seeds

  _seeds = seeded
  return unless _seeds.has_key?(seed_name)

  seed = _seeds[seed_name]

  begin
    puts "== #{seed}: SHRIVEL ==========", 0
    seed_object = get_seed_object seed
    output_config seed_object
    seed_object.migrate :down
    puts "== #{seed}: END     ==========", 0

    remove_seeded_version seed.version
  rescue Germinator::Errors::InvalidSeedEnvironment => e
    puts e.message
    remove_seeded_version seed.version
  rescue Germinator::Errors::InvalidSeedModel => e
    puts e.message
    return if seed_object && seed_object.config.stop_on_invalid_model
    remove_seeded_version seed.version
    puts "Moving on..."
  rescue Exception => e
    puts ""
    puts_error e
    puts ""
    puts "There was an error while executing the seeds.", 0
    puts ""
    raise e if !seed_object || seed_object.config.stop_on_error
    puts "Moving on..."
    remove_seeded_version seed.version
  ensure
    puts "== #{seed}: END       ==========", 0
  end
end

#shrivel_by_version(version) ⇒ Object

Executes a seed file’s germinate method using the version of the seed file. The germinate method is only executed if it has not been executed previously.



143
144
145
146
147
148
# File 'lib/germinator/seeder.rb', line 143

def shrivel_by_version version
  _seeds = seeds.select{ |seed_name, seed| seed.version == version }
  return if _seeds.size == 0
  seed_name, seed = _seeds.first
  shrivel_by_seed_name seed_name
end