123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- package sqlite
- import (
- "strconv"
- "strings"
- "testing"
- "github.com/kyleconroy/sqlc/internal/sql/ast"
- "github.com/kyleconroy/sqlc/internal/sql/catalog"
- "github.com/google/go-cmp/cmp"
- "github.com/google/go-cmp/cmp/cmpopts"
- )
- func TestUpdate(t *testing.T) {
- p := NewParser()
- for i, tc := range []struct {
- stmt string
- s *catalog.Schema
- }{
- {
- `
- CREATE TABLE foo (bar text);
- `,
- &catalog.Schema{
- Name: "main",
- Tables: []*catalog.Table{
- {
- Rel: &ast.TableName{Name: "foo"},
- Columns: []*catalog.Column{
- {
- Name: "bar",
- Type: ast.TypeName{Name: "text"},
- },
- },
- },
- },
- },
- },
- {
- `
- CREATE TABLE foo (bar text);
- ALTER TABLE foo RENAME TO baz;
- `,
- &catalog.Schema{
- Name: "main",
- Tables: []*catalog.Table{
- {
- Rel: &ast.TableName{Name: "baz"},
- Columns: []*catalog.Column{
- {
- Name: "bar",
- Type: ast.TypeName{Name: "text"},
- },
- },
- },
- },
- },
- },
- {
- `
- CREATE TABLE foo (bar text);
- ALTER TABLE foo ADD COLUMN baz bool;
- `,
- &catalog.Schema{
- Name: "main",
- Tables: []*catalog.Table{
- {
- Rel: &ast.TableName{Name: "foo"},
- Columns: []*catalog.Column{
- {
- Name: "bar",
- Type: ast.TypeName{Name: "text"},
- },
- {
- Name: "baz",
- Type: ast.TypeName{Name: "bool"},
- },
- },
- },
- },
- },
- },
- {
- `
- CREATE TABLE foo (bar text);
- ALTER TABLE foo RENAME COLUMN bar TO baz;
- `,
- &catalog.Schema{
- Name: "main",
- Tables: []*catalog.Table{
- {
- Rel: &ast.TableName{Name: "foo"},
- Columns: []*catalog.Column{
- {
- Name: "baz",
- Type: ast.TypeName{Name: "text"},
- },
- },
- },
- },
- },
- },
- {
- `
- CREATE TABLE foo (bar text);
- ALTER TABLE foo RENAME bar TO baz;
- `,
- &catalog.Schema{
- Name: "main",
- Tables: []*catalog.Table{
- {
- Rel: &ast.TableName{Name: "foo"},
- Columns: []*catalog.Column{
- {
- Name: "baz",
- Type: ast.TypeName{Name: "text"},
- },
- },
- },
- },
- },
- },
- {
- `
- ATTACH ':memory:' as ns;
- CREATE TABLE ns.foo (bar text);
- `,
- &catalog.Schema{
- Name: "ns",
- Tables: []*catalog.Table{
- {
- Rel: &ast.TableName{Schema: "ns", Name: "foo"},
- Columns: []*catalog.Column{
- {
- Name: "bar",
- Type: ast.TypeName{Name: "text"},
- },
- },
- },
- },
- },
- },
- {
- `
- ATTACH ':memory:' as ns;
- CREATE TABLE ns.foo (bar text);
- ALTER TABLE ns.foo RENAME TO baz;
- `,
- &catalog.Schema{
- Name: "ns",
- Tables: []*catalog.Table{
- {
- Rel: &ast.TableName{Schema: "ns", Name: "baz"},
- Columns: []*catalog.Column{
- {
- Name: "bar",
- Type: ast.TypeName{Name: "text"},
- },
- },
- },
- },
- },
- },
- {
- `
- ATTACH ':memory:' as ns;
- CREATE TABLE ns.foo (bar text);
- ALTER TABLE ns.foo ADD COLUMN baz bool;
- `,
- &catalog.Schema{
- Name: "ns",
- Tables: []*catalog.Table{
- {
- Rel: &ast.TableName{Schema: "ns", Name: "foo"},
- Columns: []*catalog.Column{
- {
- Name: "bar",
- Type: ast.TypeName{Name: "text"},
- },
- {
- Name: "baz",
- Type: ast.TypeName{Name: "bool"},
- },
- },
- },
- },
- },
- },
- {
- `
- ATTACH ':memory:' as ns;
- CREATE TABLE ns.foo (bar text);
- ALTER TABLE ns.foo RENAME COLUMN bar TO baz;
- `,
- &catalog.Schema{
- Name: "ns",
- Tables: []*catalog.Table{
- {
- Rel: &ast.TableName{Schema: "ns", Name: "foo"},
- Columns: []*catalog.Column{
- {
- Name: "baz",
- Type: ast.TypeName{Name: "text"},
- },
- },
- },
- },
- },
- },
- {
- `
- ATTACH ':memory:' as ns;
- CREATE TABLE ns.foo (bar text);
- ALTER TABLE ns.foo RENAME bar TO baz;
- `,
- &catalog.Schema{
- Name: "ns",
- Tables: []*catalog.Table{
- {
- Rel: &ast.TableName{Schema: "ns", Name: "foo"},
- Columns: []*catalog.Column{
- {
- Name: "baz",
- Type: ast.TypeName{Name: "text"},
- },
- },
- },
- },
- },
- },
- } {
- test := tc
- t.Run(strconv.Itoa(i), func(t *testing.T) {
- stmts, err := p.Parse(strings.NewReader(test.stmt))
- if err != nil {
- t.Log(test.stmt)
- t.Fatal(err)
- }
- c := newTestCatalog()
- if err := c.Build(stmts); err != nil {
- t.Log(test.stmt)
- t.Fatal(err)
- }
- e := newTestCatalog()
- if test.s != nil {
- var replaced bool
- for i := range e.Schemas {
- if e.Schemas[i].Name == test.s.Name {
- e.Schemas[i] = test.s
- replaced = true
- break
- }
- }
- if !replaced {
- e.Schemas = append(e.Schemas, test.s)
- }
- }
- if diff := cmp.Diff(e, c, cmpopts.EquateEmpty()); diff != "" {
- t.Log(test.stmt)
- t.Errorf("catalog mismatch:\n%s", diff)
- }
- })
- }
- }
|