Class: ActiveRecord::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/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.

Raises:

  • (StandardError)


428
429
430
431
432
# File 'lib/active_record/migration.rb', line 428

def initialize(direction, migrations_path, target_version = nil)
  raise StandardError.new("This database does not yet support migrations") unless Base.connection.supports_migrations?
  Base.connection.initialize_schema_migrations_table
  @direction, @migrations_path, @target_version = direction, migrations_path, target_version      
end

Class Method Details

.current_versionObject



413
414
415
416
417
418
419
420
# File 'lib/active_record/migration.rb', line 413

def current_version
  sm_table = schema_migrations_table_name
  if Base.connection.table_exists?(sm_table)
    get_all_versions.max || 0
  else
    0
  end
end

.down(migrations_path, target_version = nil) ⇒ Object



397
398
399
# File 'lib/active_record/migration.rb', line 397

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

.get_all_versionsObject



409
410
411
# File 'lib/active_record/migration.rb', line 409

def get_all_versions
  Base.connection.select_values("SELECT version FROM #{schema_migrations_table_name}").map(&:to_i).sort
end

.migrate(migrations_path, target_version = nil) ⇒ Object



375
376
377
378
379
380
381
# File 'lib/active_record/migration.rb', line 375

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

.proper_table_name(name) ⇒ Object



422
423
424
425
# File 'lib/active_record/migration.rb', line 422

def proper_table_name(name)
  # Use the Active Record objects own table_name, or pre/suffix from ActiveRecord::Base if name is a symbol/string
  name.table_name rescue "#{ActiveRecord::Base.table_name_prefix}#{name}#{ActiveRecord::Base.table_name_suffix}"
end

.rollback(migrations_path, steps = 1) ⇒ Object



383
384
385
386
387
388
389
390
391
# File 'lib/active_record/migration.rb', line 383

def rollback(migrations_path, steps=1)
  migrator = self.new(:down, migrations_path)
  start_index = migrator.migrations.index(migrator.current_migration)
  
  return unless start_index
  
  finish = migrator.migrations[start_index + steps]
  down(migrations_path, finish ? finish.version : 0)
end

.run(direction, migrations_path, target_version) ⇒ Object



401
402
403
# File 'lib/active_record/migration.rb', line 401

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

.schema_migrations_table_nameObject



405
406
407
# File 'lib/active_record/migration.rb', line 405

def schema_migrations_table_name
  Base.table_name_prefix + 'schema_migrations' + Base.table_name_suffix
end

.up(migrations_path, target_version = nil) ⇒ Object



393
394
395
# File 'lib/active_record/migration.rb', line 393

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

Instance Method Details

#current_migrationObject



438
439
440
# File 'lib/active_record/migration.rb', line 438

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

#current_versionObject



434
435
436
# File 'lib/active_record/migration.rb', line 434

def current_version
  migrated.last || 0
end

#migrateObject



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/active_record/migration.rb', line 451

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|
    Base.logger.info "Migrating to #{migration.name} (#{migration.version})"

    # 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
      ddl_transaction do
        migration.migrate(@direction)
        record_version_state_after_migrating(migration.version)
      end
    rescue => e
      canceled_msg = Base.connection.supports_ddl_transactions? ? "this and " : ""
      raise StandardError, "An error has occurred, #{canceled_msg}all later migrations canceled:\n\n#{e}", e.backtrace
    end
  end
end

#migratedObject



525
526
527
# File 'lib/active_record/migration.rb', line 525

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

#migrationsObject



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/active_record/migration.rb', line 490

def migrations
  @migrations ||= begin
    files = Dir["#{@migrations_path}/[0-9]*_*.rb"]
    
    migrations = files.inject([]) do |klasses, file|
      version, name = file.scan(/([0-9]+)_([_a-z0-9]*).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
      
      klasses << returning(MigrationProxy.new) do |migration|
        migration.name     = name.camelize
        migration.version  = version
        migration.filename = file
      end
    end
    
    migrations = migrations.sort_by(&:version)
    down? ? migrations.reverse : migrations
  end
end

#pending_migrationsObject



520
521
522
523
# File 'lib/active_record/migration.rb', line 520

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

#runObject



442
443
444
445
446
447
448
449
# File 'lib/active_record/migration.rb', line 442

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