data_migrator 1.3 → 1.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/data_migrator.rb CHANGED
@@ -6,304 +6,282 @@ class DataMigrationTask < Rails::Railtie
6
6
  end
7
7
  end
8
8
 
9
- class DataMigrator
10
- DEFAULT_MIGRATIONS_PATH = "#{Rails.root}/db/data_migrations"
11
- REMOVE_FILES_REGEX = /^\./
12
-
13
- def self.migrate(passed_location=nil, passed_version=nil)
14
- self.setup
9
+ module RussellEdge
10
+ class DataMigrator
11
+ REMOVE_FILES_REGEX = /^\./
12
+
13
+ def initialize(migrations_path=nil)
14
+ @default_migrations_path = migrations_path || "#{Rails.root}/db/data_migrations"
15
+ end
16
+
17
+ def migrate(passed_location=nil, passed_version=nil)
18
+ setup
15
19
 
16
- location = passed_location.nil? ? DEFAULT_MIGRATIONS_PATH : passed_location
17
- @@current_version = get_current_version
18
-
19
- if passed_version.nil? || @@current_version.nil?
20
- self.run_all_non_migrated(passed_version)
21
- elsif passed_version < @@current_version
22
- self.handle_lower_passed_version(passed_version)
23
- elsif passed_version > @@current_version
24
- self.handle_higher_passed_version(passed_version)
20
+ location = passed_location.nil? ? @default_migrations_path : passed_location
21
+ @@current_version = get_current_version
22
+
23
+ if passed_version.nil? || @@current_version.nil?
24
+ run_all_non_migrated(passed_version)
25
+ elsif passed_version < @@current_version
26
+ handle_lower_passed_version(passed_version)
27
+ elsif passed_version > @@current_version
28
+ handle_higher_passed_version(passed_version)
29
+ end
25
30
  end
26
- end
27
31
 
28
- def self.pending_migrations
29
- versions = []
30
- files = self.get_all_files
31
- files.each do |file|
32
- filename, version, klass_name = self.seperate_file_parts(file)
33
- unless version_has_been_migrated?(version)
34
- versions << filename
32
+ def pending_migrations
33
+ versions = []
34
+ files = get_all_files
35
+ files.each do |file|
36
+ filename, version, klass_name = seperate_file_parts(file)
37
+ versions << filename unless version_has_been_migrated?(version)
35
38
  end
36
- end
37
39
 
38
- versions
39
- end
40
+ versions
41
+ end
40
42
 
41
- def self.get_current_version
42
- result = ActiveRecord::Base.connection.select_all("select max(version) as current_version from data_migrations")
43
+ def get_current_version
44
+ result = ActiveRecord::Base.connection.select_all("select max(version) as current_version from data_migrations")
43
45
 
44
- current_version = result[0]['current_version'] unless result == -1
46
+ current_version = result[0]['current_version'] unless result == -1
45
47
 
46
- current_version = current_version.to_i unless current_version.nil?
48
+ current_version = current_version.to_i unless current_version.nil?
47
49
 
48
- current_version
49
- end
50
+ current_version
51
+ end
50
52
 
51
- def self.next_migration_number
52
- Time.now.utc.strftime("%Y%m%d%H%M%S")
53
- end
53
+ def next_migration_number
54
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
55
+ end
54
56
 
55
- def self.run_up(passed_version)
56
- self.setup
57
+ def run_up(passed_version)
58
+ setup
57
59
 
58
- raise "VERSION is required" unless passed_version
60
+ raise "VERSION is required" unless passed_version
59
61
 
60
- files = self.get_all_files
61
- found = false
62
+ files = get_all_files
63
+ found = false
62
64
 
