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.



96
97
98
# File 'lib/data_migrator.rb', line 96

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

Class Method Details

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



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
88
89
90
91
92
93
# File 'lib/data_migrator.rb', line 59

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

.migrations(path) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/data_migrator.rb', line 32

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



28
29
30
# File 'lib/data_migrator.rb', line 28

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

.next_migration_number(number) ⇒ Object



55
56
57
# File 'lib/data_migrator.rb', line 55

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



126
127
128
129
130
131
132
133
134
# File 'lib/data_migrator.rb', line 126

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/data_migrator.rb', line 100

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



115
116
117
118
119
120
121
122
123
124
# File 'lib/data_migrator.rb', line 115

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



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/data_migrator.rb', line 169

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



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

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

#run_up_without_migrationObject



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/data_migrator.rb', line 136

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

  versions
end