Class: ActiveRecord::Migrator
- Inherits:
-
Object
- Object
- ActiveRecord::Migrator
- Defined in:
- 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.
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 |
# File 'lib/active_record/migration.rb', line 1117 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
.migrations_paths ⇒ Object
1054 1055 1056 1057 1058 |
# File 'lib/active_record/migration.rb', line 1054 def migrations_paths @migrations_paths ||= ["db/migrate"] # just to not break things if someone uses: migrations_path = some_string Array(@migrations_paths) end |
Class Method Details
.any_migrations? ⇒ Boolean
1046 1047 1048 |
# File 'lib/active_record/migration.rb', line 1046 def any_migrations? migrations(migrations_paths).any? end |
.current_version(connection = Base.connection) ⇒ Object
1038 1039 1040 |
# File 'lib/active_record/migration.rb', line 1038 def current_version(connection = Base.connection) get_all_versions(connection).max || 0 end |
.down(migrations_paths, target_version = nil) ⇒ Object
1010 1011 1012 1013 1014 1015 |
# File 'lib/active_record/migration.rb', line 1010 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
999 1000 1001 |
# File 'lib/active_record/migration.rb', line 999 def forward(migrations_paths, steps = 1) move(:up, migrations_paths, steps) end |
.get_all_versions(connection = Base.connection) ⇒ Object
1030 1031 1032 1033 1034 1035 1036 |
# File 'lib/active_record/migration.rb', line 1030 def get_all_versions(connection = Base.connection) if SchemaMigration.table_exists? SchemaMigration.all_versions.map(&:to_i) else [] end end |
.last_migration ⇒ Object
:nodoc:
1050 1051 1052 |
# File 'lib/active_record/migration.rb', line 1050 def last_migration #:nodoc: migrations(migrations_paths).last || NullMigration.new end |
.migrate(migrations_paths, target_version = nil, &block) ⇒ Object
982 983 984 985 986 987 988 989 990 991 992 993 |
# File 'lib/active_record/migration.rb', line 982 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
1099 1100 1101 |
# File 'lib/active_record/migration.rb', line 1099 def migration_files(paths) Dir[*paths.flat_map { |path| "#{path}/**/[0-9]*_*.rb" }] end |
.migrations(paths) ⇒ Object
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 |
# File 'lib/active_record/migration.rb', line 1064 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
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 |
# File 'lib/active_record/migration.rb', line 1079 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
1042 1043 1044 |
# File 'lib/active_record/migration.rb', line 1042 def needs_migration?(connection = Base.connection) (migrations(migrations_paths).collect(&:version) - get_all_versions(connection)).size > 0 end |
.open(migrations_paths) ⇒ Object
1021 1022 1023 |
# File 'lib/active_record/migration.rb', line 1021 def open(migrations_paths) new(:up, migrations(migrations_paths), nil) end |
.parse_migration_filename(filename) ⇒ Object
:nodoc:
1060 1061 1062 |
# File 'lib/active_record/migration.rb', line 1060 def parse_migration_filename(filename) # :nodoc: File.basename(filename).scan(Migration::MigrationFilenameRegexp).first end |
.rollback(migrations_paths, steps = 1) ⇒ Object
995 996 997 |
# File 'lib/active_record/migration.rb', line 995 def rollback(migrations_paths, steps = 1) move(:down, migrations_paths, steps) end |
.run(direction, migrations_paths, target_version) ⇒ Object
1017 1018 1019 |
# File 'lib/active_record/migration.rb', line 1017 def run(direction, migrations_paths, target_version) new(direction, migrations(migrations_paths), target_version).run end |
.schema_migrations_table_name ⇒ Object
1025 1026 1027 |
# File 'lib/active_record/migration.rb', line 1025 def schema_migrations_table_name SchemaMigration.table_name end |
.up(migrations_paths, target_version = nil) ⇒ Object
1003 1004 1005 1006 1007 1008 |
# File 'lib/active_record/migration.rb', line 1003 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
1133 1134 1135 |
# File 'lib/active_record/migration.rb', line 1133 def current_migration migrations.detect { |m| m.version == current_version } end |
#current_version ⇒ Object
1129 1130 1131 |
# File 'lib/active_record/migration.rb', line 1129 def current_version migrated.max || 0 end |
#load_migrated ⇒ Object
1178 1179 1180 |
# File 'lib/active_record/migration.rb', line 1178 def load_migrated @migrated_versions = Set.new(self.class.get_all_versions) end |
#migrate ⇒ Object
1146 1147 1148 1149 1150 1151 1152 |
# File 'lib/active_record/migration.rb', line 1146 def migrate if use_advisory_lock? with_advisory_lock { migrate_without_lock } else migrate_without_lock end end |
#migrated ⇒ Object
1174 1175 1176 |
# File 'lib/active_record/migration.rb', line 1174 def migrated @migrated_versions || load_migrated end |
#migrations ⇒ Object
1165 1166 1167 |
# File 'lib/active_record/migration.rb', line 1165 def migrations down? ? @migrations.reverse : @migrations.sort_by(&:version) end |
#pending_migrations ⇒ Object
1169 1170 1171 1172 |
# File 'lib/active_record/migration.rb', line 1169 def pending_migrations already_migrated = migrated migrations.reject { |m| already_migrated.include?(m.version) } end |
#run ⇒ Object
1138 1139 1140 1141 1142 1143 1144 |
# File 'lib/active_record/migration.rb', line 1138 def run if use_advisory_lock? with_advisory_lock { run_without_lock } else run_without_lock end end |
#runnable ⇒ Object
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 |
# File 'lib/active_record/migration.rb', line 1154 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 |