Class: ActiveRecord::MigrationContext
- Inherits:
-
Object
- Object
- ActiveRecord::MigrationContext
- Defined in:
- lib/active_record/migration.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#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
- #any_migrations? ⇒ Boolean
- #current_environment ⇒ Object
- #current_version ⇒ Object
- #down(target_version = nil) ⇒ Object
- #forward(steps = 1) ⇒ Object
- #get_all_versions ⇒ Object
-
#initialize(migrations_paths, schema_migration) ⇒ MigrationContext
constructor
A new instance of MigrationContext.
- #last_stored_environment ⇒ Object
- #migrate(target_version = nil, &block) ⇒ Object
- #migrations ⇒ Object
- #migrations_status ⇒ Object
- #needs_migration? ⇒ Boolean
- #open ⇒ Object
- #protected_environment? ⇒ Boolean
- #rollback(steps = 1) ⇒ Object
- #run(direction, target_version) ⇒ Object
- #up(target_version = nil) ⇒ Object
Constructor Details
#initialize(migrations_paths, schema_migration) ⇒ MigrationContext
Returns a new instance of MigrationContext.
1053 1054 1055 1056 |
# File 'lib/active_record/migration.rb', line 1053 def initialize(migrations_paths, schema_migration) @migrations_paths = migrations_paths @schema_migration = schema_migration end |
Instance Attribute Details
#migrations_paths ⇒ Object (readonly)
Returns the value of attribute migrations_paths.
1051 1052 1053 |
# File 'lib/active_record/migration.rb', line 1051 def migrations_paths @migrations_paths end |
#schema_migration ⇒ Object (readonly)
Returns the value of attribute schema_migration.
1051 1052 1053 |
# File 'lib/active_record/migration.rb', line 1051 def schema_migration @schema_migration end |
Instance Method Details
#any_migrations? ⇒ Boolean
1124 1125 1126 |
# File 'lib/active_record/migration.rb', line 1124 def any_migrations? migrations.any? end |
#current_environment ⇒ Object
1159 1160 1161 |
# File 'lib/active_record/migration.rb', line 1159 def current_environment ActiveRecord::ConnectionHandling::DEFAULT_ENV.call end |
#current_version ⇒ Object
1115 1116 1117 1118 |
# File 'lib/active_record/migration.rb', line 1115 def current_version get_all_versions.max || 0 rescue ActiveRecord::NoDatabaseError end |
#down(target_version = nil) ⇒ Object
1089 1090 1091 1092 1093 1094 1095 1096 1097 |
# File 'lib/active_record/migration.rb', line 1089 def down(target_version = nil) selected_migrations = if block_given? migrations.select { |m| yield m } else migrations end Migrator.new(:down, selected_migrations, schema_migration, target_version).migrate end |
#forward(steps = 1) ⇒ Object
1075 1076 1077 |
# File 'lib/active_record/migration.rb', line 1075 def forward(steps = 1) move(:up, steps) end |
#get_all_versions ⇒ Object
1107 1108 1109 1110 1111 1112 1113 |
# File 'lib/active_record/migration.rb', line 1107 def get_all_versions if schema_migration.table_exists? schema_migration.all_versions.map(&:to_i) else [] end end |
#last_stored_environment ⇒ Object
1167 1168 1169 1170 1171 1172 1173 1174 1175 |
# File 'lib/active_record/migration.rb', line 1167 def last_stored_environment return nil unless ActiveRecord::InternalMetadata.enabled? return nil if current_version == 0 raise NoEnvironmentInSchemaError unless ActiveRecord::InternalMetadata.table_exists? environment = ActiveRecord::InternalMetadata[:environment] raise NoEnvironmentInSchemaError unless environment environment end |
#migrate(target_version = nil, &block) ⇒ Object
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 |
# File 'lib/active_record/migration.rb', line 1058 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
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 |
# File 'lib/active_record/migration.rb', line 1128 def migrations 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
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 |
# File 'lib/active_record/migration.rb', line 1141 def migrations_status db_list = schema_migration.normalized_versions file_list = migration_files.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.compact db_list.map! do |version| ["up", version, "********** NO FILE **********"] end (db_list + file_list).sort_by { |_, version, _| version } end |
#needs_migration? ⇒ Boolean
1120 1121 1122 |
# File 'lib/active_record/migration.rb', line 1120 def needs_migration? (migrations.collect(&:version) - get_all_versions).size > 0 end |
#open ⇒ Object
1103 1104 1105 |
# File 'lib/active_record/migration.rb', line 1103 def open Migrator.new(:up, migrations, schema_migration) end |
#protected_environment? ⇒ Boolean
1163 1164 1165 |
# File 'lib/active_record/migration.rb', line 1163 def protected_environment? ActiveRecord::Base.protected_environments.include?(last_stored_environment) if last_stored_environment end |
#rollback(steps = 1) ⇒ Object
1071 1072 1073 |
# File 'lib/active_record/migration.rb', line 1071 def rollback(steps = 1) move(:down, steps) end |
#run(direction, target_version) ⇒ Object
1099 1100 1101 |
# File 'lib/active_record/migration.rb', line 1099 def run(direction, target_version) Migrator.new(direction, migrations, schema_migration, target_version).run end |
#up(target_version = nil) ⇒ Object
1079 1080 1081 1082 1083 1084 1085 1086 1087 |
# File 'lib/active_record/migration.rb', line 1079 def up(target_version = nil) selected_migrations = if block_given? migrations.select { |m| yield m } else migrations end Migrator.new(:up, selected_migrations, schema_migration, target_version).migrate end |