Class: ActiveRecord::MigrationContext

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

Overview

Migration Context

MigrationContext sets the context in which a migration is run.

A migration context requires the path to the migrations is set in the migrations_paths parameter. Optionally a schema_migration class can be provided. Multiple database applications will instantiate a SchemaMigration object per database. From the Rake tasks, Rails will handle this for you.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(migrations_paths, schema_migration = nil, internal_metadata = nil) ⇒ MigrationContext

Returns a new instance of MigrationContext.



1219
1220
1221
1222
1223
# File 'lib/active_record/migration.rb', line 1219

def initialize(migrations_paths, schema_migration = nil,  = nil)
  @migrations_paths = migrations_paths
  @schema_migration = schema_migration || SchemaMigration.new(connection_pool)
  @internal_metadata =  || InternalMetadata.new(connection_pool)
end

Instance Attribute Details

#internal_metadataObject (readonly)

Returns the value of attribute internal_metadata.



1217
1218
1219
# File 'lib/active_record/migration.rb', line 1217

def 
  @internal_metadata
end

#migrations_pathsObject (readonly)

Returns the value of attribute migrations_paths.



1217
1218
1219
# File 'lib/active_record/migration.rb', line 1217

def migrations_paths
  @migrations_paths
end

#schema_migrationObject (readonly)

Returns the value of attribute schema_migration.



1217
1218
1219
# File 'lib/active_record/migration.rb', line 1217

def schema_migration
  @schema_migration
end

Instance Method Details

#current_environmentObject

:nodoc:



1345
1346
1347
# File 'lib/active_record/migration.rb', line 1345

def current_environment # :nodoc:
  ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
end

#current_versionObject

:nodoc:



1295
1296
1297
1298
# File 'lib/active_record/migration.rb', line 1295

def current_version # :nodoc:
  get_all_versions.max || 0
rescue ActiveRecord::NoDatabaseError
end

#down(target_version = nil, &block) ⇒ Object

:nodoc:



1269
1270
1271
1272
1273
1274
1275
1276
1277
# File 'lib/active_record/migration.rb', line 1269

def down(target_version = nil, &block) # :nodoc:
  selected_migrations = if block_given?
    migrations.select(&block)
  else
    migrations
  end

  Migrator.new(:down, selected_migrations, schema_migration, , target_version).migrate
end

#forward(steps = 1) ⇒ Object

:nodoc:



1255
1256
1257
# File 'lib/active_record/migration.rb', line 1255

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

#get_all_versionsObject

:nodoc:



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

def get_all_versions # :nodoc:
  if schema_migration.table_exists?
    schema_migration.integer_versions
  else
    []
  end
end

#last_stored_environmentObject

:nodoc:



1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
# File 'lib/active_record/migration.rb', line 1353

def last_stored_environment # :nodoc:
   = connection_pool.
  return nil unless .enabled?
  return nil if current_version == 0
  raise NoEnvironmentInSchemaError unless .table_exists?

  environment = [:environment]
  raise NoEnvironmentInSchemaError unless environment
  environment
end

#migrate(target_version = nil, &block) ⇒ Object

Runs the migrations in the migrations_path.

If target_version is nil, migrate will run up.

If the current_version and target_version are both 0 then an empty array will be returned and no migrations will be run.

If the current_version in the schema is greater than the target_version, then down will be run.

If none of the conditions are met, up will be run with the target_version.



1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
# File 'lib/active_record/migration.rb', line 1238

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

#migrationsObject

:nodoc:



1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
# File 'lib/active_record/migration.rb', line 1308

def migrations # :nodoc:
  migrations = migration_files.map do |file|
    version, name, scope = parse_migration_filename(file)
    raise IllegalMigrationNameError.new(file) unless version
    if validate_timestamp? && !valid_migration_timestamp?(version)
      raise InvalidMigrationTimestampError.new(version, name)
    end
    version = version.to_i
    name = name.camelize

    MigrationProxy.new(name, version, file, scope)
  end

  migrations.sort_by(&:version)
end

#migrations_statusObject

:nodoc:



1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
# File 'lib/active_record/migration.rb', line 1324

def migrations_status # :nodoc:
  db_list = schema_migration.normalized_versions

  file_list = migration_files.filter_map do |file|
    version, name, scope = parse_migration_filename(file)
    raise IllegalMigrationNameError.new(file) unless version
    if validate_timestamp? && !valid_migration_timestamp?(version)
      raise InvalidMigrationTimestampError.new(version, name)
    end
    version = schema_migration.normalize_migration_number(version)
    status = db_list.delete(version) ? "up" : "down"
    [status, version, (name + scope).humanize]
  end

  db_list.map! do |version|
    ["up", version, "********** NO FILE **********"]
  end

  (db_list + file_list).sort_by { |_, version, _| version.to_i }
end

#needs_migration?Boolean

:nodoc:

Returns:

  • (Boolean)


1300
1301
1302
# File 'lib/active_record/migration.rb', line 1300

def needs_migration? # :nodoc:
  pending_migration_versions.size > 0
end

#openObject

:nodoc:



1283
1284
1285
# File 'lib/active_record/migration.rb', line 1283

def open # :nodoc:
  Migrator.new(:up, migrations, schema_migration, )
end

#pending_migration_versionsObject

:nodoc:



1304
1305
1306
# File 'lib/active_record/migration.rb', line 1304

def pending_migration_versions # :nodoc:
  migrations.collect(&:version) - get_all_versions
end

#protected_environment?Boolean

:nodoc:

Returns:

  • (Boolean)


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

def protected_environment? # :nodoc:
  ActiveRecord::Base.protected_environments.include?(last_stored_environment) if last_stored_environment
end

#rollback(steps = 1) ⇒ Object

:nodoc:



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

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

#run(direction, target_version) ⇒ Object

:nodoc:



1279
1280
1281
# File 'lib/active_record/migration.rb', line 1279

def run(direction, target_version) # :nodoc:
  Migrator.new(direction, migrations, schema_migration, , target_version).run
end

#up(target_version = nil, &block) ⇒ Object

:nodoc:



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

def up(target_version = nil, &block) # :nodoc:
  selected_migrations = if block_given?
    migrations.select(&block)
  else
    migrations
  end

  Migrator.new(:up, selected_migrations, schema_migration, , target_version).migrate
end