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:.
-
.match_to_migration_filename?(filename) ⇒ Boolean
:nodoc:.
- .migrate(migrations_paths, target_version = nil, &block) ⇒ Object
- .migrations(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.
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 |
# File 'lib/active_record/migration.rb', line 1098 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
1053 1054 1055 1056 1057 |
# File 'lib/active_record/migration.rb', line 1053 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
1045 1046 1047 |
# File 'lib/active_record/migration.rb', line 1045 def any_migrations? migrations(migrations_paths).any? end |
.current_version(connection = Base.connection) ⇒ Object
1037 1038 1039 |
# File 'lib/active_record/migration.rb', line 1037 def current_version(connection = Base.connection) get_all_versions(connection).max || 0 end |
.down(migrations_paths, target_version = nil) ⇒ Object
1008 1009 1010 1011 1012 1013 |
# File 'lib/active_record/migration.rb', line 1008 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
997 998 999 |
# File 'lib/active_record/migration.rb', line 997 def forward(migrations_paths, steps=1) move(:up, migrations_paths, steps) end |
.get_all_versions(connection = Base.connection) ⇒ Object
1027 1028 1029 1030 1031 1032 1033 1034 1035 |
# File 'lib/active_record/migration.rb', line 1027 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:
1049 1050 1051 |
# File 'lib/active_record/migration.rb', line 1049 def last_migration #:nodoc: migrations(migrations_paths).last || NullMigration.new end |
.match_to_migration_filename?(filename) ⇒ Boolean
:nodoc:
1059 1060 1061 |
# File 'lib/active_record/migration.rb', line 1059 def match_to_migration_filename?(filename) # :nodoc: File.basename(filename) =~ Migration::MigrationFilenameRegexp end |
.migrate(migrations_paths, target_version = nil, &block) ⇒ Object
980 981 982 983 984 985 986 987 988 989 990 991 |
# File 'lib/active_record/migration.rb', line 980 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 |
.migrations(paths) ⇒ Object
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 |
# File 'lib/active_record/migration.rb', line 1067 def migrations(paths) paths = Array(paths) files = Dir[*paths.map { |p| "#{p}/**/[0-9]*_*.rb" }] migrations = 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 |
.needs_migration?(connection = Base.connection) ⇒ Boolean
1041 1042 1043 |
# File 'lib/active_record/migration.rb', line 1041 def needs_migration?(connection = Base.connection) (migrations(migrations_paths).collect(&:version) - get_all_versions(connection)).size > 0 end |
.open(migrations_paths) ⇒ Object
1019 1020 1021 |
# File 'lib/active_record/migration.rb', line 1019 def open(migrations_paths) new(:up, migrations(migrations_paths), nil) end |
.parse_migration_filename(filename) ⇒ Object
:nodoc:
1063 1064 1065 |
# File 'lib/active_record/migration.rb', line 1063 def parse_migration_filename(filename) # :nodoc: File.basename(filename).scan(Migration::MigrationFilenameRegexp).first end |
.rollback(migrations_paths, steps = 1) ⇒ Object
993 994 995 |
# File 'lib/active_record/migration.rb', line 993 def rollback(migrations_paths, steps=1) move(:down, migrations_paths, steps) end |
.run(direction, migrations_paths, target_version) ⇒ Object
1015 1016 1017 |
# File 'lib/active_record/migration.rb', line 1015 def run(direction, migrations_paths, target_version) new(direction, migrations(migrations_paths), target_version).run end |
.schema_migrations_table_name ⇒ Object
1023 1024 1025 |
# File 'lib/active_record/migration.rb', line 1023 def schema_migrations_table_name SchemaMigration.table_name end |
.up(migrations_paths, target_version = nil) ⇒ Object
1001 1002 1003 1004 1005 1006 |
# File 'lib/active_record/migration.rb', line 1001 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
1116 1117 1118 |
# File 'lib/active_record/migration.rb', line 1116 def current_migration migrations.detect { |m| m.version == current_version } end |
#current_version ⇒ Object
1112 1113 1114 |
# File 'lib/active_record/migration.rb', line 1112 def current_version migrated.max || 0 end |
#load_migrated ⇒ Object
1161 1162 1163 |
# File 'lib/active_record/migration.rb', line 1161 def load_migrated @migrated_versions = Set.new(self.class.get_all_versions) end |
#migrate ⇒ Object
1129 1130 1131 1132 1133 1134 1135 |
# File 'lib/active_record/migration.rb', line 1129 def migrate if use_advisory_lock? with_advisory_lock { migrate_without_lock } else migrate_without_lock end end |
#migrated ⇒ Object
1157 1158 1159 |
# File 'lib/active_record/migration.rb', line 1157 def migrated @migrated_versions || load_migrated end |
#migrations ⇒ Object
1148 1149 1150 |
# File 'lib/active_record/migration.rb', line 1148 def migrations down? ? @migrations.reverse : @migrations.sort_by(&:version) end |
#pending_migrations ⇒ Object
1152 1153 1154 1155 |
# File 'lib/active_record/migration.rb', line 1152 def pending_migrations already_migrated = migrated migrations.reject { |m| already_migrated.include?(m.version) } end |
#run ⇒ Object
1121 1122 1123 1124 1125 1126 1127 |
# File 'lib/active_record/migration.rb', line 1121 def run if use_advisory_lock? with_advisory_lock { run_without_lock } else run_without_lock end end |
#runnable ⇒ Object
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 |
# File 'lib/active_record/migration.rb', line 1137 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 |