Skip to content

Conversation

@mistermoe
Copy link
Contributor

closes #254

split out from #255

Problem

@alecthomas explained the problem quite well in #254 so I won't repeat that here.

Fix

Extended the existing function dependency handling pattern to cover domains. The codebase already splits tables into those with/without function dependencies and creates them at appropriate points relative to function creation. This change applies the same approach to domains:

  1. Domain ordering: Domains with CHECK constraints referencing functions are deferred until after functions are created
  2. Table ordering: Tables using deferred domains are also deferred until after those domains exist

Domain constraint expression normalization

While testing the ordering fix, discovered that PostgreSQL always returns fully-qualified function names in constraint definitions (e.g., public.validate_id(VALUE)), but source SQL typically uses unqualified names. This caused constraint definitions to not match on re-comparison, producing spurious diffs.

Fix: Strip same-schema prefixes from function calls in domain CHECK constraints, matching the existing pattern in normalizePolicyExpression (added for Issue #220). This ensures consistent comparison regardless of whether the source SQL includes schema qualification.

Alternative considered: Normalize expressions to always use fully-qualified names — would require adding schema prefixes to source expressions.

Copilot AI review requested due to automatic review settings January 27, 2026 17:09
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 migration ordering and comparison for PostgreSQL domains whose CHECK constraints depend on functions, ensuring both domains and tables using them are created in a dependency-safe order and compared without spurious diffs.

Changes:

  • Extend generateCreateSQL to treat domains with function dependencies similarly to function-dependent tables: create functions first, then dependent domains, then tables that use those domains.
  • Add domain-aware dependency helpers (domainReferencesNewFunction, tableUsesDeferredDomain) and wire them into the create ordering.
  • Normalize domain CHECK constraint expressions to strip same-schema function qualifiers, and add regression test fixtures for function-dependent domains and domain-using tables.

Reviewed changes

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

Show a summary per file
File Description
testdata/diff/create_domain/domain_function_table_dependency/plan.txt Asserts plan summary and execution order for function → domain → table chain, verifying correct creation sequencing.
testdata/diff/create_domain/domain_function_table_dependency/plan.sql Captures expected SQL ordering: create function, then domain with CHECK, then table using that domain.
testdata/diff/create_domain/domain_function_table_dependency/plan.json Encodes the same scenario in JSON plan form, exercising step typing and ordering for function, domain, and table.
testdata/diff/create_domain/domain_function_table_dependency/old.sql Defines the empty starting schema for the function → domain → table dependency test.
testdata/diff/create_domain/domain_function_table_dependency/new.sql Defines desired schema with a validating function, a domain using it in CHECK, and a table using the domain.
testdata/diff/create_domain/domain_function_table_dependency/diff.sql Expected migration SQL used by diff tests to validate the correct statement ordering.
testdata/diff/create_domain/domain_function_check_dependency/plan.txt Asserts plan summary and ordering for a function-dependent domain without tables, ensuring domains wait for functions.
testdata/diff/create_domain/domain_function_check_dependency/plan.sql Expected SQL for creating the validating function and dependent domain in correct order.
testdata/diff/create_domain/domain_function_check_dependency/plan.json JSON representation of the function + domain plan, verifying step metadata for domains with function CHECKs.
testdata/diff/create_domain/domain_function_check_dependency/old.sql Empty starting schema for the function-dependent domain-only test case.
testdata/diff/create_domain/domain_function_check_dependency/new.sql Desired schema with a validating function and a domain whose CHECK constraint calls it.
testdata/diff/create_domain/domain_function_check_dependency/diff.sql Expected migration SQL for the function + domain test used by file-based diff tests.
ir/normalize.go Enhances domain normalization to accept the domain schema and strip same-schema function qualifiers in CHECK expressions, mirroring policy expression normalization and preventing spurious diffs.
internal/diff/diff.go Adjusts create-time ordering to (a) partition types into domains with/without new-function dependencies, (b) track deferred domains, (c) classify tables by function/deferred-domain usage, and (d) introduce tableUsesDeferredDomain and domainReferencesNewFunction helpers used by generateCreateSQL.

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

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

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


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

Copy link
Contributor

@tianzhou tianzhou left a comment

Choose a reason for hiding this comment

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

LGTM

@tianzhou tianzhou merged commit dbc67f6 into pgplex:main Jan 27, 2026
12 of 13 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.

Invalid statement ordering with CREATE DOMAIN CHECK constraints with function references

2 participants