Skip to content

Commit e496882

Browse files
EliSauderCopilot
andcommitted
fix(postgresql): keep a domain's NOT NULL apart from the column's own
A column took its domain's NOT NULL as its own, so changing its type kept it, and dropping the column's NOT NULL dropped the domain's too. Remember which one the column has, and work it out again when the column's type or constraint changes. Addresses review comment r4556992493 on #2. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 7f1c349 commit e496882

4 files changed

Lines changed: 41 additions & 15 deletions

File tree

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

Lines changed: 2 additions & 0 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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,11 @@ CREATE TABLE shelves (
2121
tags tags NOT NULL,
2222
tag_sets tags[] NOT NULL,
2323
label title,
24-
note text
24+
note text,
25+
retyped title,
26+
strict title NOT NULL
2527
);
28+
29+
-- The domain's NOT NULL leaves with it, and outlasts the column's own.
30+
ALTER TABLE shelves ALTER COLUMN retyped TYPE text;
31+
ALTER TABLE shelves ALTER COLUMN strict DROP NOT NULL;

‎internal/sql/catalog/table.go‎

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (c *Catalog) addColumn(table *Table, cmd *ast.AlterTableCmd) error {
5959
return nil
6060
}
6161

62-
func (table *Table) alterColumnType(cmd *ast.AlterTableCmd) error {
62+
func (c *Catalog) alterColumnType(table *Table, cmd *ast.AlterTableCmd) error {
6363
index, err := table.isExistColumn(cmd)
6464
if err != nil {
6565
return err
@@ -68,6 +68,7 @@ func (table *Table) alterColumnType(cmd *ast.AlterTableCmd) error {
6868
table.Columns[index].Type = *cmd.Def.TypeName
6969
table.Columns[index].IsArray = cmd.Def.IsArray
7070
table.Columns[index].ArrayDims = cmd.Def.ArrayDims
71+
c.setDomainNotNull(table.Columns[index])
7172
}
7273
return nil
7374
}
@@ -95,13 +96,15 @@ func (c *Catalog) dropColumn(table *Table, cmd *ast.AlterTableCmd) error {
9596
return nil
9697
}
9798

98-
func (table *Table) dropNotNull(cmd *ast.AlterTableCmd) error {
99+
func (c *Catalog) dropNotNull(table *Table, cmd *ast.AlterTableCmd) error {
99100
index, err := table.isExistColumn(cmd)
100101
if err != nil {
101102
return err
102103
}
103104
if index >= 0 {
104105
table.Columns[index].IsNotNull = false
106+
table.Columns[index].domainNotNull = false
107+
c.setDomainNotNull(table.Columns[index])
105108
}
106109
return nil
107110
}
@@ -113,6 +116,7 @@ func (table *Table) setNotNull(cmd *ast.AlterTableCmd) error {
113116
}
114117
if index >= 0 {
115118
table.Columns[index].IsNotNull = true
119+
table.Columns[index].domainNotNull = false
116120
}
117121
return nil
118122
}
@@ -136,6 +140,9 @@ type Column struct {
136140
TypeExpr *core.TypeExpr
137141

138142
linkedType bool
143+
// domainNotNull says IsNotNull comes from the column's domain alone,
144+
// so a change to the type or the domain takes it back.
145+
domainNotNull bool
139146
}
140147

141148
// An interface is used to resolve a circular import between the catalog and compiler packages.
@@ -206,15 +213,15 @@ func (c *Catalog) alterTable(stmt *ast.AlterTableStmt) error {
206213
return err
207214
}
208215
case ast.AT_AlterColumnType:
209-
if err := table.alterColumnType(cmd); err != nil {
216+
if err := c.alterColumnType(table, cmd); err != nil {
210217
return err
211218
}
212219
case ast.AT_DropColumn:
213220
if err := c.dropColumn(table, cmd); err != nil {
214221
return err
215222
}
216223
case ast.AT_DropNotNull:
217-
if err := table.dropNotNull(cmd); err != nil {
224+
if err := c.dropNotNull(table, cmd); err != nil {
218225
return err
219226
}
220227
case ast.AT_SetNotNull:
@@ -357,15 +364,7 @@ func (c *Catalog) defineColumn(table *ast.TableName, col *ast.ColumnDef) (*Colum
357364
Comment: col.Comment,
358365
Length: col.Length,
359366
}
360-
// A domain declared NOT NULL forbids NULL in every column of it. An
361-
// array of one may still be NULL itself.
362-
if !col.IsArray && col.TypeName != nil {
363-
if typ, _, err := c.getType(col.TypeName); err == nil {
364-
if d, ok := typ.(*Domain); ok && d.NotNull {
365-
tc.IsNotNull = true
366-
}
367-
}
368-
}
367+
c.setDomainNotNull(tc)
369368
if col.Vals != nil {
370369
typeName := ast.TypeName{
371370
Name: fmt.Sprintf("%s_%s", table.Name, col.Colname),
@@ -380,6 +379,23 @@ func (c *Catalog) defineColumn(table *ast.TableName, col *ast.ColumnDef) (*Colum
380379
return tc, nil
381380
}
382381

382+
// setDomainNotNull makes col NOT NULL when its type is a domain declared
383+
// NOT NULL, which forbids NULL in every column of it, and drops what an
384+
// earlier type gave it. An array of one may still be NULL itself.
385+
func (c *Catalog) setDomainNotNull(col *Column) {
386+
if col.domainNotNull {
387+
col.IsNotNull, col.domainNotNull = false, false
388+
}
389+
if col.IsNotNull || col.IsArray {
390+
return
391+
}
392+
if typ, _, err := c.getType(&col.Type); err == nil {
393+
if d, ok := typ.(*Domain); ok && d.NotNull {
394+
col.IsNotNull, col.domainNotNull = true, true
395+
}
396+
}
397+
}
398+
383399
func (c *Catalog) dropTable(stmt *ast.DropTableStmt) error {
384400
for _, name := range stmt.Tables {
385401
ns := name.Schema

0 commit comments

Comments
 (0)