Skip to content

Commit e357cf3

Browse files
authored
sql/catalog: Port tests over from catalog pkg (#402)
Rename the sql/errors package to sql/sqlerr Remove spew from PostgreSQL parser Fix table renaming bug
1 parent 40b05bd commit e357cf3

File tree

9 files changed

+177
-16
lines changed

9 files changed

+177
-16
lines changed

internal/postgresql/catalog_test.go

+161
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package postgresql
22

33
import (
4+
"errors"
45
"strconv"
56
"strings"
67
"testing"
78

89
"github.com/kyleconroy/sqlc/internal/sql/ast"
910
"github.com/kyleconroy/sqlc/internal/sql/catalog"
11+
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
1012

1113
"github.com/google/go-cmp/cmp"
1214
"github.com/google/go-cmp/cmp/cmpopts"
@@ -527,3 +529,162 @@ func TestUpdate(t *testing.T) {
527529
})
528530
}
529531
}
532+
533+
func TestUpdateErrors(t *testing.T) {
534+
p := NewParser()
535+
536+
for i, tc := range []struct {
537+
stmt string
538+
err *sqlerr.Error
539+
}{
540+
{
541+
`
542+
CREATE TABLE foo ();
543+
CREATE TABLE foo ();
544+
`,
545+
sqlerr.RelationExists("foo"),
546+
},
547+
{
548+
`
549+
CREATE TYPE foo AS ENUM ('bar');
550+
CREATE TYPE foo AS ENUM ('bar');
551+
`,
552+
sqlerr.TypeExists("foo"),
553+
},
554+
{
555+
`
556+
DROP TABLE foo;
557+
`,
558+
sqlerr.RelationNotFound("foo"),
559+
},
560+
{
561+
`
562+
DROP TYPE foo;
563+
`,
564+
sqlerr.TypeNotFound("foo"),
565+
},
566+
{
567+
`
568+
CREATE TABLE foo ();
569+
CREATE TABLE bar ();
570+
ALTER TABLE foo RENAME TO bar;
571+
`,
572+
sqlerr.RelationExists("bar"),
573+
},
574+
{
575+
`
576+
ALTER TABLE foo RENAME TO bar;
577+
`,
578+
sqlerr.RelationNotFound("foo"),
579+
},
580+
{
581+
`
582+
CREATE TABLE foo ();
583+
ALTER TABLE foo ADD COLUMN bar text;
584+
ALTER TABLE foo ADD COLUMN bar text;
585+
`,
586+
sqlerr.ColumnExists("foo", "bar"),
587+
},
588+
{
589+
`
590+
CREATE TABLE foo ();
591+
ALTER TABLE foo DROP COLUMN bar;
592+
`,
593+
sqlerr.ColumnNotFound("foo", "bar"),
594+
},
595+
{
596+
`
597+
CREATE TABLE foo ();
598+
ALTER TABLE foo ALTER COLUMN bar SET NOT NULL;
599+
`,
600+
sqlerr.ColumnNotFound("foo", "bar"),
601+
},
602+
{
603+
`
604+
CREATE TABLE foo ();
605+
ALTER TABLE foo ALTER COLUMN bar DROP NOT NULL;
606+
`,
607+
sqlerr.ColumnNotFound("foo", "bar"),
608+
},
609+
{
610+
`
611+
CREATE SCHEMA foo;
612+
CREATE SCHEMA foo;
613+
`,
614+
sqlerr.SchemaExists("foo"),
615+
},
616+
{
617+
`
618+
ALTER TABLE foo.baz SET SCHEMA bar;
619+
`,
620+
sqlerr.SchemaNotFound("foo"),
621+
},
622+
{
623+
`
624+
CREATE SCHEMA foo;
625+
ALTER TABLE foo.baz SET SCHEMA bar;
626+
`,
627+
sqlerr.RelationNotFound("baz"),
628+
},
629+
{
630+
`
631+
CREATE SCHEMA foo;
632+
CREATE TABLE foo.baz ();
633+
ALTER TABLE foo.baz SET SCHEMA bar;
634+
`,
635+
sqlerr.SchemaNotFound("bar"),
636+
},
637+
{
638+
`
639+
DROP SCHEMA bar;
640+
`,
641+
sqlerr.SchemaNotFound("bar"),
642+
},
643+
{
644+
`
645+
ALTER TABLE foo RENAME bar TO baz;
646+
`,
647+
sqlerr.RelationNotFound("foo"),
648+
},
649+
{
650+
`
651+
CREATE TABLE foo ();
652+
ALTER TABLE foo RENAME bar TO baz;
653+
`,
654+
sqlerr.ColumnNotFound("foo", "bar"),
655+
},
656+
{
657+
`
658+
CREATE TABLE foo (bar text, baz text);
659+
ALTER TABLE foo RENAME bar TO baz;
660+
`,
661+
sqlerr.ColumnExists("foo", "baz"),
662+
},
663+
} {
664+
test := tc
665+
t.Run(strconv.Itoa(i), func(t *testing.T) {
666+
stmts, err := p.Parse(strings.NewReader(test.stmt))
667+
if err != nil {
668+
t.Log(test.stmt)
669+
t.Fatal(err)
670+
}
671+
672+
c := NewCatalog()
673+
err = c.Build(stmts)
674+
if err == nil {
675+
t.Log(test.stmt)
676+
t.Fatal("err was nil")
677+
}
678+
679+
var actual *sqlerr.Error
680+
if !errors.As(err, &actual) {
681+
t.Fatalf("err is not *sqlerr.Error: %#v", err)
682+
}
683+
684+
if diff := cmp.Diff(test.err.Error(), actual.Error()); diff != "" {
685+
t.Log(test.stmt)
686+
t.Errorf("error mismatch: \n%s", diff)
687+
}
688+
})
689+
}
690+
}

