Skip to content

Comments

fix: sort object names in multi-file dump for deterministic include order (#299)#301

Merged
tianzhou merged 1 commit intomainfrom
fix/issue-299-deterministic-multifile-include-order
Feb 20, 2026
Merged

fix: sort object names in multi-file dump for deterministic include order (#299)#301
tianzhou merged 1 commit intomainfrom
fix/issue-299-deterministic-multifile-include-order

Conversation

@tianzhou
Copy link
Contributor

Summary

  • pgschema dump --multi-file iterated a Go map (map[string][]diff.Diff) without sorting keys, causing \i include lines in main.sql to appear in random order across runs
  • Sort object names alphabetically within each directory group before generating include statements, ensuring deterministic output

Fixes #299

Test plan

  • Added TestMultiFileIncludeOrderDeterministic — creates 4 tables + 2 functions, runs FormatMultiFile 20 times, and verifies identical sorted output each time
  • Test fails before fix (non-deterministic map iteration detected), passes after
  • Full test suite passes (go test ./...)

🤖 Generated with Claude Code

…rder (#299)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings February 20, 2026 04:19
@greptile-apps
Copy link

greptile-apps bot commented Feb 20, 2026

Greptile Summary

Fixed non-deterministic include order in pgschema dump --multi-file by sorting object names alphabetically before generating \i include statements.

  • The root cause was Go map iteration order being non-deterministic when iterating map[string][]diff.Diff
  • Added sort.Strings() on extracted object names (lines 122-126 in formatter.go) before creating include files
  • Test creates 4 tables + 2 functions, runs FormatMultiFile 20 times, and verifies identical sorted output
  • Include order now follows: functions/alpha, functions/beta, tables/accounts, tables/orders, tables/products, tables/users (alphabetical within each directory group)

Confidence Score: 5/5

  • Safe to merge - simple, well-tested fix for deterministic output
  • The fix is minimal (5 lines), solves a well-defined problem, and includes comprehensive test coverage with 20 iterations. The change only affects output ordering, not functionality.
  • No files require special attention

Important Files Changed

Filename Overview
internal/dump/formatter.go Sorts object names alphabetically before generating include statements to ensure deterministic multi-file output
cmd/dump/multifile_test.go Adds comprehensive test with 20 iterations to verify deterministic include order for 4 tables and 2 functions

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[FormatMultiFile called] --> B[Group diffs by directory & object name]
    B --> C[filesByType map created]
    C --> D[Iterate orderedDirs]
    D --> E{objects exist for directory?}
    E -->|No| D
    E -->|Yes| F[Extract object names from map]
    F --> G[Sort object names alphabetically]
    G --> H[Iterate sorted names]
    H --> I[Write object file]
    I --> J[Add include statement]
    J --> K{More objects?}
    K -->|Yes| H
    K -->|No| L{More directories?}
    L -->|Yes| D
    L -->|No| M[Write main.sql with sorted includes]
    M --> N[Return success]
    
    style G fill:#90EE90
    style F fill:#FFB6C1
Loading

Last reviewed commit: 586030a

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes non-deterministic output in pgschema dump --multi-file caused by random Go map iteration order when generating include statements in main.sql.

Changes:

  • Sort object names alphabetically within each directory group before generating \i include lines in multi-file dumps
  • Add comprehensive test that verifies deterministic output across 20 runs with multiple tables and functions

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
internal/dump/formatter.go Extract map keys to slice, sort alphabetically, then iterate sorted slice to generate deterministic include statements
cmd/dump/multifile_test.go Add TestMultiFileIncludeOrderDeterministic to verify consistent sorted output across multiple runs

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@tianzhou tianzhou merged commit 9f9305c into main Feb 20, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dump --multi-file generates non-deterministic include order in main.sql

1 participant