Class: ActiveRecord::MigrationContext
- Defined in:
- activerecord/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.
1034 1035 1036 1037 |
# File 'activerecord/lib/active_record/migration.rb', line 1034 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
1032 1033 1034 |
# File 'activerecord/lib/active_record/migration.rb', line 1032 def migrations_paths @migrations_paths end |
#schema_migration ⇒ Object (readonly)
Returns the value of attribute schema_migration
1032 1033 1034 |
# File 'activerecord/lib/active_record/migration.rb', line 1032 def schema_migration @schema_migration end |
Instance Method Details
#any_migrations? ⇒ Boolean
1105 1106 1107 |
# File 'activerecord/lib/active_record/migration.rb', line 1105 def any_migrations? migrations.any? end |
#current_environment ⇒ Object
1140 1141 1142 |
# File 'activerecord/lib/active_record/migration.rb', line 1140 def current_environment ActiveRecord::ConnectionHandling::DEFAULT_ENV.call end |
#current_version ⇒ Object
1096 1097 1098 1099 |
# File 'activerecord/lib/active_record/migration.rb', line 1096 def current_version get_all_versions.max || 0 rescue ActiveRecord::NoDatabaseError end |
#down(target_version = nil) ⇒ Object
1070 1071 1072 1073 1074 1075 1076 1077 1078 |
# File 'activerecord/lib/active_record/migration.rb', line 1070 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
1056 1057 1058 |
# File 'activerecord/lib/active_record/migration.rb', line 1056 def forward(steps = 1) move(:up, steps) end |
#get_all_versions ⇒ Object
1088 1089 1090 1091 1092 1093 1094 |
# File 'activerecord/lib/active_record/migration.rb', line 1088 def get_all_versions if schema_migration.table_exists? schema_migration.all_versions.map(&:to_i) else [] end end |
#last_stored_environment ⇒ Object
1148 1149 1150 1151 1152 1153 1154 1155 |
# File 'activerecord/lib/active_record/migration.rb', line 1148 def last_stored_environment 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
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 |
# File 'activerecord/lib/active_record/migration.rb', line 1039 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
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 |
# File 'activerecord/lib/active_record/migration.rb', line 1109 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
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 |
# File 'activerecord/lib/active_record/migration.rb', line 1122 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
1101 1102 1103 |
# File 'activerecord/lib/active_record/migration.rb', line 1101 def needs_migration? (migrations.collect(&:version) - get_all_versions).size > 0 end |
#open ⇒ Object
1084 1085 1086 |
# File 'activerecord/lib/active_record/migration.rb', line 1084 def open Migrator.new(:up, migrations, schema_migration) end |
#protected_environment? ⇒ Boolean
1144 1145 1146 |
# File 'activerecord/lib/active_record/migration.rb', line 1144 def protected_environment? ActiveRecord::Base.protected_environments.include?(last_stored_environment) if last_stored_environment end |
#rollback(steps = 1) ⇒ Object
1052 1053 1054 |
# File 'activerecord/lib/active_record/migration.rb', line 1052 def rollback(steps = 1) move(:down, steps) end |
#run(direction, target_version) ⇒ Object
1080 1081 1082 |
# File 'activerecord/lib/active_record/migration.rb', line 1080 def run(direction, target_version) Migrator.new(direction, migrations, schema_migration, target_version).run end |
#up(target_version = nil) ⇒ Object
1060 1061 1062 1063 1064 1065 1066 1067 1068 |
# File 'activerecord/lib/active_record/migration.rb', line 1060 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 |