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.
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) ⇒ MigrationContext
constructor
A new instance of MigrationContext.
-
#last_migration ⇒ Object
:nodoc:.
- #last_stored_environment ⇒ Object
- #migrate(target_version = nil, &block) ⇒ Object
- #migration_files ⇒ Object
- #migrations ⇒ Object
- #migrations_status ⇒ Object
- #needs_migration? ⇒ Boolean
- #open ⇒ Object
-
#parse_migration_filename(filename) ⇒ Object
:nodoc:.
- #protected_environment? ⇒ Boolean
- #rollback(steps = 1) ⇒ Object
- #run(direction, target_version) ⇒ Object
- #up(target_version = nil) ⇒ Object
Constructor Details
#initialize(migrations_paths) ⇒ MigrationContext
Returns a new instance of MigrationContext.
1004 1005 1006 |
# File 'lib/active_record/migration.rb', line 1004 def initialize(migrations_paths) @migrations_paths = migrations_paths end |
Instance Attribute Details
#migrations_paths ⇒ Object (readonly)
Returns the value of attribute migrations_paths.
1002 1003 1004 |
# File 'lib/active_record/migration.rb', line 1002 def migrations_paths @migrations_paths end |
Instance Method Details
#any_migrations? ⇒ Boolean
1074 1075 1076 |
# File 'lib/active_record/migration.rb', line 1074 def any_migrations? migrations.any? end |
#current_environment ⇒ Object
1122 1123 1124 |
# File 'lib/active_record/migration.rb', line 1122 def current_environment ActiveRecord::ConnectionHandling::DEFAULT_ENV.call end |
#current_version ⇒ Object
1065 1066 1067 1068 |
# File 'lib/active_record/migration.rb', line 1065 def current_version get_all_versions.max || 0 rescue ActiveRecord::NoDatabaseError end |
#down(target_version = nil) ⇒ Object
1039 1040 1041 1042 1043 1044 1045 1046 1047 |
# File 'lib/active_record/migration.rb', line 1039 def down(target_version = nil) selected_migrations = if block_given? migrations.select { |m| yield m } else migrations end Migrator.new(:down, selected_migrations, target_version).migrate end |
#forward(steps = 1) ⇒ Object
1025 1026 1027 |
# File 'lib/active_record/migration.rb', line 1025 def forward(steps = 1) move(:up, steps) end |
#get_all_versions ⇒ Object
1057 1058 1059 1060 1061 1062 1063 |
# File 'lib/active_record/migration.rb', line 1057 def get_all_versions if SchemaMigration.table_exists? SchemaMigration.all_versions.map(&:to_i) else [] end end |
#last_migration ⇒ Object
:nodoc:
1078 1079 1080 |
# File 'lib/active_record/migration.rb', line 1078 def last_migration #:nodoc: migrations.last || NullMigration.new end |
#last_stored_environment ⇒ Object
1130 1131 1132 1133 1134 1135 1136 1137 |
# File 'lib/active_record/migration.rb', line 1130 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
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 |
# File 'lib/active_record/migration.rb', line 1008 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 |
#migration_files ⇒ Object
1117 1118 1119 1120 |
# File 'lib/active_record/migration.rb', line 1117 def migration_files paths = Array(migrations_paths) Dir[*paths.flat_map { |path| "#{path}/**/[0-9]*_*.rb" }] end |
#migrations ⇒ Object
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 |
# File 'lib/active_record/migration.rb', line 1086 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
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 |
# File 'lib/active_record/migration.rb', line 1099 def migrations_status db_list = ActiveRecord::SchemaMigration.normalized_versions file_list = migration_files.map do |file| version, name, scope = parse_migration_filename(file) raise IllegalMigrationNameError.new(file) unless version version = ActiveRecord::SchemaMigration.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
1070 1071 1072 |
# File 'lib/active_record/migration.rb', line 1070 def needs_migration? (migrations.collect(&:version) - get_all_versions).size > 0 end |
#open ⇒ Object
1053 1054 1055 |
# File 'lib/active_record/migration.rb', line 1053 def open Migrator.new(:up, migrations, nil) end |
#parse_migration_filename(filename) ⇒ Object
:nodoc:
1082 1083 1084 |
# File 'lib/active_record/migration.rb', line 1082 def parse_migration_filename(filename) # :nodoc: File.basename(filename).scan(Migration::MigrationFilenameRegexp).first end |
#protected_environment? ⇒ Boolean
1126 1127 1128 |
# File 'lib/active_record/migration.rb', line 1126 def protected_environment? ActiveRecord::Base.protected_environments.include?(last_stored_environment) if last_stored_environment end |
#rollback(steps = 1) ⇒ Object
1021 1022 1023 |
# File 'lib/active_record/migration.rb', line 1021 def rollback(steps = 1) move(:down, steps) end |
#run(direction, target_version) ⇒ Object
1049 1050 1051 |
# File 'lib/active_record/migration.rb', line 1049 def run(direction, target_version) Migrator.new(direction, migrations, target_version).run end |
#up(target_version = nil) ⇒ Object
1029 1030 1031 1032 1033 1034 1035 1036 1037 |
# File 'lib/active_record/migration.rb', line 1029 def up(target_version = nil) selected_migrations = if block_given? migrations.select { |m| yield m } else migrations end Migrator.new(:up, selected_migrations, target_version).migrate end |