diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 753b800eef..63b43ace4f 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,13 @@ +* Reversed the order of `INSERT` statements in `structure.sql` dumps + + This should decrease the likelihood of merge conflicts. New migrations + will now be added at the top of the list. + + For existing apps, there will be a large diff the next time `structure.sql` + is generated. + + *Alex Ghiculescu*, *Matt Larraz* + * Fix PG.connect keyword arguments deprecation warning on ruby 2.7 Fixes #44307. diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index 363bd48e4b..bbf39f9130 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -1675,7 +1675,7 @@ module ActiveRecord if versions.is_a?(Array) sql = +"INSERT INTO #{sm_table} (version) VALUES\n" - sql << versions.map { |v| "(#{quote(v)})" }.join(",\n") + sql << versions.reverse.map { |v| "(#{quote(v)})" }.join(",\n") sql << ";\n\n" sql else diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 30b0c5f321..235749c707 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -25,15 +25,21 @@ class SchemaDumperTest < ActiveRecord::TestCase assert_no_match(/INSERT INTO/, schema_info) end - def test_dump_schema_information_outputs_lexically_ordered_versions + def test_dump_schema_information_outputs_lexically_reverse_ordered_versions_regardless_of_database_order versions = %w{ 20100101010101 20100201010101 20100301010101 } - versions.reverse_each do |v| + versions.shuffle.each do |v| ActiveRecord::SchemaMigration.create!(version: v) end schema_info = ActiveRecord::Base.connection.dump_schema_information - assert_match(/20100201010101.*20100301010101/m, schema_info) - assert_includes schema_info, "20100101010101" + expected = <<~STR + INSERT INTO #{ActiveRecord::Base.connection.quote_table_name("schema_migrations")} (version) VALUES + ('20100301010101'), + ('20100201010101'), + ('20100101010101'); + + STR + assert_equal expected, schema_info ensure ActiveRecord::SchemaMigration.delete_all end