Class: ActiveRecord::Migrator
- Defined in:
- activerecord/lib/active_record/migration.rb
Overview
:nodoc:
Class Attribute Summary collapse
Class Method Summary collapse
- .any_migrations? ⇒ Boolean
- .current_version(connection = Base.connection) ⇒ Object
- .down(migrations_paths, target_version = nil) ⇒ Object
- .forward(migrations_paths, steps = 1) ⇒ Object
- .get_all_versions(connection = Base.connection) ⇒ Object
-
.last_migration ⇒ Object
:nodoc:.
- .migrate(migrations_paths, target_version = nil, &block) ⇒ Object
- .migration_files(paths) ⇒ Object
- .migrations(paths) ⇒ Object
- .migrations_status(paths) ⇒ Object
- .needs_migration?(connection = Base.connection) ⇒ Boolean
- .open(migrations_paths) ⇒ Object
-
.parse_migration_filename(filename) ⇒ Object
:nodoc:.
- .rollback(migrations_paths, steps = 1) ⇒ Object
- .run(direction, migrations_paths, target_version) ⇒ Object
- .schema_migrations_table_name ⇒ Object
- .up(migrations_paths, target_version = nil) ⇒ Object
Instance Method Summary collapse
- #current_migration ⇒ Object (also: #current)
- #current_version ⇒ Object
-
#initialize(direction, migrations, target_version = nil) ⇒ Migrator
constructor
A new instance of Migrator.
- #load_migrated ⇒ Object
- #migrate ⇒ Object
- #migrated ⇒ Object
- #migrations ⇒ Object
- #pending_migrations ⇒ Object
- #run ⇒ Object
- #runnable ⇒ Object
Constructor Details
#initialize(direction, migrations, target_version = nil) ⇒ Migrator
Returns a new instance of Migrator.
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 |
# File 'activerecord/lib/active_record/migration.rb', line 1129 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
Class Method Details
.any_migrations? ⇒ Boolean
1050 1051 1052 |
# File 'activerecord/lib/active_record/migration.rb', line 1050 def any_migrations? migrations(migrations_paths).any? end |
.current_version(connection = Base.connection) ⇒ Object
1042 1043 1044 |
# File 'activerecord/lib/active_record/migration.rb', line 1042 def current_version(connection = Base.connection) get_all_versions(connection).max || 0 end |
.down(migrations_paths, target_version = nil) ⇒ Object
1014 1015 1016 1017 1018 1019 |
# File 'activerecord/lib/active_record/migration.rb', line 1014 def down(migrations_paths, target_version = nil) migrations = migrations(migrations_paths) migrations.select! { |m| yield m } if block_given? new(:down, migrations, target_version).migrate end |
.forward(migrations_paths, steps = 1) ⇒ Object
1003 1004 1005 |
# File 'activerecord/lib/active_record/migration.rb', line 1003 def forward(migrations_paths, steps = 1) move(:up, migrations_paths, steps) end |
.get_all_versions(connection = Base.connection) ⇒ Object
1034 1035 1036 1037 1038 1039 1040 |
# File 'activerecord/lib/active_record/migration.rb', line 1034 def get_all_versions(connection = Base.connection) if SchemaMigration.table_exists? SchemaMigration.all_versions.map(&:to_i) else [] end end |
.last_migration ⇒ Object
:nodoc:
1054 1055 1056 |
# File 'activerecord/lib/active_record/migration.rb', line 1054 def last_migration #:nodoc: migrations(migrations_paths).last || NullMigration.new end |
.migrate(migrations_paths, target_version = nil, &block) ⇒ Object
986 987 988 989 990 991 992 993 994 995 996 997 |
# File 'activerecord/lib/active_record/migration.rb', line 986 def migrate(migrations_paths, target_version = nil, &block) case when target_version.nil? up(migrations_paths, target_version, &block) when current_version == 0 && target_version == 0 [] when current_version > target_version down(migrations_paths, target_version, &block) else up(migrations_paths, target_version, &block) end end |
.migration_files(paths) ⇒ Object
1103 1104 1105 |
# File 'activerecord/lib/active_record/migration.rb', line 1103 def migration_files(paths) Dir[*paths.flat_map { |path| "#{path}/**/[0-9]*_*.rb" }] end |
.migrations(paths) ⇒ Object
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 |
# File 'activerecord/lib/active_record/migration.rb', line 1068 def migrations(paths) paths = Array(paths) migrations = migration_files(paths).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(paths) ⇒ Object
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 |
# File 'activerecord/lib/active_record/migration.rb', line 1083 def migrations_status(paths) paths = Array(paths) db_list = ActiveRecord::SchemaMigration.normalized_versions file_list = migration_files(paths).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?(connection = Base.connection) ⇒ Boolean
1046 1047 1048 |
# File 'activerecord/lib/active_record/migration.rb', line 1046 def needs_migration?(connection = Base.connection) (migrations(migrations_paths).collect(&:version) - get_all_versions(connection)).size > 0 end |
.open(migrations_paths) ⇒ Object
1025 1026 1027 |
# File 'activerecord/lib/active_record/migration.rb', line 1025 def open(migrations_paths) new(:up, migrations(migrations_paths), nil) end |
.parse_migration_filename(filename) ⇒ Object
:nodoc:
1064 1065 1066 |
# File 'activerecord/lib/active_record/migration.rb', line 1064 def parse_migration_filename(filename) # :nodoc: File.basename(filename).scan(Migration::MigrationFilenameRegexp).first end |
.rollback(migrations_paths, steps = 1) ⇒ Object
999 1000 1001 |
# File 'activerecord/lib/active_record/migration.rb', line 999 def rollback(migrations_paths, steps = 1) move(:down, migrations_paths, steps) end |
.run(direction, migrations_paths, target_version) ⇒ Object
1021 1022 1023 |
# File 'activerecord/lib/active_record/migration.rb', line 1021 def run(direction, migrations_paths, target_version) new(direction, migrations(migrations_paths), target_version).run end |
.schema_migrations_table_name ⇒ Object
1029 1030 1031 |
# File 'activerecord/lib/active_record/migration.rb', line 1029 def schema_migrations_table_name SchemaMigration.table_name end |
.up(migrations_paths, target_version = nil) ⇒ Object
1007 1008 1009 1010 1011 1012 |
# File 'activerecord/lib/active_record/migration.rb', line 1007 def up(migrations_paths, target_version = nil) migrations = migrations(migrations_paths) migrations.select! { |m| yield m } if block_given? new(:up, migrations, target_version).migrate end |
Instance Method Details
#current_migration ⇒ Object Also known as: current
1145 1146 1147 |
# File 'activerecord/lib/active_record/migration.rb', line 1145 def current_migration migrations.detect { |m| m.version == current_version } end |
#current_version ⇒ Object
1141 1142 1143 |
# File 'activerecord/lib/active_record/migration.rb', line 1141 def current_version migrated.max || 0 end |
#load_migrated ⇒ Object
1190 1191 1192 |
# File 'activerecord/lib/active_record/migration.rb', line 1190 def load_migrated @migrated_versions = Set.new(self.class.get_all_versions) end |
#migrate ⇒ Object
1158 1159 1160 1161 1162 1163 1164 |
# File 'activerecord/lib/active_record/migration.rb', line 1158 def migrate if use_advisory_lock? with_advisory_lock { migrate_without_lock } else migrate_without_lock end end |
#migrated ⇒ Object
1186 1187 1188 |
# File 'activerecord/lib/active_record/migration.rb', line 1186 def migrated @migrated_versions || load_migrated end |
#migrations ⇒ Object
1177 1178 1179 |
# File 'activerecord/lib/active_record/migration.rb', line 1177 def migrations down? ? @migrations.reverse : @migrations.sort_by(&:version) end |
#pending_migrations ⇒ Object
1181 1182 1183 1184 |
# File 'activerecord/lib/active_record/migration.rb', line 1181 def pending_migrations already_migrated = migrated migrations.reject { |m| already_migrated.include?(m.version) } end |
#run ⇒ Object
1150 1151 1152 1153 1154 1155 1156 |
# File 'activerecord/lib/active_record/migration.rb', line 1150 def run if use_advisory_lock? with_advisory_lock { run_without_lock } else run_without_lock end end |
#runnable ⇒ Object
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 |
# File 'activerecord/lib/active_record/migration.rb', line 1166 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 |