Skip to content

Commit 24f8566

Browse files
authored
catalog: Improve alter column type (#818)
* catalog: Improve alter column type This commit adds support for altering a column type into an array. It adds a new test to ensure that the generated code is valid. * catalog: remove unecessary type annotation * ast: Add a String method to AlterTableType
1 parent f8f6cf1 commit 24f8566

File tree

9 files changed

+92
-4
lines changed

9 files changed

+92
-4
lines changed

internal/endtoend/testdata/ddl_alter_table_alter_type/postgresql/go/db.go

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

internal/endtoend/testdata/ddl_alter_table_alter_type/postgresql/go/models.go

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

internal/endtoend/testdata/ddl_alter_table_alter_type/postgresql/go/query.sql.go

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: Placeholder :exec
2+
SELECT 1;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE TABLE foo (bar TEXT);
2+
ALTER TABLE foo ALTER bar TYPE TEXT ARRAY;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "postgresql",
7+
"name": "querytest",
8+
"schema": "schema.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/sql/ast/alter_table_cmd.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package ast
22

3-
type AlterTableType int
4-
53
const (
64
AT_AddColumn AlterTableType = iota
75
AT_AlterColumnType
@@ -10,6 +8,25 @@ const (
108
AT_SetNotNull
119
)
1210

11+
type AlterTableType int
12+
13+
func (t AlterTableType) String() string {
14+
switch t {
15+
case AT_AddColumn:
16+
return "AddColumn"
17+
case AT_AlterColumnType:
18+
return "AlterColumnType"
19+
case AT_DropColumn:
20+
return "DropColumn"
21+
case AT_DropNotNull:
22+
return "DropNotNull"
23+
case AT_SetNotNull:
24+
return "SetNotNull"
25+
default:
26+
return "Unknown"
27+
}
28+
}
29+
1330
type AlterTableCmd struct {
1431
Subtype AlterTableType
1532
Name *string

internal/sql/catalog/catalog.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func New(def string) *Catalog {
258258
return &Catalog{
259259
DefaultSchema: def,
260260
Schemas: []*Schema{
261-
&Schema{Name: def},
261+
{Name: def},
262262
},
263263
Extensions: map[string]struct{}{},
264264
}

internal/sql/catalog/table.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (c *Catalog) alterTable(stmt *ast.AlterTableStmt) error {
7878

7979
case ast.AT_AlterColumnType:
8080
table.Columns[idx].Type = *cmd.Def.TypeName
81-
// table.Columns[idx].IsArray = isArray(d.TypeName)
81+
table.Columns[idx].IsArray = cmd.Def.IsArray
8282

8383
case ast.AT_DropColumn:
8484
table.Columns = append(table.Columns[:idx], table.Columns[idx+1:]...)

0 commit comments

Comments
 (0)