63
- files.each do |file|
64
- filename, version, klass_name = self.seperate_file_parts(file)
65
- if passed_version == version
66
- found = true
67
- unless self.version_has_been_migrated?(version)
68
- self.handle_action(file, klass_name, version, :up)
69
- else
70
- puts "** Version #{passed_version} has already been migrated"
65
+ files.each do |file|
66
+ filename, version, klass_name = seperate_file_parts(file)
67
+ if passed_version == version
68
+ found = true
69
+ (version_has_been_migrated?(version)) ? (puts "** Version #{passed_version} has already been migrated") : handle_action(file, klass_name, version, :up)
71
70
  end
72
71
  end
73
- end
74
72
 
75
- unless found
76
- puts "** Version #{passed_version} not found"
77
- end
73
+ puts "** Version #{passed_version} not found" unless found
78
74
 
79
- end
75
+ end
80
76
 
81
- def self.run_down(passed_version)
82
- self.setup
77
+ def run_down(passed_version)
78
+ setup
83
79
 
84
- raise "VERSION is required" unless passed_version
80
+ raise "VERSION is required" unless passed_version
85
81
 
86
- files = self.get_all_files
87
- found = false
88
-
89
- files.each do |file|
90
- filename, version, klass_name = self.seperate_file_parts(file)
91
- if passed_version == version
92
- found = true
93
- if self.version_has_been_migrated?(version)
94
- self.handle_action(file, klass_name, version, :down)
95
- else
96
- puts "** Version #{passed_version} has not been migrated"
82
+ files = get_all_files
83
+ found = false
84
+
85
+ files.each do |file|
86
+ filename, version, klass_name = seperate_file_parts(file)
87
+ if passed_version == version
88
+ found = true
89
+ (version_has_been_migrated?(version)) ? (puts "** Version #{passed_version} has not been migrated") : handle_action(file, klass_name, version, :down)
97
90
  end
98
91
  end
99
- end
100
92
 
101
- unless found
102
- puts "** Version #{passed_version} not found"
103
- end
93
+ puts "** Version #{passed_version} not found" unless found
104
94
 
105
- end
95
+ end
106
96
 
107
- def self.prepare_migrations
108
- target = "#{Rails.root}/db/data_migrations/"
97
+ def prepare_migrations
98
+ target = "#{Rails.root}/db/data_migrations/"
109
99
 
110
- # first copy all app data_migrations away
111
- files = Dir["#{target}*.rb"]
100
+ # first copy all app data_migrations away
101
+ files = Dir["#{target}*.rb"]
112
102
 
113
- unless files.empty?
114
- FileUtils.mkdir_p "#{target}/ignore/app/"
115
- FileUtils.cp files, "#{target}/ignore/app/"
116
- puts "copied #{files.size} data_migrations to db/data_migrations/ignore/app"
117
- end
103
+ unless files.empty?
104
+ FileUtils.mkdir_p "#{target}/ignore/app/"
105
+ FileUtils.cp files, "#{target}/ignore/app/"
106
+ puts "copied #{files.size} data_migrations to db/data_migrations/ignore/app"
107
+ end
118
108
 
119
- dirs = Rails::Application::Railties.engines.map{|p| p.config.root.to_s}
120
- files = Dir["{#{dirs.join(',')}}/db/data_migrations/*.rb"]
109
+ dirs = Rails::Application::Railties.engines.map{|p| p.config.root.to_s}
110
+ files = Dir["{#{dirs.join(',')}}/db/data_migrations/*.rb"]
121
111
 
122
- unless files.empty?
123
- FileUtils.mkdir_p target
124
- FileUtils.cp files, target
125
- puts "copied #{files.size} migrations to db/data_migrations"
112
+ unless files.empty?
113
+ FileUtils.mkdir_p target
114
+ FileUtils.cp files, target
115
+ puts "copied #{files.size} migrations to db/data_migrations"
116
+ end
126
117
  end
127
- end
128
118
 
129
- def self.cleanup_migrations
130
- target = "#{Rails.root}/db/data_migrations/"
119
+ def cleanup_migrations
120
+ target = "#{Rails.root}/db/data_migrations/"
131
121
 
