diff options
author | bogdanvlviv <bogdanvlviv@gmail.com> | 2017-09-26 00:10:17 +0300 |
---|---|---|
committer | bogdanvlviv <bogdanvlviv@gmail.com> | 2017-11-06 22:40:10 +0000 |
commit | 90fe2a42f0b68a66e970169d38e91a0126de7d3e (patch) | |
tree | 1dfbb41a911c19f1494d495ce4b22d055300bfb1 /activerecord/lib/active_record/tasks/database_tasks.rb | |
parent | 63f0c04850dd0bcdc7d35266e81fa1a7778570a8 (diff) | |
download | rails-90fe2a42f0b68a66e970169d38e91a0126de7d3e.tar.gz rails-90fe2a42f0b68a66e970169d38e91a0126de7d3e.tar.bz2 rails-90fe2a42f0b68a66e970169d38e91a0126de7d3e.zip |
Fix `bin/rails db:migrate` with specified `VERSION`
Ensure that `bin/rails db:migrate` with specified `VERSION` reverts
all migrations only if `VERSION` is `0`.
Raise error if target migration doesn't exist.
Diffstat (limited to 'activerecord/lib/active_record/tasks/database_tasks.rb')
-rw-r--r-- | activerecord/lib/active_record/tasks/database_tasks.rb | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index ff388ff1f6..4657e51e6d 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -164,13 +164,12 @@ module ActiveRecord end def migrate - raise "Empty VERSION provided" if ENV["VERSION"] && ENV["VERSION"].empty? + check_target_version verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] != "false" : true - version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil scope = ENV["SCOPE"] verbose_was, Migration.verbose = Migration.verbose, verbose - Migrator.migrate(migrations_paths, version) do |migration| + Migrator.migrate(migrations_paths, target_version) do |migration| scope.blank? || scope == migration.scope end ActiveRecord::Base.clear_cache! @@ -178,6 +177,16 @@ module ActiveRecord Migration.verbose = verbose_was end + def check_target_version + if target_version && !(Migration::MigrationFilenameRegexp.match?(ENV["VERSION"]) || /\A\d+\z/.match?(ENV["VERSION"])) + raise "Invalid format of target version: `VERSION=#{ENV['VERSION']}`" + end + end + + def target_version + ENV["VERSION"].to_i if ENV["VERSION"] && !ENV["VERSION"].empty? + end + def charset_current(environment = env) charset ActiveRecord::Base.configurations[environment] end |