feat: support domains - #309
Open
iambilotta wants to merge 1 commit into
Open
iambilotta wants to merge 1 commit into
iambilotta wants to merge 1 commit into
Conversation
Domains are fetched from pg_type (base type, default, NOT NULL and their CHECK constraints) and diffed like enums: created before anything that can use them, altered in place (default, NOT NULL, constraints dropped and re-added by name), dropped after the last column that used them. Changing the base type is reported as not implemented, since a domain in use cannot be dropped and re-created and Postgres has no ALTER DOMAIN ... TYPE. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Adds domains to the schema model and to the diff.
GetDomainsreadspg_typerows withtyptype = 'd': base type (format_typewith the typmod), default (pg_get_exprontypdefaultbin),NOT NULL, and theCHECKconstraints (pg_get_constraintdef, sorted by name).NOT NULLis itself a constraint on Postgres 17+, so onlycontype = 'c'is read;typnotnullalready carries it. Extension-owned domains are excluded, as for enums.schema.Domain(BaseType,Default,NotNull,Constraints) is part ofSchemaand of its hash;Normalizesorts domains and their constraints.domainSQLGeneratormirrors the enum generator:CREATE DOMAIN … [DEFAULT …] [NOT NULL] CONSTRAINT … CHECK …before anything that can use the domain,DROP DOMAINafter the last column that did, and in betweenALTER DOMAINfor the default,NOT NULL, and constraints (a changed constraint is dropped and re-added under its name). Changing the base type returnsErrNotImplemented: a domain in use cannot be dropped and re-created, and Postgres has noALTER DOMAIN … TYPE.format_type, so a column moving fromtextwith aCHECKto a domain plans asALTER COLUMN … SET DATA TYPEplus the constraint drop, and validates.Motivation
Without domains in the model, plan validation fails as soon as a column takes a domain type (
type "…" does not existin the temporary database), which makes declarative schemas that useCREATE DOMAINfor value objects unusable with validation on. Found on a real catalog with 68 domains over 170 columns.Testing
domain_cases_test.go: no-op; domain and table created in the same plan; domain dropped with its table; constraints, default andNOT NULLadded, changed and dropped; a column moving from a checkedtextto a domain while the old domain is dropped; a base type change reported as not implemented. The schema fixture ininternal/schema/schema_test.gogains a domain (hashes updated). Run with the Docker test runner on Postgres 17;make lintandmake code_genin the repository's own images.🤖 Generated with Claude Code
https://claude.ai/code/session_014TKmA55WMHjNEb6DmYZfJX