Skip to content

Commit 1edb808

Browse files
authored
Merge pull request rails#53281 from jduff/schema_dumper_sort_columns
Sort table columns by name when dumping schema
2 parents 7acfb75 + e403adf commit 1edb808

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

activerecord/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
* The table columns inside `schema.rb` are now sorted alphabetically.
2+
3+
Previously they'd be sorted by creation order, which can cause merge conflicts when two
4+
branches modify the same table concurrently.
5+
6+
*John Duff*
7+
18
* Introduce versions formatter for the schema dumper.
29

310
It is now possible to override how schema dumper formats versions information inside the

activerecord/lib/active_record/schema_dumper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def table(table, stream)
192192
tbl.puts ", force: :cascade do |t|"
193193

194194
# then dump all non-primary key columns
195-
columns.each do |column|
195+
columns.sort_by(&:name).each do |column|
196196
raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" unless @connection.valid_type?(column.type)
197197
next if column.name == pk
198198

activerecord/test/cases/schema_dumper_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ def test_schema_dump_with_regexp_ignored_table
167167
assert_no_match %r{create_table "ar_internal_metadata"}, output
168168
end
169169

170+
def test_table_columns_sorted
171+
column_names = column_definition_lines(dump_table_schema("companies")).flatten.filter_map do |line|
172+
$1 if line !~ /t\.index/ && line.match(/t\..*"(\w+)"/)
173+
end
174+
175+
assert_equal %w[account_id client_of description firm_id firm_name name rating status type], column_names
176+
end
177+
170178
def test_schema_dumps_index_columns_in_right_order
171179
index_definition = dump_table_schema("companies").split(/\n/).grep(/t\.index.*company_index/).first.strip
172180
if current_adapter?(:Mysql2Adapter, :TrilogyAdapter)

0 commit comments

Comments
 (0)