Skip to content

Commit bc5a65b

Browse files
EliSauderCopilot
andcommitted
feat(postgresql): apply ALTER DOMAIN SET and DROP NOT NULL
The catalog ignored ALTER DOMAIN, so a domain kept the NOT NULL it was created with, and so did its columns. Apply the change to the domain and work out each column's again, leaving a column's own NOT NULL be. Addresses review comment r4556992586 on #2. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent e496882 commit bc5a65b

5 files changed

Lines changed: 68 additions & 16 deletions

File tree

‎internal/endtoend/testdata/domain_types/postgresql/go/models.go‎

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/endtoend/testdata/domain_types/postgresql/go/query.sql.go‎

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/endtoend/testdata/domain_types/postgresql/schema.sql‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ CREATE SCHEMA billing;
55
CREATE DOMAIN billing.ksuid AS char(27);
66
-- Named like a built-in, but off the search path.
77
CREATE DOMAIN billing.text AS integer;
8+
CREATE DOMAIN relaxed AS text NOT NULL;
9+
CREATE DOMAIN tightened AS text;
810
CREATE DOMAIN dropped AS text;
911
DROP DOMAIN dropped;
1012

@@ -23,9 +25,14 @@ CREATE TABLE shelves (
2325
label title,
2426
note text,
2527
retyped title,
26-
strict title NOT NULL
28+
strict title NOT NULL,
29+
relaxed relaxed,
30+
tightened tightened
2731
);
2832

2933
-- The domain's NOT NULL leaves with it, and outlasts the column's own.
3034
ALTER TABLE shelves ALTER COLUMN retyped TYPE text;
3135
ALTER TABLE shelves ALTER COLUMN strict DROP NOT NULL;
36+
37+
ALTER DOMAIN relaxed DROP NOT NULL;
38+
ALTER DOMAIN tightened SET NOT NULL;

‎internal/sql/catalog/catalog.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ func (c *Catalog) Update(stmt ast.Statement, colGen columnGenerator) error {
5858
case *ast.AlterTypeAddValueStmt:
5959
err = c.alterTypeAddValue(n)
6060

61+
case *ast.AlterDomainStmt:
62+
err = c.alterDomain(n)
63+
6164
case *ast.AlterTypeRenameValueStmt:
6265
err = c.alterTypeRenameValue(n)
6366

‎internal/sql/catalog/types.go‎

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,10 @@ func (c *Catalog) createEnum(stmt *ast.CreateEnumStmt) error {
109109
}
110110

111111
func (c *Catalog) createDomain(stmt *ast.CreateDomainStmt) error {
112-
names := stringSlice(stmt.Domainname)
113-
if len(names) == 0 || stmt.TypeName == nil {
112+
name := domainName(stmt.Domainname)
113+
if name == nil || stmt.TypeName == nil {
114114
return fmt.Errorf("create domain: missing name or type")
115115
}
116-
name := &ast.TypeName{Name: names[len(names)-1]}
117-
if len(names) > 1 {
118-
name.Schema = names[len(names)-2]
119-
}
120116
ns := name.Schema
121117
if ns == "" {
122118
ns = c.DefaultSchema
@@ -143,6 +139,48 @@ func (c *Catalog) createDomain(stmt *ast.CreateDomainStmt) error {
143139
return nil
144140
}
145141

142+
func domainName(list *ast.List) *ast.TypeName {
143+
names := stringSlice(list)
144+
if len(names) == 0 {
145+
return nil
146+
}
147+
name := &ast.TypeName{Name: names[len(names)-1]}
148+
if len(names) > 1 {
149+
name.Schema = names[len(names)-2]
150+
}
151+
return name
152+
}
153+
154+
// alterDomain applies the one change to a domain codegen sees, its NOT
155+
// NULL, to the domain and every column of it.
156+
func (c *Catalog) alterDomain(stmt *ast.AlterDomainStmt) error {
157+
// PostgreSQL's subtypes: O sets NOT NULL, N drops it.
158+
if stmt.Subtype != 'O' && stmt.Subtype != 'N' {
159+
return nil
160+
}
161+
name := domainName(stmt.TypeName)
162+
if name == nil {
163+
return fmt.Errorf("alter domain: missing name")
164+
}
165+
typ, _, err := c.getType(name)
166+
if err != nil {
167+
return err
168+
}
169+
d, ok := typ.(*Domain)
170+
if !ok {
171+
return fmt.Errorf("%q is not a domain", name.Name)
172+
}
173+
d.NotNull = stmt.Subtype == 'O'
174+
for _, schema := range c.Schemas {
175+
for _, table := range schema.Tables {
176+
for _, col := range table.Columns {
177+
c.setDomainNotNull(col)
178+
}
179+
}
180+
}
181+
return nil
182+
}
183+
146184
func stringSlice(list *ast.List) []string {
147185
items := []string{}
148186
for _, item := range list.Items {

0 commit comments

Comments
 (0)