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, target_version = nil) ⇒ Migrator

Returns a new instance of Migrator.



1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
# File 'lib/active_record/migration.rb', line 1299

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

  validate(@migrations)

  @schema_migration.create_table
  ActiveRecord::InternalMetadata.create_table
end

Class Attribute Details

.migrations_pathsObject

Returns the value of attribute migrations_paths.



1289
1290
1291
# File 'lib/active_record/migration.rb', line 1289

def migrations_paths
  @migrations_paths
end

Class Method Details

.current_versionObject

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



1292
1293
1294
# File 'lib/active_record/migration.rb', line 1292

def current_version
  MigrationContext.new(migrations_paths, SchemaMigration).current_version
end

Instance Method Details

#current_migrationObject Also known as: current



1316
1317
1318
# File 'lib/active_record/migration.rb', line 1316

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

#current_versionObject



1312
1313
1314
# File 'lib/active_record/migration.rb', line 1312

def current_version
  migrated.max || 0
end

#load_migratedObject



1361
1362
1363
# File 'lib/active_record/migration.rb', line 1361

def load_migrated
  @migrated_versions = Set.new(@schema_migration.all_versions.map(&:to_i))
end

#migrateObject



1329
1330
1331
1332
1333
1334
1335
# File 'lib/active_record/migration.rb', line 1329

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

#migratedObject



1357
1358
1359
# File 'lib/active_record/migration.rb', line 1357

def migrated
  @migrated_versions || load_migrated
end

#migrationsObject



1348
1349
1350
# File 'lib/active_record/migration.rb', line 1348

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

#pending_migrationsObject



1352
1353
1354
1355
# File 'lib/active_record/migration.rb', line 1352

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

#runObject



1321
1322
1323
1324
1325
1326
1327
# File 'lib/active_record/migration.rb', line 1321

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

#runnableObject



1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
# File 'lib/active_record/migration.rb', line 1337

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