Sort migration ID as int in db:migrate:status for consistency.

This commit is contained in:
Gregory N. Schmit 2021-07-16 12:05:28 -05:00
parent 359efef64b
commit 78955b925a
No known key found for this signature in database
GPG Key ID: FE10BE3BD89CD73D
6 changed files with 59 additions and 1 deletions

View File

@ -1177,7 +1177,7 @@ module ActiveRecord
["up", version, "********** NO FILE **********"]
end
(db_list + file_list).sort_by { |_, version, _| version }
(db_list + file_list).sort_by { |_, version, _| version.to_i }
end
def current_environment # :nodoc:

View File

@ -167,6 +167,44 @@ class MigratorTest < ActiveRecord::TestCase
], ActiveRecord::MigrationContext.new(path, schema_migration).migrations_status
end
def test_migrations_status_order_new_and_old_version
path = MIGRATIONS_ROOT + "/old_and_new_versions"
schema_migration = ActiveRecord::Base.connection.schema_migration
@schema_migration.create(version: 230)
@schema_migration.create(version: 231)
@schema_migration.create(version: 20210716122844)
@schema_migration.create(version: 20210716123013)
assert_equal [
["up", "230", "Add people hobby"],
["up", "231", "Add people last name"],
["up", "20210716122844", "Add people description"],
["up", "20210716123013", "Add people number of legs"],
], ActiveRecord::MigrationContext.new(path, schema_migration).migrations_status
end
def test_migrations_status_order_new_and_old_version_applied_out_of_order
path = MIGRATIONS_ROOT + "/old_and_new_versions"
schema_migration = ActiveRecord::Base.connection.schema_migration
@schema_migration.create(version: 230)
@schema_migration.create(version: 231)
# "Apply" a newer migration and not an older to simulate out-of-order
# migration application which should not affect ordering in status and is
# possible if a branch is merged which contains a migration which has an
# earlier version but is judged to be compatible with existing migrations.
@schema_migration.create(version: 20210716123013)
assert_equal [
["up", "230", "Add people hobby"],
["up", "231", "Add people last name"],
["down", "20210716122844", "Add people description"],
["up", "20210716123013", "Add people number of legs"],
], ActiveRecord::MigrationContext.new(path, schema_migration).migrations_status
end
def test_migrations_status_in_subdirectories
path = MIGRATIONS_ROOT + "/valid_with_subdirectories"
schema_migration = ActiveRecord::Base.connection.schema_migration

View File

@ -0,0 +1,5 @@
# frozen_string_literal: true
class AddPeopleDescription < ActiveRecord::Migration::Current
add_column :people, :description, :string
end

View File

@ -0,0 +1,5 @@
# frozen_string_literal: true
class AddPeopleNumberOfLegs < ActiveRecord::Migration::Current
add_column :people, :number_of_legs, :integer
end

View File

@ -0,0 +1,5 @@
# frozen_string_literal: true
class AddPeopleHobby < ActiveRecord::Migration::Current
add_column :people, :hobby, :string
end

View File

@ -0,0 +1,5 @@
# frozen_string_literal: true
class AddPeopleLastName < ActiveRecord::Migration::Current
add_column :people, :last_name, :string
end