Skip to content

Fix: Deterministic Referenced column sort to fix views referencing same table #903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 19, 2025

Conversation

saint-james-fr
Copy link
Contributor

What kind of change does this PR introduce?

Bug fix: more deterministic sort to provide consistent output of typescript type generation

What is the current behavior?

It's sorted by foreign_key_name, if they're the same then it's sorted by referenced_relation.

Pain

Generating new types, unexpected changes occur in the generated types file, even for columns that were not modified through migrations. This issue causes problems when comparing the generated types, as the differences seem random and inconsistent. Specifically, the changes appear within a view that references the same table twice, in two Common Table Expressions (CTEs) or subqueries.

How it affected me? Comparing generated types in a CI (so types are always up to date to the latest migrations) was failing for random reasons as the generated typings were not consistent.

Issue #831 on Supabase/CLI then transferred to supabase/postgres-meta and Issue supabase/postgres-meta#908 on supabase

This PR addresses the problem by adding an additional sorting criterion to ensure a stable order of referencedColumns.

What is the new behavior?

The new behavior ensures consistent and stable type generation. The sorting logic has been updated to ensure a deterministic order, preventing unintended changes in the generated types file.

It's sorted by foreign_key_name, if they're the same it's sorted by referenced_relation, then if they're the same it iterates over referenced_columns array to find the first difference and use it to sort and provide a deterministic order.

Finally if all columns are the same, it uses the length of the array to find a sorting order : i think this part could be improved and i'm open to any ways to improve it.

Additional context

To verify the fix, I created a view that references the same table twice through Common Table Expressions (CTEs). I also updated the test cases to ensure that the initial_id block always appears below the second_id block, with initial_id being sorted before second_id.

Adding this view also led me to update relevant Swift and Go generated type code.

@saint-james-fr saint-james-fr requested review from a team as code owners March 2, 2025 16:06
@coveralls
Copy link

coveralls commented Mar 19, 2025

Pull Request Test Coverage Report for Build 13958114957

Details

  • 8 of 8 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.03%) to 75.426%

Totals Coverage Status
Change from base Build 13882848159: 0.03%
Covered Lines: 4932
Relevant Lines: 6454

💛 - Coveralls

@soedirgo soedirgo force-pushed the referenced_column_sort_fix branch from 3160637 to 8d6b5a0 Compare March 19, 2025 22:42
Copy link
Member

@soedirgo soedirgo left a comment

Choose a reason for hiding this comment

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

Thanks @saint-james-fr!

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.

3 participants