132
- files = Dir["#{target}*.rb"]
133
- unless files.empty?
134
- FileUtils.rm files
135
- puts "removed #{files.size} data_migrations from db/data_migrations"
136
- end
137
- files = Dir["#{target}/ignore/app/*.rb"]
138
- unless files.empty?
139
- FileUtils.cp files, target
140
- puts "copied #{files.size} data_migrations back to db/data_migrations"
122
+ files = Dir["#{target}*.rb"]
123
+ unless files.empty?
124
+ FileUtils.rm files
125
+ puts "removed #{files.size} data_migrations from db/data_migrations"
126
+ end
127
+ files = Dir["#{target}/ignore/app/*.rb"]
128
+ unless files.empty?
129
+ FileUtils.cp files, target
130
+ puts "copied #{files.size} data_migrations back to db/data_migrations"
131
+ end
132
+ FileUtils.rm_rf "#{target}/ignore/app"
141
133
  end
142
- FileUtils.rm_rf "#{target}/ignore/app"
143
- end
144
134
 
145
- private
135
+ private
146
136
 
147
- def self.setup
148
- unless self.data_migrations_table_exists?
149
- self.create_data_migrations_table
150
- end
151
-
152
- unless File.directory? DEFAULT_MIGRATIONS_PATH
153
- FileUtils.mkdir_p(DEFAULT_MIGRATIONS_PATH)
154
- #create ignore folder
155
- FileUtils.mkdir_p(DEFAULT_MIGRATIONS_PATH + '/ignore/')
137
+ def setup
138
+ create_data_migrations_table unless data_migrations_table_exists?
139
+
140
+ unless File.directory? @default_migrations_path
141
+ FileUtils.mkdir_p( @default_migrations_path)
142
+ #create ignore folder
143
+ FileUtils.mkdir_p(File.join(@default_migrations_path,'ignore/'))
144
+ end
156
145
  end
157
- end
158
146
 
159
- def self.handle_higher_passed_version(passed_version)
160
- files = self.get_all_files
161
-
162
- files.each do |file|
163
- filename, version, klass_name = self.seperate_file_parts(file)
164
- if version <= passed_version
165
- unless self.version_has_been_migrated?(version)
166
- self.handle_action(file, klass_name, version, :up)
147
+ def handle_higher_passed_version(passed_version)
148
+ files = get_all_files
149
+
150
+ files.each do |file|
151
+ filename, version, klass_name = seperate_file_parts(file)
152
+ if version <= passed_version
153
+ unless version_has_been_migrated?(version)
154
+ handle_action(file, klass_name, version, :up)
155
+ end
167
156
  end
168
157
  end
169
- end
170
158
 
171
- end
159
+ end
172
160
 
173
- def self.run_all_non_migrated(passed_version)
174
- files = self.get_all_files
161
+ def run_all_non_migrated(passed_version)
162
+ files = get_all_files
175
163
 
176
- files.each do |file|
177
- filename, version, klass_name = self.seperate_file_parts(file)
178
- if passed_version.nil? or version <= passed_version
179
- if !self.version_has_been_migrated?(version)
180
- self.handle_action(file, klass_name, version, :up)
164
+ files.each do |file|
165
+ filename, version, klass_name = seperate_file_parts(file)
166
+ if passed_version.nil? or version <= passed_version
167
+ if !version_has_been_migrated?(version)
168
+ handle_action(file, klass_name, version, :up)
169
+ end
181
170
  end
182
171
  end
183
172
  end
184
- end
185
173
 
186
- def self.handle_lower_passed_version(passed_version)
187
- files = self.get_all_files
174
+ def handle_lower_passed_version(passed_version)
175
+ files = get_all_files
188
176
 
189
- files.each do |file|
190
- filename, version, klass_name = self.seperate_file_parts(file)
191
- if version > passed_version
192
- if self.version_has_been_migrated?(version)
193
- self.handle_action(file, klass_name, version, :down)
177
+ files.each do |file|
178
+ filename, version, klass_name = seperate_file_parts(file)
179
+ if version > passed_version
180
+ handle_action(file, klass_name, version, :down) if version_has_been_migrated?(version)
194
181
  end
