Class: MongoidMigration::Migrator

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

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(direction, migrations_path, target_version = nil) ⇒ Migrator

Returns a new instance of Migrator.



254
255
256
# File 'lib/mongoid_migration/migration.rb', line 254

def initialize(direction, migrations_path, target_version = nil)
  @direction, @migrations_path, @target_version = direction, migrations_path, target_version
end

Class Method Details

.current_versionObject



236
237
238
# File 'lib/mongoid_migration/migration.rb', line 236

def current_version
  get_all_versions.max || 0
end

.down(migrations_path, target_version = nil) ⇒ Object



220
221
222
# File 'lib/mongoid_migration/migration.rb', line 220

def down(migrations_path, target_version = nil)
  self.new(:down, migrations_path, target_version).migrate
end

.forward(migrations_path, steps = 1) ⇒ Object



212
213
214
# File 'lib/mongoid_migration/migration.rb', line 212

def forward(migrations_path, steps=1)
  move(:up, migrations_path, steps)
end

.get_all_versionsObject



232
233
234
# File 'lib/mongoid_migration/migration.rb', line 232

def get_all_versions
  Migration.all.map {|e| e.version.to_i}.sort
end

.migrate(migrations_path, target_version = nil) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/mongoid_migration/migration.rb', line 196

def migrate(migrations_path, target_version = nil)
  case
    when target_version.nil?
      up(migrations_path, target_version)
    when current_version == 0 && target_version == 0
    when current_version > target_version
      down(migrations_path, target_version)
    else
      up(migrations_path, target_version)
  end
end

.migrations_pathObject



228
229
230
# File 'lib/mongoid_migration/migration.rb', line 228

def migrations_path
  'mongodb/migrate'
end

.rollback(migrations_path, steps = 1) ⇒ Object



208
209
210
# File 'lib/mongoid_migration/migration.rb', line 208

def rollback(migrations_path, steps=1)
  move(:down, migrations_path, steps)
end

.run(direction, migrations_path, target_version) ⇒ Object



224
225
226
# File 'lib/mongoid_migration/migration.rb', line 224

def run(direction, migrations_path, target_version)
  self.new(direction, migrations_path, target_version).run
end

.up(migrations_path, target_version = nil) ⇒ Object



216
217
218
# File 'lib/mongoid_migration/migration.rb', line 216

def up(migrations_path, target_version = nil)
  self.new(:up, migrations_path, target_version).migrate
end

Instance Method Details

#current_migrationObject



262
263
264
# File 'lib/mongoid_migration/migration.rb', line 262

def current_migration
  migrations.detect { |m| m.version == current_version }
end

#current_versionObject



258
259
260
# File 'lib/mongoid_migration/migration.rb', line 258

def current_version
  migrated.last || 0
end

#migrateObject



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/mongoid_migration/migration.rb', line 275

def migrate
  current = migrations.detect { |m| m.version == current_version }
  target = migrations.detect { |m| m.version == @target_version }

  if target.nil? && !@target_version.nil? && @target_version > 0
    raise UnknownMigrationVersionError.new(@target_version)
  end

  start = up? ? 0 : (migrations.index(current) || 0)
  finish = migrations.index(target) || migrations.size - 1
  runnable = migrations[start..finish]

  # skip the last migration if we're headed down, but not ALL the way down
  runnable.pop if down? && !target.nil?

  runnable.each do |migration|
    Rails.logger.info "Migrating to #{migration.name} (#{migration.version})" if Rails.logger

    # On our way up, we skip migrating the ones we've already migrated
    next if up? && migrated.include?(migration.version.to_i)

    # On our way down, we skip reverting the ones we've never migrated
    if down? && !migrated.include?(migration.version.to_i)
      migration.announce 'never migrated, skipping'; migration.write
      next
    end

    begin
      migration.migrate(@direction)
      record_version_state_after_migrating(migration.version)
    rescue => e
      raise StandardError, "An error has occurred, all later migrations canceled:\n\n#{e}", e.backtrace
    end
  end
end

#migratedObject



348
349
350
# File 'lib/mongoid_migration/migration.rb', line 348

def migrated
  @migrated_versions ||= self.class.get_all_versions
end

#migrationsObject



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/mongoid_migration/migration.rb', line 311

def migrations
  @migrations ||= begin
    files = Dir["#{@migrations_path}/[0-9]*_*.rb"]

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

      raise IllegalMigrationNameError.new(file) unless version
      version = version.to_i

      if klasses.detect { |m| m.version == version }
        raise DuplicateMigrationVersionError.new(version)
      end

      if klasses.detect { |m| m.name == name.camelize }
        raise DuplicateMigrationNameError.new(name.camelize)
      end

      migration = MigrationProxy.new
      migration.basename = File.basename(file)
      migration.name     = name.camelize
      migration.version  = version
      migration.filename = file
      migration.scope    = scope unless scope.blank?
      klasses << migration
    end

    migrations = migrations.sort_by { |m| m.version }
    down? ? migrations.reverse : migrations
  end
end

#pending_migrationsObject



343
344
345
346
# File 'lib/mongoid_migration/migration.rb', line 343

def pending_migrations
  already_migrated = migrated
  migrations.reject { |m| already_migrated.include?(m.version.to_i) }
end

#runObject



266
267
268
269
270
271
272
273
# File 'lib/mongoid_migration/migration.rb', line 266

def run
  target = migrations.detect { |m| m.version == @target_version }
  raise UnknownMigrationVersionError.new(@target_version) if target.nil?
  unless (up? && migrated.include?(target.version.to_i)) || (down? && !migrated.include?(target.version.to_i))
    target.migrate(@direction)
    record_version_state_after_migrating(target.version)
  end
end