From 78955b925acf1785005421b6f26468856ef4ce04 Mon Sep 17 00:00:00 2001 From: "Gregory N. Schmit" Date: Fri, 16 Jul 2021 12:05:28 -0500 Subject: [PATCH] Sort migration ID as int in db:migrate:status for consistency. --- activerecord/lib/active_record/migration.rb | 2 +- activerecord/test/cases/migrator_test.rb | 38 +++++++++++++++++++ .../20210716122844_add_people_description.rb | 5 +++ ...0210716123013_add_people_number_of_legs.rb | 5 +++ .../230_add_people_hobby.rb | 5 +++ .../231_add_people_last_name.rb | 5 +++ 6 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 activerecord/test/migrations/old_and_new_versions/20210716122844_add_people_description.rb create mode 100644 activerecord/test/migrations/old_and_new_versions/20210716123013_add_people_number_of_legs.rb create mode 100644 activerecord/test/migrations/old_and_new_versions/230_add_people_hobby.rb create mode 100644 activerecord/test/migrations/old_and_new_versions/231_add_people_last_name.rb diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index e73e66a0429..ab278c9a59a 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -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: diff --git a/activerecord/test/cases/migrator_test.rb b/activerecord/test/cases/migrator_test.rb index aeba8e1d14b..3c87b4b164d 100644 --- a/activerecord/test/cases/migrator_test.rb +++ b/activerecord/test/cases/migrator_test.rb @@ -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 diff --git a/activerecord/test/migrations/old_and_new_versions/20210716122844_add_people_description.rb b/activerecord/test/migrations/old_and_new_versions/20210716122844_add_people_description.rb new file mode 100644 index 00000000000..6de72ce31e9 --- /dev/null +++ b/activerecord/test/migrations/old_and_new_versions/20210716122844_add_people_description.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class AddPeopleDescription < ActiveRecord::Migration::Current + add_column :people, :description, :string +end diff --git a/activerecord/test/migrations/old_and_new_versions/20210716123013_add_people_number_of_legs.rb b/activerecord/test/migrations/old_and_new_versions/20210716123013_add_people_number_of_legs.rb new file mode 100644 index 00000000000..61fe81ee544 --- /dev/null +++ b/activerecord/test/migrations/old_and_new_versions/20210716123013_add_people_number_of_legs.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class AddPeopleNumberOfLegs < ActiveRecord::Migration::Current + add_column :people, :number_of_legs, :integer +end diff --git a/activerecord/test/migrations/old_and_new_versions/230_add_people_hobby.rb b/activerecord/test/migrations/old_and_new_versions/230_add_people_hobby.rb new file mode 100644 index 00000000000..4fc022ece77 --- /dev/null +++ b/activerecord/test/migrations/old_and_new_versions/230_add_people_hobby.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class AddPeopleHobby < ActiveRecord::Migration::Current + add_column :people, :hobby, :string +end diff --git a/activerecord/test/migrations/old_and_new_versions/231_add_people_last_name.rb b/activerecord/test/migrations/old_and_new_versions/231_add_people_last_name.rb new file mode 100644 index 00000000000..0bfa59f5b12 --- /dev/null +++ b/activerecord/test/migrations/old_and_new_versions/231_add_people_last_name.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class AddPeopleLastName < ActiveRecord::Migration::Current + add_column :people, :last_name, :string +end