Class: RussellEdge::DataMigrator

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

Constant Summary collapse

REMOVE_FILES_REGEX =
/^\./

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(migrations_path = nil) ⇒ DataMigrator

Returns a new instance of DataMigrator.



91
92
93
# File 'lib/data_migrator.rb', line 91

def initialize(migrations_path=nil)
  @default_migrations_path = migrations_path || "#{Rails.root}/db/data_migrations"
end

Class Method Details

.copy(destination, sources, options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
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
87
# File 'lib/data_migrator.rb', line 53

def copy(destination, sources, options = {})
  copied = []

  FileUtils.mkdir_p(destination) unless File.exists?(destination)

  destination_migrations = migrations(destination)
  last = destination_migrations.last
  sources.each do |scope, path|
    migrations(path).each do |migration|
      source = File.read(migration[:filename])
      source = "# This migration comes from #{scope} (originally #{migration[:version]})\n#{source}"

      if duplicate = destination_migrations.detect { |m| m[:name] == migration[:name] }
        if options[:refresh] && duplicate[:scope] == scope.to_s
          Dir.glob(File.join(destination,"*_#{migration[:name].underscore}.#{scope.to_s}.rb")).each { |f| puts "Removing old migration #{migration[:name]}"; File.delete(f) }
        elsif options[:on_skip] && duplicate[:scope] != scope.to_s
          options[:on_skip].call(scope, migration)
        end
        next unless options[:refresh]
      end

      migration[:version] = next_migration_number(last ? last[:version] + 1 : 0).to_i unless options[:preserve_timestamp]
      new_path = File.join(destination, "#{migration[:version]}_#{migration[:name].underscore}.#{scope}.rb")
      old_path, migration[:filename] = migration[:filename], new_path
      last = migration

      File.open(migration[:filename], "w") { |f| f.write source }
      copied << migration
      options[:on_copy].call(scope, migration, old_path) if options[:on_copy]
      destination_migrations << migration
    end
  end

  copied
end

.initialize_data_migrations_tableObject



47
48
49
50
51
# File 'lib/data_migrator.rb', line 47

def initialize_data_migrations_table
  puts "** data_migrations table missing creating now...."
  puts ActiveRecord::Migrator.run(:up, File.join(File.dirname(__FILE__), '../db/migrate/'), 20100819181805)
  puts "** done"
end

.migrations(path) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/data_migrator.rb', line 20

def migrations(path)
  files = Dir["#{path}/**/[0-9]*_*.rb" ]

  seen = Hash.new false

  migrations = files.map do |file|
    version, name, scope = file.scan(/([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?.rb/).first

    raise ActiveRecord::IllegalMigrationNameError.new(file) unless version
    version = version.to_i
    name = name.camelize

    raise ActiveRecord::DuplicateMigrationVersionError.new(version) if seen[version]
    raise ActiveRecord::DuplicateMigrationNameError.new(name) if seen[name]

    seen[version] = seen[name] = true

    {:name => name, :filename => file, :version => version, :scope => scope}
  end

  migrations.sort_by{|h| h[:version]}
end

.migrations_pathObject



16
17
18
# File 'lib/data_migrator.rb', line 16

def migrations_path
  "#{Rails.root}/db/data_migrations/"
end

.next_migration_number(number) ⇒ Object



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

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

Instance Method Details

#get_current_versionObject



121
122
123
124
125
126
127
128
129
# File 'lib/data_migrator.rb', line 121

def get_current_version
  result = ActiveRecord::Base.connection.select_all("select max(version) as current_version from data_migrations")

  current_version = result[0]['current_version'] unless result == -1

  current_version = current_version.to_i unless current_version.nil?

  current_version
end

#migrate(passed_location = nil, passed_version = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/data_migrator.rb', line 95

def migrate(passed_location=nil, passed_version=nil)
  setup

  location = passed_location.nil? ? @default_migrations_path : passed_location
  @@current_version = get_current_version

  if passed_version.nil? || @@current_version.nil?
    run_all_non_migrated(passed_version)
  elsif passed_version < @@current_version
    handle_lower_passed_version(passed_version)
  elsif passed_version > @@current_version
    handle_higher_passed_version(passed_version)
  end
end

#pending_migrationsObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/data_migrator.rb', line 110

def pending_migrations
  versions = []
  files = get_all_files
  files.each do |file|
    filename, version, klass_name = separate_file_parts(file)
    versions << filename unless version_has_been_migrated?(version)
  end

  versions
end

#run_down(passed_version) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/data_migrator.rb', line 151

def run_down(passed_version)
  setup

  raise "VERSION is required" unless passed_version

  files = get_all_files
  found = false

  files.each do |file|
    filename, version, klass_name = separate_file_parts(file)
    if passed_version == version
      found = true
      (version_has_been_migrated?(version)) ? handle_action(file, klass_name, version, :down) : (puts "** Version #{passed_version} has not been migrated")
    end
  end

  puts "** Version #{passed_version} not found" unless found

end

#run_up(passed_version) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/data_migrator.rb', line 131

def run_up(passed_version)
  setup

  raise "VERSION is required" unless passed_version

  files = get_all_files
  found = false

  files.each do |file|
    filename, version, klass_name = separate_file_parts(file)
    if passed_version == version
      found = true
      (version_has_been_migrated?(version)) ? (puts "** Version #{passed_version} has already been migrated") : handle_action(file, klass_name, version, :up)
    end
  end

  puts "** Version #{passed_version} not found" unless found

end