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

Returns a new instance of Migrator.



1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
# File 'lib/active_record/migration.rb', line 1179

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

  validate(@migrations)

  ActiveRecord::SchemaMigration.create_table
  ActiveRecord::InternalMetadata.create_table
end

Class Attribute Details

.migrations_pathsObject

Returns the value of attribute migrations_paths.



1162
1163
1164
# File 'lib/active_record/migration.rb', line 1162

def migrations_paths
  @migrations_paths
end

Class Method Details

.current_versionObject

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



1172
1173
1174
# File 'lib/active_record/migration.rb', line 1172

def current_version
  MigrationContext.new(migrations_paths).current_version
end

.migrations_path=(path) ⇒ Object



1164
1165
1166
1167
1168
1169
# File 'lib/active_record/migration.rb', line 1164

def migrations_path=(path)
  ActiveSupport::Deprecation.warn \
    "ActiveRecord::Migrator.migrations_paths= is now deprecated and will be removed in Rails 6.0." \
    "You can set the `migrations_paths` on the `connection` instead through the `database.yml`."
  self.migrations_paths = [path]
end

Instance Method Details

#current_migrationObject Also known as: current



1195
1196
1197
# File 'lib/active_record/migration.rb', line 1195

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

#current_versionObject



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

def current_version
  migrated.max || 0
end

#load_migratedObject



1240
1241
1242
# File 'lib/active_record/migration.rb', line 1240

def load_migrated
  @migrated_versions = Set.new(Base.connection.migration_context.get_all_versions)
end

#migrateObject



1208
1209
1210
1211
1212
1213
1214
# File 'lib/active_record/migration.rb', line 1208

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

#migratedObject



1236
1237
1238
# File 'lib/active_record/migration.rb', line 1236

def migrated
  @migrated_versions || load_migrated
end

#migrationsObject



1227
1228
1229
# File 'lib/active_record/migration.rb', line 1227

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

#pending_migrationsObject



1231
1232
1233
1234
# File 'lib/active_record/migration.rb', line 1231

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

#runObject



1200
1201
1202
1203
1204
1205
1206
# File 'lib/active_record/migration.rb', line 1200

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

#runnableObject



1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
# File 'lib/active_record/migration.rb', line 1216

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