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.



1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
# File 'activerecord/lib/active_record/migration.rb', line 1199

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



1189
1190
1191
# File 'activerecord/lib/active_record/migration.rb', line 1189

def migrations_paths
  @migrations_paths
end

Class Method Details

.current_versionObject

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



1192
1193
1194
# File 'activerecord/lib/active_record/migration.rb', line 1192

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

Instance Method Details

#current_migrationObject Also known as: current



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

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

#current_versionObject



1212
1213
1214
# File 'activerecord/lib/active_record/migration.rb', line 1212

def current_version
  migrated.max || 0
end

#load_migratedObject



1261
1262
1263
# File 'activerecord/lib/active_record/migration.rb', line 1261

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

#migrateObject



1229
1230
1231
1232
1233
1234
1235
# File 'activerecord/lib/active_record/migration.rb', line 1229

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

#migratedObject



1257
1258
1259
# File 'activerecord/lib/active_record/migration.rb', line 1257

def migrated
  @migrated_versions || load_migrated
end

#migrationsObject



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

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

#pending_migrationsObject



1252
1253
1254
1255
# File 'activerecord/lib/active_record/migration.rb', line 1252

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

#runObject



1221
1222
1223
1224
1225
1226
1227
# File 'activerecord/lib/active_record/migration.rb', line 1221

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

#runnableObject



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

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