195
182
  end
196
- end
197
183
 
198
- end
184
+ end
199
185
 
200
- def self.handle_action(file, klass_name, version, action)
201
- require file
202
- klass = klass_name.camelize.constantize
203
- puts "=================Migrating #{klass.to_s} #{action.to_s.upcase}============"
204
- begin
205
- time = Benchmark.measure do
206
- ActiveRecord::Base.transaction do
207
- klass.send(action.to_s)
186
+ def handle_action(file, klass_name, version, action)
187
+ require file
188
+ klass = klass_name.camelize.constantize
189
+ puts "=================Migrating #{klass.to_s} #{action.to_s.upcase}============"
190
+ begin
191
+ time = Benchmark.measure do
192
+ ActiveRecord::Base.transaction do
193
+ klass.send(action.to_s)
194
+ end
208
195
  end
196
+ rescue Exception=>ex
197
+ cleanup_migrations
198
+ raise ex
209
199
  end
210
- rescue Exception=>ex
211
- self.cleanup_migrations
212
- raise ex
200
+ time_str = "(%.4fs)" % time.real
201
+ puts "================Finished #{klass.to_s} in #{time_str}=="
202
+ insert_migration_version(version)
213
203
  end
214
- time_str = "(%.4fs)" % time.real
215
- puts "================Finished #{klass.to_s} in #{time_str}=="
216
- self.insert_migration_version(version)
217
- end
218
204
 
219
- def self.insert_migration_version(version)
220
- ActiveRecord::Base.connection.execute("insert into data_migrations (version) values ('#{version}')")
221
- end
205
+ def insert_migration_version(version)
206
+ ActiveRecord::Base.connection.execute("insert into data_migrations (version) values ('#{version}')")
207
+ end
222
208
 
223
- def self.remove_migration_version(version)
224
- ActiveRecord::Base.connection.execute("delete from data_migrations where version = '#{version}'")
225
- end
209
+ def remove_migration_version(version)
210
+ ActiveRecord::Base.connection.execute("delete from data_migrations where version = '#{version}'")
211
+ end
226
212
 
227
- def self.version_has_been_migrated?(version)
228
- result = true
213
+ def version_has_been_migrated?(version)
214
+ result = true
229
215
 
230
- db_result = ActiveRecord::Base.connection.select_all("select count(*) as num_rows from data_migrations where version = '#{version}'")
216
+ db_result = ActiveRecord::Base.connection.select_all("select count(*) as num_rows from data_migrations where version = '#{version}'")
231
217
 
232
- num_rows = db_result[0]['num_rows'] unless db_result == -1
218
+ num_rows = db_result[0]['num_rows'] unless db_result == -1
233
219
 
234
- if num_rows.nil? || num_rows.to_i == 0
235
- result = false
236
- end
220
+ if num_rows.nil? || num_rows.to_i == 0
221
+ result = false
222
+ end
237
223
 
238
- result
239
- end
224
+ result
225
+ end
240
226
 
241
- def self.data_migrations_table_exists?
242
- table_names = ActiveRecord::Base.connection.tables
243
- table_names.include?('data_migrations')
244
- end
227
+ def data_migrations_table_exists?
228
+ table_names = ActiveRecord::Base.connection.tables
229
+ table_names.include?('data_migrations')
230
+ end
245
231
 
246
- def self.create_data_migrations_table
247
- puts "** data_migrations table missing creating now...."
248
- puts ActiveRecord::Migrator.run(:up, File.join(File.dirname(__FILE__),'../db/migrate/'), 20100819181805)
249
- puts "** done"
250
- end
232
+ def create_data_migrations_table
233
+ puts "** data_migrations table missing creating now...."
234
+ puts ActiveRecord::Migrator.run(:up, File.join(File.dirname(__FILE__),'../db/migrate/'), 20100819181805)
235
+ puts "** done"
236
+ end
251
237
 
