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 1128 1129 |
# File 'lib/active_record/migration.rb', line 1117 def initialize(direction, migrations, target_version = nil) raise StandardError.new("This database does not yet support migrations") unless Base.connection.supports_migrations? @direction = direction @target_version = target_version @migrated_versions = nil @migrations = migrations validate(@migrations) Base.connection.initialize_schema_migrations_table Base.connection. 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
1009 1010 1011 1012 1013 1014 |
# File 'lib/active_record/migration.rb', line 1009 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
998 999 1000 |
# File 'lib/active_record/migration.rb', line 998 def forward(migrations_paths, steps=1) move(:up, migrations_paths, steps) end |
.get_all_versions(connection = Base.connection) ⇒ Object
1028 1029 1030 1031 1032 1033 1034 1035 1036 |
# File 'lib/active_record/migration.rb', line 1028 def get_all_versions(connection = Base.connection) ActiveSupport::Deprecation.silence do if connection.table_exists?(schema_migrations_table_name) SchemaMigration.all.map { |x| x.version.to_i }.sort else [] end 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
981 982 983 984 985 986 987 988 989 990 991 992 |
# File 'lib/active_record/migration.rb', line 981 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
1020 1021 1022 |
# File 'lib/active_record/migration.rb', line 1020 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
994 995 996 |
# File 'lib/active_record/migration.rb', line 994 def rollback(migrations_paths, steps=1) move(:down, migrations_paths, steps) end |
.run(direction, migrations_paths, target_version) ⇒ Object
1016 1017 1018 |
# File 'lib/active_record/migration.rb', line 1016 def run(direction, migrations_paths, target_version) new(direction, migrations(migrations_paths), target_version).run end |
.schema_migrations_table_name ⇒ Object
1024 1025 1026 |
# File 'lib/active_record/migration.rb', line 1024 def schema_migrations_table_name SchemaMigration.table_name end |
.up(migrations_paths, target_version = nil) ⇒ Object
1002 1003 1004 1005 1006 1007 |
# File 'lib/active_record/migration.rb', line 1002 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
1135 1136 1137 |
# File 'lib/active_record/migration.rb', line 1135 def current_migration migrations.detect { |m| m.version == current_version } end |
#current_version ⇒ Object
1131 1132 1133 |
# File 'lib/active_record/migration.rb', line 1131 def current_version migrated.max || 0 end |
#load_migrated ⇒ Object
1180 1181 1182 |
# File 'lib/active_record/migration.rb', line 1180 def load_migrated @migrated_versions = Set.new(self.class.get_all_versions) end |
#migrate ⇒ Object
1148 1149 1150 1151 1152 1153 1154 |
# File 'lib/active_record/migration.rb', line 1148 def migrate if use_advisory_lock? with_advisory_lock { migrate_without_lock } else migrate_without_lock end end |
#migrated ⇒ Object
1176 1177 1178 |
# File 'lib/active_record/migration.rb', line 1176 def migrated @migrated_versions || load_migrated end |
#migrations ⇒ Object
1167 1168 1169 |
# File 'lib/active_record/migration.rb', line 1167 def migrations down? ? @migrations.reverse : @migrations.sort_by(&:version) end |
#pending_migrations ⇒ Object
1171 1172 1173 1174 |
# File 'lib/active_record/migration.rb', line 1171 def pending_migrations already_migrated = migrated migrations.reject { |m| already_migrated.include?(m.version) } end |
#run ⇒ Object
1140 1141 1142 1143 1144 1145 1146 |
# File 'lib/active_record/migration.rb', line 1140 def run if use_advisory_lock? with_advisory_lock { run_without_lock } else run_without_lock end end |
#runnable ⇒ Object
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 |
# File 'lib/active_record/migration.rb', line 1156 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 |