internal/postgresql/parse.go

-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/kyleconroy/sqlc/internal/sql/ast"
1111

12-
"github.com/davecgh/go-spew/spew"
1312
pg "github.com/lfittl/pg_query_go"
1413
nodes "github.com/lfittl/pg_query_go/nodes"
1514
)
@@ -403,7 +402,6 @@ func translate(node nodes.Node) (ast.Node, error) {
403402
switch n.RemoveType {
404403

405404
case nodes.OBJECT_FUNCTION:
406-
spew.Dump(n)
407405
drop := &ast.DropFunctionStmt{
408406
MissingOk: n.MissingOk,
409407
}

internal/sql/catalog/catalog.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package catalog
22

33
import (
44
"github.com/kyleconroy/sqlc/internal/sql/ast"
5-
sqlerr "github.com/kyleconroy/sqlc/internal/sql/errors"
5+
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
66
)
77

88
func stringSlice(list *ast.List) []string {

internal/sql/catalog/comment_on.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package catalog
22

33
import (
44
"github.com/kyleconroy/sqlc/internal/sql/ast"
5-
"github.com/kyleconroy/sqlc/internal/sql/errors"
5+
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
66
)
77

88
func (c *Catalog) commentOnColumn(stmt *ast.CommentOnColumnStmt) error {
@@ -20,7 +20,7 @@ func (c *Catalog) commentOnColumn(stmt *ast.CommentOnColumnStmt) error {
2020
return nil
2121
}
2222
}
23-
return errors.ColumnNotFound(stmt.Table.Name, stmt.Col.Name)
23+
return sqlerr.ColumnNotFound(stmt.Table.Name, stmt.Col.Name)
2424
}
2525

2626
func (c *Catalog) commentOnSchema(stmt *ast.CommentOnSchemaStmt) error {

internal/sql/catalog/func.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"errors"
55

66
"github.com/kyleconroy/sqlc/internal/sql/ast"
7-
sqlerr "github.com/kyleconroy/sqlc/internal/sql/errors"
7+
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
88
)
99

1010
func (c *Catalog) createFunction(stmt *ast.CreateFunctionStmt) error {

internal/sql/catalog/schema.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55

66
"github.com/kyleconroy/sqlc/internal/sql/ast"
7-
sqlerr "github.com/kyleconroy/sqlc/internal/sql/errors"
7+
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
88
)
99

1010
func (c *Catalog) createSchema(stmt *ast.CreateSchemaStmt) error {

internal/sql/catalog/table.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"errors"
55

66
"github.com/kyleconroy/sqlc/internal/sql/ast"
7-
sqlerr "github.com/kyleconroy/sqlc/internal/sql/errors"
7+
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
88
)
99

1010
func (c *Catalog) alterTable(stmt *ast.AlterTableStmt) error {
@@ -128,12 +128,11 @@ func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error {
128128
if err != nil {
129129
return err
130130
}
131-
if _, _, err := schema.getTable(stmt.Name); err != nil {
132-
if !errors.Is(err, sqlerr.NotFound) {
133-
return err
134-
}
135-
} else if stmt.IfNotExists {
131+
_, _, err = schema.getTable(stmt.Name)
132+
if err == nil && stmt.IfNotExists {
136133
return nil
134+
} else if err == nil {
135+
return sqlerr.RelationExists(stmt.Name.Name)
137136
}
138137
tbl := Table{Rel: stmt.Name}
139138
for _, col := range stmt.Cols {
@@ -194,10 +193,13 @@ func (c *Catalog) renameColumn(stmt *ast.RenameColumnStmt) error {
194193
}
195194

196195
func (c *Catalog) renameTable(stmt *ast.RenameTableStmt) error {
197-
_, tbl, err := c.getTable(stmt.Table)
196+
sch, tbl, err := c.getTable(stmt.Table)
198197
if err != nil {
199198
return err
200199
}
200+
if _, _, err := sch.getTable(&ast.TableName{Name: *stmt.NewName}); err == nil {
201+
return sqlerr.RelationExists(*stmt.NewName)
202+
}
201203
if stmt.NewName != nil {
202204
tbl.Rel.Name = *stmt.NewName
203205
}

internal/sql/catalog/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"errors"
55

66
"github.com/kyleconroy/sqlc/internal/sql/ast"
7-
sqlerr "github.com/kyleconroy/sqlc/internal/sql/errors"
7+
"github.com/kyleconroy/sqlc/internal/sql/sqlerr"
88
)
99

1010
func (c *Catalog) createEnum(stmt *ast.CreateEnumStmt) error {

internal/sql/errors/errors.go renamed to internal/sql/sqlerr/errors.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package errors
1+
package sqlerr
22

33
import (
44
"errors"

0 commit comments

Comments
 (0)