data_migrator 1.8 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -23,7 +23,7 @@ module RussellEdge
23
23
  seen = Hash.new false
24
24
 
25
25
  migrations = files.map do |file|
26
- version, name = file.scan(/([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?.rb/).first
26
+ version, name, scope = file.scan(/([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?.rb/).first
27
27
 
28
28
  raise ActiveRecord::IllegalMigrationNameError.new(file) unless version
29
29
  version = version.to_i
@@ -34,14 +34,14 @@ module RussellEdge
34
34
 
35
35
  seen[version] = seen[name] = true
36
36
 
37
- {:name => File.basename(file), :filename => file, :version => version}
37
+ {:name => name, :filename => file, :version => version, :scope => scope}
38
38
  end
39
39
 
40
- migrations.sort{|h| h[:version]}
40
+ migrations.sort_by{|h| h[:version]}
41
41
  end
42
42
 
43
- def next_migration_number
44
- Time.now.utc.strftime("%Y%m%d%H%M%S")
43
+ def next_migration_number(number)
44
+ [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % number].max
45
45
  end
46
46
 
47
47
  def initialize_data_migrations_table
@@ -58,24 +58,24 @@ module RussellEdge
58
58
  destination_migrations = migrations(destination)
59
59
  last = destination_migrations.last
60
60
  sources.each do |scope, path|
61
- source_migrations = migrations(path)
62
-
63
- source_migrations.each do |migration|
64
-
61
+ migrations(path).each do |migration|
65
62
  source = File.read(migration[:filename])
66
63
  source = "# This migration comes from #{scope} (originally #{migration[:version]})\n#{source}"
67
-
64
+
68
65
  if duplicate = destination_migrations.detect { |m| m[:name] == migration[:name] }
69
- if options[:on_skip] && duplicate.scope != scope.to_s
70
- options[:on_skip].call(scope, migration)
66
+ if options[:refresh] && duplicate[:scope] == scope.to_s
67
+ Dir.glob(File.join(destination,"*_#{migration[:name].underscore}.#{scope.to_s}.rb")).each { |f| puts "Removing old migration #{migration[:name]}"; File.delete(f) }
68
+ elsif options[:on_skip] && duplicate[:scope] != scope.to_s
69
+ options[:on_skip].call(scope, migration)
71
70
  end
72
- next
71
+ next unless options[:refresh]
73
72
  end
74
-
75
- migration[:version] = next_migration_number().to_i
76
- new_path = File.join(destination, "#{File.basename(migration[:filename], '.*')}.#{scope}.rb")
73
+
74
+ migration[:version] = next_migration_number(last ? last[:version] + 1 : 0).to_i unless options[:preserve_timestamp]
75
+ new_path = File.join(destination, "#{migration[:version]}_#{migration[:name].underscore}.#{scope}.rb")
77
76
  old_path, migration[:filename] = migration[:filename], new_path
78
-
77
+ last = migration
78
+
79
79
  File.open(migration[:filename], "w") { |f| f.write source }
80
80
  copied << migration
81
81
  options[:on_copy].call(scope, migration, old_path) if options[:on_copy]
@@ -0,0 +1,9 @@
1
+ module DataMigrator
2
+ module VERSION #:nodoc:
3
+ MAJOR = 2
4
+ MINOR = 0
5
+ TINY = 0
6
+
7
+ STRING = [MAJOR, MINOR, TINY].compact.join('.')
8
+ end
9
+ end
@@ -2,6 +2,6 @@ class DataMigrationGenerator < Rails::Generators::NamedBase
2
2
  source_root File.expand_path('../templates', __FILE__)
3
3
 
4
4
  def generate_layout
5
- template "migration_template.rb", "db/data_migrations/#{RussellEdge::DataMigrator.next_migration_number}_#{file_name}.rb"
5
+ template "migration_template.rb", "db/data_migrations/#{RussellEdge::DataMigrator.next_migration_number(0)}_#{file_name}.rb"
6
6
  end
7
7
  end
@@ -40,6 +40,10 @@ namespace :railties do
40
40
  # desc "Copies missing data_migrations from Railties (e.g. plugins, engines). You can specify Railties to use with FROM=railtie1,railtie2"
41
41
  task :data_migrations => :environment do
42
42
  to_load = ENV['FROM'].blank? ? :all : ENV['FROM'].split(",").map {|n| n.strip }
43
+ #added to allow developer to perserve timestamps
44
+ preserve_timestamp = ENV['PRESERVE_TIMESTAMPS'].blank? ? false : (ENV['PRESERVE_TIMESTAMPS'].to_s.downcase == "true")
45
+ #refresh will replace migrations from engines
46
+ refresh = ENV['REFRESH'].blank? ? false : (ENV['REFRESH'].to_s.downcase == "true")
43
47
  railties = ActiveSupport::OrderedHash.new
44
48
  Rails.application.railties.all do |railtie|
45
49
  next unless to_load == :all || to_load.include?(railtie.railtie_name)
@@ -57,9 +61,14 @@ namespace :railties do
57
61
  puts "Copied data_migration #{migration[:name]} from #{name}"
58
62
  end
59
63
 
60
- RussellEdge::DataMigrator.copy(RussellEdge::DataMigrator.migrations_path, railties, :on_skip => on_skip, :on_copy => on_copy)
61
- end
62
- end
63
- end
64
+ RussellEdge::DataMigrator.copy(RussellEdge::DataMigrator.migrations_path, railties,
65
+ :on_skip => on_skip,
66
+ :on_copy => on_copy,
67
+ :preserve_timestamp => preserve_timestamp,
68
+ :refresh => refresh)
69
+ end #data_migrations
70
+
71
+ end #install
72
+ end #railties
64
73
 
65
74
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_migrator
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.8'
4
+ version: 2.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,19 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-07 00:00:00.000000000 Z
12
+ date: 2013-01-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70230211897860 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ! '>='
19
+ - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '2.3'
21
+ version: 3.2.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70230211897860
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.0
25
30
  description: Allows you to create data migrations that can be run up and down to insert
26
31
  data into the database.
27
32
  email: russellfholmes@gmail.com
@@ -30,6 +35,7 @@ extensions: []
30
35
  extra_rdoc_files: []
31
36
  files:
32
37
  - db/migrate/20100819181805_create_data_migrations_table.rb
38
+ - lib/data_migrator/version.rb
33
39
  - lib/data_migrator.rb
34
40
  - lib/extensions/railties/engine/configuration.rb
35
41
  - lib/extensions/railties/engine.rb
@@ -61,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
67
  version: '0'
62
68
  requirements: []
63
69
  rubyforge_project:
64
- rubygems_version: 1.8.11
70
+ rubygems_version: 1.8.24
65
71
  signing_key:
66
72
  specification_version: 3
67
73
  summary: Creates Data Migrations for data similar to schema migrations.