Merge pull request #42803 from gregschmit/gns/fix-migrate-status-order

db:migrate:status: Sort migration ID as integer for consistency.
This commit is contained in:
Eileen M. Uchitelle 2021-07-20 08:29:10 -04:00 committed by GitHub
commit fad8d8a0f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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