Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/activerecord-clean-db-structure/clean_dump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,11 @@ def order_column_definitions(source)
elsif source_line.start_with?(")")
if inside_table
inside_table = false
columns.sort_by!(&:first)

# Every inline CHECK constraint parses to the same name ("CONSTRAINT"), and
# Ruby's sort is not stable, so tied lines get reordered differently depending
# on the platform's qsort. Breaking ties by index keeps pg_dump's own ordering.
columns = columns.sort_by.with_index { |(name, _line), index| [name, index] }

columns[0..-2].each do |_, line|
result << with_column_separator[line]
Expand Down
11 changes: 11 additions & 0 deletions test/clean_dump_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ def test_order_column_definitions
assert_cleans_dump "data/input.sql", "expectations/order_column_definitions.sql", order_column_definitions: true
end

def test_order_column_definitions_keeps_the_order_of_inline_constraints
names = %w[alpha bravo charlie delta echo foxtrot golf hotel india juliet kilo lima]
constraints = names.map { |name| " CONSTRAINT things_#{name}_check CHECK ((#{name} > 0)),\n" }
source = "CREATE TABLE public.things (\n id bigint NOT NULL,\n#{constraints.join})\n"

cleaner = ActiveRecordCleanDbStructure::CleanDump.new(+"", order_column_definitions: true)
dump = cleaner.order_column_definitions(source)

assert_equal names, dump.scan(/things_(\w+)_check/).flatten
end

def test_order_schema_migrations_values
assert_cleans_dump "data/input.sql", "expectations/order_schema_migrations_values.sql", order_schema_migrations_values: true
end
Expand Down
Loading