252
- def self.seperate_file_parts(file)
253
- paths = file.split('/')
254
- filename = paths[paths.length - 1]
255
- version = filename.split('_')[0]
256
- klass_name = filename.gsub(/#{version}/, "").gsub(/.rb/, "")[1..filename.length]
238
+ def seperate_file_parts(file)
239
+ paths = file.split('/')
240
+ filename = paths[paths.length - 1]
241
+ version = filename.split('_')[0]
242
+ klass_name = filename.gsub(/#{version}/, "").gsub(/.rb/, "")[1..filename.length]
257
243
 
258
- return filename, version.to_i, klass_name
259
- end
244
+ return filename, version.to_i, klass_name
245
+ end
260
246
 
261
- def self.get_all_files
262
- files = []
247
+ def get_all_files
248
+ files = []
263
249
 
264
- if File.directory? DEFAULT_MIGRATIONS_PATH
250
+ if File.directory? @default_migrations_path
265
251
 
266
- files_or_directories = Dir.entries(DEFAULT_MIGRATIONS_PATH).map{|directory| directory}
252
+ files_or_directories = Dir.entries(@default_migrations_path).map{|directory| directory}
267
253
 
268
- files_or_directories.delete_if{|name| name =~ REMOVE_FILES_REGEX} #remove any file leading with . or ..
269
- files_or_directories.delete_if{|name| name == 'ignore'} #ignore the ignore folder
254
+ files_or_directories.delete_if{|name| name =~ REMOVE_FILES_REGEX} #remove any file leading with . or ..
255
+ files_or_directories.delete_if{|name| name == 'ignore'} #ignore the ignore folder
270
256
 
271
257
 
272
- files_or_directories.each do |file_or_directory|
273
- file_or_directory = DEFAULT_MIGRATIONS_PATH + "/" + file_or_directory
274
- files = self.get_files_in_directory(file_or_directory, files)
258
+ files_or_directories.each do |file_or_directory|
259
+ file_or_directory = @default_migrations_path + "/" + file_or_directory
260
+ files = get_files_in_directory(file_or_directory, files)
261
+ end
262
+
263
+ files.sort! {|x,y| File.basename(x) <=> File.basename(y)}
275
264
  end
276
265
 
277
- files.sort! {|x,y| self.get_file_name(x) <=> self.get_file_name(y)}
266
+ files
278
267
  end
279
268
 
280
- files
281
- end
282
-
283
- def self.get_files_in_directory(file_or_directory, files)
284
- unless file_or_directory =~ /\w\.rb/
285
- files_or_directories = Dir.entries(file_or_directory).map{|directory| directory}
269
+ def get_files_in_directory(file_or_directory, files)
270
+ unless file_or_directory =~ /\w\.rb/
271
+ files_or_directories = Dir.entries(file_or_directory).map{|directory| directory}
286
272
 
287
- files_or_directories.delete_if{|name| name =~ REMOVE_FILES_REGEX} #remove any file leading with . or ..
288
- files_or_directories.delete_if{|name| name == 'ignore'} #ignore the ignore folder
273
+ files_or_directories.delete_if{|name| name =~ REMOVE_FILES_REGEX} #remove any file leading with . or ..
274
+ files_or_directories.delete_if{|name| name == 'ignore'} #ignore the ignore folder
289
275
 
290
- files_or_directories.each do |_file_or_directory|
291
- _file_or_directory = file_or_directory + "/" + _file_or_directory
292
- files = self.get_files_in_directory(_file_or_directory, files)
276
+ files_or_directories.each do |_file_or_directory|
277
+ _file_or_directory = file_or_directory + "/" + _file_or_directory
278
+ files = get_files_in_directory(_file_or_directory, files)
279
+ end
280
+ else
281
+ files << file_or_directory
293
282
  end
294
- else
295
- files << file_or_directory
296
- end
297
-
298
- files
299
- end
300
-
301
- def self.get_file_name(dir)
302
- file_name = dir
303
283
 
304
- paths = dir.split('/')
305
- file_name = paths[paths.length - 1]
306
-
307
- file_name
284
+ files
285
+ end
308
286
  end
309
287
  end
@@ -1,53 +1,53 @@
1
1
  namespace :db do
2
2
  desc 'migrates data into database'
3
3
  task :migrate_data => :environment do
4
-
5
4
  passed_version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
6
- DataMigrator.prepare_migrations
7
- DataMigrator.migrate(nil, passed_version)
8
- DataMigrator.cleanup_migrations
5
+ data_migrator = RussellEdge::DataMigrator.new
6
+ data_migrator.prepare_migrations
7
+ data_migrator.migrate(nil, passed_version)
8
+ data_migrator.cleanup_migrations
9
9
 
10
- end#end task
10
+ end#end migrate_data task
11
11
 
12
12
  namespace :migrate_data do
13
13
  task :list_pending => :environment do
14
- DataMigrator.prepare_migrations
15
- pending_migrations = DataMigrator.pending_migrations
14
+ data_migrator = RussellEdge::DataMigrator.new
15
+ data_migrator.prepare_migrations
16
+ pending_migrations = data_migrator.pending_migrations
16
17
  puts "================Pending Data Migrations=========="
17
18
  puts pending_migrations
18
19
  puts "================================================="
19
- DataMigrator.cleanup_migrations
20
- end
20
+ data_migrator.cleanup_migrations
21
+ end#end list_pending task
21
22
 
22
23
  task :version => :environment do
23
- version = DataMigrator.get_current_version
24
+ data_migrator = RussellEdge::DataMigrator.new
25
+ version = data_migrator.get_current_version
24
26
 
25
- unless version.nil?
26
- puts "** Current version #{version}"
27
- else
28
- puts "** No migrations ran"
29
- end
30
- end
27
+ puts (version.nil?) ? "** No migrations ran" : "** Current version #{version}"
28
+ end#end version task
31
29
 
32
30
  task :up => :environment do
33
31
  passed_version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
34
32
  raise "VERSION is required" unless passed_version
33
+
34
+ data_migrator = RussellEdge::DataMigrator.new
35
+ data_migrator.prepare_migrations
36
+ data_migrator.run_up(passed_version)
37
+ data_migrator.cleanup_migrations
35
38
 
36
- DataMigrator.prepare_migrations
37
- DataMigrator.run_up(passed_version)
38
- DataMigrator.cleanup_migrations
39
-
40
- end#end task
39
+ end#end up task
41
40
 
42
41
  task :down => :environment do
43
42
  passed_version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
44
43
  raise "VERSION is required" unless passed_version
45
-
46
- DataMigrator.prepare_migrations
47
- DataMigrator.run_down(passed_version)
48
- DataMigrator.cleanup_migrations
44
+
45
+ data_migrator = RussellEdge::DataMigrator.new
46
+ data_migrator.prepare_migrations
47
+ data_migrator.run_down(passed_version)
48
+ data_migrator.cleanup_migrations
49
49
 
50
- end#end task
50
+ end#end down task
51
51
  end#end namespace
52
52
  end
53
53
 
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_migrator
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
- - 3
9
- version: "1.3"
8
+ - 4
9
+ version: "1.4"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Russell Holmes, Adam Hull
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-08-19 00:00:00 -04:00
17
+ date: 2011-08-25 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -48,7 +48,6 @@ files:
48
48
  - lib/generators/data_migration/templates/migration_template.rb
49
49
  - lib/generators/data_migration/USAGE
50
50
  - lib/tasks/data_migrator.rake
51
- - lib/version.rb
52
51
  - MIT-LICENSE
53
52
  - README.rdoc
54
53
  - test/data_migrator_test.rb
data/lib/version.rb DELETED
@@ -1,3 +0,0 @@
1
- module DataMigrator
2
- VERSION = '1.3' unless defined?(::DataMigrator::VERSION)
3
- end