Skip to content

Commit be6c673

Browse files
Support PostgreSQL CREATE TABLE LIKE (#2924)
* Support PostgreSQL CREATE TABLE LIKE * test: Add test case for CREATE TABLE LIKE --------- Co-authored-by: Kyle Conroy <kyle@sqlc.dev>
1 parent 9cd9139 commit be6c673

File tree

8 files changed

+119
-18
lines changed

8 files changed

+119
-18
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://github.com/sqlc-dev/sqlc/issues/481

internal/endtoend/testdata/ddl_create_table_like/postgresql/pgx/go/db.go

Lines changed: 32 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_create_table_like/postgresql/pgx/go/models.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.

internal/endtoend/testdata/ddl_create_table_like/postgresql/pgx/go/query.sql.go

Lines changed: 34 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: AllRanked :many
2+
SELECT * FROM changes_ranked;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE TABLE changes (
2+
ranked INT NOT NULL
3+
);
4+
5+
CREATE TABLE changes_ranked (
6+
LIKE changes INCLUDING ALL,
7+
rank_by_effect_size INT NOT NULL,
8+
rank_by_abs_percent_change INT NOT NULL
9+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "2"
2+
sql:
3+
- engine: "postgresql"
4+
schema: "schema.sql"
5+
queries: "query.sql"
6+
gen:
7+
go:
8+
package: "querytest"
9+
out: "go"
10+
sql_package: "pgx/v5"

internal/sql/catalog/table.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,6 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
289289
}
290290
}
291291

292-
if stmt.ReferTable != nil && len(stmt.Cols) != 0 {
293-
return errors.New("create table node cannot have both a ReferTable and Cols")
294-
}
295-
296292
if stmt.ReferTable != nil {
297293
_, original, err := c.getTable(stmt.ReferTable)
298294
if err != nil {
@@ -302,23 +298,23 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
302298
newCol := *col // make a copy, so changes to the ReferTable don't propagate
303299
tbl.Columns = append(tbl.Columns, &newCol)
304300
}
305-
} else {
306-
for _, col := range stmt.Cols {
307-
if notNull, ok := seen[col.Colname]; ok {
308-
seen[col.Colname] = notNull || col.IsNotNull
309-
if a, ok := coltype[col.Colname]; ok {
310-
if !sameType(&a, col.TypeName) {
311-
return fmt.Errorf("column %q has a type conflict", col.Colname)
312-
}
301+
}
302+
303+
for _, col := range stmt.Cols {
304+
if notNull, ok := seen[col.Colname]; ok {
305+
seen[col.Colname] = notNull || col.IsNotNull
306+
if a, ok := coltype[col.Colname]; ok {
307+
if !sameType(&a, col.TypeName) {
308+
return fmt.Errorf("column %q has a type conflict", col.Colname)
313309
}
314-
continue
315310
}
316-
tc, err := c.defineColumn(stmt.Name, col)
317-
if err != nil {
318-
return err
319-
}
320-
tbl.Columns = append(tbl.Columns, tc)
311+
continue
312+
}
313+
tc, err := c.defineColumn(stmt.Name, col)
314+
if err != nil {
315+
return err
321316
}
317+
tbl.Columns = append(tbl.Columns, tc)
322318
}
323319

324320
// If one of the merged columns was not null, mark the column as not null

0 commit comments

Comments
 (0)