Class: ActiveRecord::Migrator

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

Overview

:nodoc:

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(direction, migrations, schema_migration, internal_metadata, target_version = nil) ⇒ Migrator

Returns a new instance of Migrator.



1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
# File 'lib/active_record/migration.rb', line 1426

def initialize(direction, migrations, schema_migration, , target_version = nil)
  @direction         = direction
  @target_version    = target_version
  @migrated_versions = nil
  @migrations        = migrations
  @schema_migration  = schema_migration
  @internal_metadata = 

  validate(@migrations)

  @schema_migration.create_table
  @internal_metadata.create_table
end

Class Attribute Details

.migrations_pathsObject

Returns the value of attribute migrations_paths.



1412
1413
1414
# File 'lib/active_record/migration.rb', line 1412

def migrations_paths
  @migrations_paths
end

Class Method Details

.current_versionObject

For cases where a table doesn’t exist like loading from schema cache



1415
1416
1417
1418
1419
1420
1421
# File 'lib/active_record/migration.rb', line 1415

def current_version
  connection_pool = ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool
  schema_migration = SchemaMigration.new(connection_pool)
   = InternalMetadata.new(connection_pool)

  MigrationContext.new(migrations_paths, schema_migration, ).current_version
end

Instance Method Details

#current_migrationObject Also known as: current



1444
1445
1446
# File 'lib/active_record/migration.rb', line 1444

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

#current_versionObject



1440
1441
1442
# File 'lib/active_record/migration.rb', line 1440

def current_version
  migrated.max || 0
end

#load_migratedObject



1489
1490
1491
# File 'lib/active_record/migration.rb', line 1489

def load_migrated
  @migrated_versions = Set.new(@schema_migration.integer_versions)
end

#migrateObject



1457
1458
1459
1460
1461
1462
1463
# File 'lib/active_record/migration.rb', line 1457

def migrate
  if use_advisory_lock?
    with_advisory_lock { migrate_without_lock }
  else
    migrate_without_lock
  end
end

#migratedObject



1485
1486
1487
# File 'lib/active_record/migration.rb', line 1485

def migrated
  @migrated_versions || load_migrated
end

#migrationsObject



1476
1477
1478
# File 'lib/active_record/migration.rb', line 1476

def migrations
  down? ? @migrations.reverse : @migrations.sort_by(&:version)
end

#pending_migrationsObject



1480
1481
1482
1483
# File 'lib/active_record/migration.rb', line 1480

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

#runObject



1449
1450
1451
1452
1453
1454
1455
# File 'lib/active_record/migration.rb', line 1449

def run
  if use_advisory_lock?
    with_advisory_lock { run_without_lock }
  else
    run_without_lock
  end
end

#runnableObject



1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
# File 'lib/active_record/migration.rb', line 1465

def runnable
  runnable = migrations[start..finish]
  if up?
    runnable.reject { |m| ran?(m) }
  else
    # skip the last migration if we're headed down, but not ALL the way down
    runnable.pop if target
    runnable.find_all { |m| ran?(m) }
  end
end