Fix: Deterministic Referenced column sort to fix views referencing same table #903
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.