Class: ActiveRecord::MigrationContext
- Defined in:
- activerecord/lib/active_record/migration.rb
Overview
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
-
#internal_metadata ⇒ Object
readonly
Returns the value of attribute internal_metadata.
-
#migrations_paths ⇒ Object
readonly
Returns the value of attribute migrations_paths.
-
#schema_migration ⇒ Object
readonly
Returns the value of attribute schema_migration.
Instance Method Summary collapse
-
#current_environment ⇒ Object
:nodoc:.
-
#current_version ⇒ Object
:nodoc:.
-
#down(target_version = nil, &block) ⇒ Object
:nodoc:.
-
#forward(steps = 1) ⇒ Object
:nodoc:.
-
#get_all_versions ⇒ Object
:nodoc:.
-
#initialize(migrations_paths, schema_migration = nil, internal_metadata = nil) ⇒ MigrationContext
constructor
A new instance of MigrationContext.
-
#last_stored_environment ⇒ Object
:nodoc:.
-
#migrate(target_version = nil, &block) ⇒ Object
Runs the migrations in the
migrations_path
. -
#migrations ⇒ Object
:nodoc:.
-
#migrations_status ⇒ Object
:nodoc:.
-
#needs_migration? ⇒ Boolean
:nodoc:.
-
#open ⇒ Object
:nodoc:.
-
#pending_migration_versions ⇒ Object
:nodoc:.
-
#protected_environment? ⇒ Boolean
:nodoc:.
-
#rollback(steps = 1) ⇒ Object
:nodoc:.
-
#run(direction, target_version) ⇒ Object
:nodoc:.
-
#up(target_version = nil, &block) ⇒ Object
:nodoc:.
Constructor Details
#initialize(migrations_paths, schema_migration = nil, internal_metadata = nil) ⇒ MigrationContext
Returns a new instance of MigrationContext.
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 |
# File 'activerecord/lib/active_record/migration.rb', line 1125 def initialize(migrations_paths, schema_migration = nil, = nil) if schema_migration == SchemaMigration ActiveRecord.deprecator.warn(<<-MSG.squish) SchemaMigration no longer inherits from ActiveRecord::Base. If you want to use the default connection, remove this argument. If you want to use a specific connection, instantiate MigrationContext with the connection's schema migration, for example `MigrationContext.new(path, Dog.connection.schema_migration)`. MSG schema_migration = nil end if == InternalMetadata ActiveRecord.deprecator.warn(<<-MSG.squish) SchemaMigration no longer inherits from ActiveRecord::Base. If you want to use the default connection, remove this argument. If you want to use a specific connection, instantiate MigrationContext with the connection's internal metadata, for example `MigrationContext.new(path, nil, Dog.connection.internal_metadata)`. MSG = nil end @migrations_paths = migrations_paths @schema_migration = schema_migration || SchemaMigration.new(connection) @internal_metadata = || InternalMetadata.new(connection) end |
Instance Attribute Details
#internal_metadata ⇒ Object (readonly)
Returns the value of attribute internal_metadata
1123 1124 1125 |
# File 'activerecord/lib/active_record/migration.rb', line 1123 def @internal_metadata end |
#migrations_paths ⇒ Object (readonly)
Returns the value of attribute migrations_paths
1123 1124 1125 |
# File 'activerecord/lib/active_record/migration.rb', line 1123 def migrations_paths @migrations_paths end |
#schema_migration ⇒ Object (readonly)
Returns the value of attribute schema_migration
1123 1124 1125 |
# File 'activerecord/lib/active_record/migration.rb', line 1123 def schema_migration @schema_migration end |
Instance Method Details
#current_environment ⇒ Object
:nodoc:
1267 1268 1269 |
# File 'activerecord/lib/active_record/migration.rb', line 1267 def current_environment # :nodoc: ActiveRecord::ConnectionHandling::DEFAULT_ENV.call end |
#current_version ⇒ Object
:nodoc:
1223 1224 1225 1226 |
# File 'activerecord/lib/active_record/migration.rb', line 1223 def current_version # :nodoc: get_all_versions.max || 0 rescue ActiveRecord::NoDatabaseError end |
#down(target_version = nil, &block) ⇒ Object
:nodoc:
1197 1198 1199 1200 1201 1202 1203 1204 1205 |
# File 'activerecord/lib/active_record/migration.rb', line 1197 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:
1183 1184 1185 |
# File 'activerecord/lib/active_record/migration.rb', line 1183 def forward(steps = 1) # :nodoc: move(:up, steps) end |
#get_all_versions ⇒ Object
:nodoc:
1215 1216 1217 1218 1219 1220 1221 |
# File 'activerecord/lib/active_record/migration.rb', line 1215 def get_all_versions # :nodoc: if schema_migration.table_exists? schema_migration.integer_versions else [] end end |
#last_stored_environment ⇒ Object
:nodoc:
1275 1276 1277 1278 1279 1280 1281 1282 1283 |
# File 'activerecord/lib/active_record/migration.rb', line 1275 def last_stored_environment # :nodoc: return nil unless connection..enabled? return nil if current_version == 0 raise NoEnvironmentInSchemaError unless connection..table_exists? environment = connection.[: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
.
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 |
# File 'activerecord/lib/active_record/migration.rb', line 1166 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 |
#migrations ⇒ Object
:nodoc:
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 |
# File 'activerecord/lib/active_record/migration.rb', line 1236 def migrations # :nodoc: migrations = migration_files.map do |file| version, name, scope = parse_migration_filename(file) raise IllegalMigrationNameError.new(file) unless version version = version.to_i name = name.camelize MigrationProxy.new(name, version, file, scope) end migrations.sort_by(&:version) end |
#migrations_status ⇒ Object
:nodoc:
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 |
# File 'activerecord/lib/active_record/migration.rb', line 1249 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 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:
1228 1229 1230 |
# File 'activerecord/lib/active_record/migration.rb', line 1228 def needs_migration? # :nodoc: pending_migration_versions.size > 0 end |
#open ⇒ Object
:nodoc:
1211 1212 1213 |
# File 'activerecord/lib/active_record/migration.rb', line 1211 def open # :nodoc: Migrator.new(:up, migrations, schema_migration, ) end |
#pending_migration_versions ⇒ Object
:nodoc:
1232 1233 1234 |
# File 'activerecord/lib/active_record/migration.rb', line 1232 def pending_migration_versions # :nodoc: migrations.collect(&:version) - get_all_versions end |
#protected_environment? ⇒ Boolean
:nodoc:
1271 1272 1273 |
# File 'activerecord/lib/active_record/migration.rb', line 1271 def protected_environment? # :nodoc: ActiveRecord::Base.protected_environments.include?(last_stored_environment) if last_stored_environment end |
#rollback(steps = 1) ⇒ Object
:nodoc:
1179 1180 1181 |
# File 'activerecord/lib/active_record/migration.rb', line 1179 def rollback(steps = 1) # :nodoc: move(:down, steps) end |
#run(direction, target_version) ⇒ Object
:nodoc:
1207 1208 1209 |
# File 'activerecord/lib/active_record/migration.rb', line 1207 def run(direction, target_version) # :nodoc: Migrator.new(direction, migrations, schema_migration, , target_version).run end |
#up(target_version = nil, &block) ⇒ Object
:nodoc:
1187 1188 1189 1190 1191 1192 1193 1194 1195 |
# File 'activerecord/lib/active_record/migration.rb', line 1187 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 |