Class: ActiveRecord::Migrator

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


1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
# File 'activerecord/lib/active_record/migration.rb', line 1257

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.


1247
1248
1249
# File 'activerecord/lib/active_record/migration.rb', line 1247

def migrations_paths
  @migrations_paths
end

Class Method Details

.current_versionObject

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


1250
1251
1252
# File 'activerecord/lib/active_record/migration.rb', line 1250

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

Instance Method Details

#current_migrationObject Also known as: current


1274
1275
1276
# File 'activerecord/lib/active_record/migration.rb', line 1274

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

#current_versionObject


1270
1271
1272
# File 'activerecord/lib/active_record/migration.rb', line 1270

def current_version
  migrated.max || 0
end

#load_migratedObject


1319
1320
1321
# File 'activerecord/lib/active_record/migration.rb', line 1319

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

#migrateObject


1287
1288
1289
1290
1291
1292
1293
# File 'activerecord/lib/active_record/migration.rb', line 1287

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

#migratedObject


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

def migrated
  @migrated_versions || load_migrated
end

#migrationsObject


1306
1307
1308
# File 'activerecord/lib/active_record/migration.rb', line 1306

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

#pending_migrationsObject


1310
1311
1312
1313
# File 'activerecord/lib/active_record/migration.rb', line 1310

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

#runObject


1279
1280
1281
1282
1283
1284
1285
# File 'activerecord/lib/active_record/migration.rb', line 1279

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

#runnableObject


1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
# File 'activerecord/lib/active_record/migration.rb', line 1295

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