Merge pull request #44276 from Cofense/activerecord_databasetasks_check_target_version_integer

Accept _ integer notation in VERSION arg to database tasks

Also correct unrelated typo in DatabaseTasks test description.
This commit is contained in:
Jean Boussier 2022-02-12 10:51:46 +01:00
commit 41c9d15b74
4 changed files with 26 additions and 5 deletions

View File

@ -1,3 +1,7 @@
* Permit underscores in the VERSION argument to database rake tasks.
*Eddie Lebow*
* Reversed the order of `INSERT` statements in `structure.sql` dumps * Reversed the order of `INSERT` statements in `structure.sql` dumps
This should decrease the likelihood of merge conflicts. New migrations This should decrease the likelihood of merge conflicts. New migrations

View File

@ -575,6 +575,13 @@ module ActiveRecord
MigrationFilenameRegexp = /\A([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?\.rb\z/ # :nodoc: MigrationFilenameRegexp = /\A([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?\.rb\z/ # :nodoc:
def self.valid_version_format?(version_string) # :nodoc:
[
MigrationFilenameRegexp,
/\A\d(_?\d)*\z/ # integer with optional underscores
].any? { |pattern| pattern.match?(version_string) }
end
# This class is used to verify that all migrations have been run before # This class is used to verify that all migrations have been run before
# loading a web page if <tt>config.active_record.migration_error</tt> is set to :page_load # loading a web page if <tt>config.active_record.migration_error</tt> is set to :page_load
class CheckPending class CheckPending

View File

@ -305,7 +305,7 @@ module ActiveRecord
end end
def check_target_version def check_target_version
if target_version && !(Migration::MigrationFilenameRegexp.match?(ENV["VERSION"]) || /\A\d+\z/.match?(ENV["VERSION"])) if target_version && !Migration.valid_version_format?(ENV["VERSION"])
raise "Invalid format of target version: `VERSION=#{ENV['VERSION']}`" raise "Invalid format of target version: `VERSION=#{ENV['VERSION']}`"
end end
end end

View File

@ -1075,6 +1075,10 @@ module ActiveRecord
e = assert_raise(RuntimeError) { ActiveRecord::Tasks::DatabaseTasks.migrate } e = assert_raise(RuntimeError) { ActiveRecord::Tasks::DatabaseTasks.migrate }
assert_match(/Invalid format of target version/, e.message) assert_match(/Invalid format of target version/, e.message)
ENV["VERSION"] = "1__1"
e = assert_raise(RuntimeError) { ActiveRecord::Tasks::DatabaseTasks.migrate }
assert_match(/Invalid format of target version/, e.message)
ENV["VERSION"] = "1_name" ENV["VERSION"] = "1_name"
e = assert_raise(RuntimeError) { ActiveRecord::Tasks::DatabaseTasks.migrate } e = assert_raise(RuntimeError) { ActiveRecord::Tasks::DatabaseTasks.migrate }
assert_match(/Invalid format of target version/, e.message) assert_match(/Invalid format of target version/, e.message)
@ -1415,13 +1419,16 @@ module ActiveRecord
version = ENV["VERSION"] version = ENV["VERSION"]
ENV["VERSION"] = "0" ENV["VERSION"] = "0"
assert_equal ENV["VERSION"].to_i, ActiveRecord::Tasks::DatabaseTasks.target_version assert_equal 0, ActiveRecord::Tasks::DatabaseTasks.target_version
ENV["VERSION"] = "42" ENV["VERSION"] = "42"
assert_equal ENV["VERSION"].to_i, ActiveRecord::Tasks::DatabaseTasks.target_version assert_equal 42, ActiveRecord::Tasks::DatabaseTasks.target_version
ENV["VERSION"] = "042" ENV["VERSION"] = "042"
assert_equal ENV["VERSION"].to_i, ActiveRecord::Tasks::DatabaseTasks.target_version assert_equal 42, ActiveRecord::Tasks::DatabaseTasks.target_version
ENV["VERSION"] = "2000_01_01_000042"
assert_equal 20000101000042, ActiveRecord::Tasks::DatabaseTasks.target_version
ensure ensure
ENV["VERSION"] = version ENV["VERSION"] = version
end end
@ -1436,7 +1443,7 @@ module ActiveRecord
ENV["VERSION"] = version ENV["VERSION"] = version
end end
def test_check_target_version_does_not_raise_error_if_version_is_not_setted def test_check_target_version_does_not_raise_error_if_version_is_not_set
version = ENV.delete("VERSION") version = ENV.delete("VERSION")
assert_nothing_raised { ActiveRecord::Tasks::DatabaseTasks.check_target_version } assert_nothing_raised { ActiveRecord::Tasks::DatabaseTasks.check_target_version }
ensure ensure
@ -1489,6 +1496,9 @@ module ActiveRecord
ENV["VERSION"] = "001" ENV["VERSION"] = "001"
assert_nothing_raised { ActiveRecord::Tasks::DatabaseTasks.check_target_version } assert_nothing_raised { ActiveRecord::Tasks::DatabaseTasks.check_target_version }
ENV["VERSION"] = "1_001"
assert_nothing_raised { ActiveRecord::Tasks::DatabaseTasks.check_target_version }
ENV["VERSION"] = "001_name.rb" ENV["VERSION"] = "001_name.rb"
assert_nothing_raised { ActiveRecord::Tasks::DatabaseTasks.check_target_version } assert_nothing_raised { ActiveRecord::Tasks::DatabaseTasks.check_target_version }
ensure ensure