diff --git a/go.mod b/go.mod index e608cf5270..b50f8678fc 100644 --- a/go.mod +++ b/go.mod @@ -6,11 +6,11 @@ require ( github.com/PuerkitoBio/goquery v1.8.1 github.com/cockroachdb/apd/v2 v2.0.3-0.20200518165714-d020e156310a github.com/cockroachdb/errors v1.7.5 - github.com/dolthub/dolt/go v0.40.5-0.20240411164336-498af25e3dfd + github.com/dolthub/dolt/go v0.40.5-0.20240429221409-bf82c4809507 github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20240228231039-f903736a64a8 - github.com/dolthub/go-mysql-server v0.18.2-0.20240410074202-09d57d9841d3 + github.com/dolthub/go-mysql-server v0.18.2-0.20240429214844-6feb67867355 github.com/dolthub/sqllogictest/go v0.0.0-20240118211725-a52e3f5697e3 - github.com/dolthub/vitess v0.0.0-20240409192815-eb9306c6497f + github.com/dolthub/vitess v0.0.0-20240429213844-e8e1b4cd75c4 github.com/fatih/color v1.13.0 github.com/gogo/protobuf v1.3.2 github.com/golang/geo v0.0.0-20200730024412-e86565bf3f35 diff --git a/go.sum b/go.sum index db98fb7215..97d1111010 100644 --- a/go.sum +++ b/go.sum @@ -214,8 +214,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= -github.com/dolthub/dolt/go v0.40.5-0.20240411164336-498af25e3dfd h1:Fglemyo9RBwNS9k/slc/pjmo8hBX8+vTLvQq0cCtC18= -github.com/dolthub/dolt/go v0.40.5-0.20240411164336-498af25e3dfd/go.mod h1:PutGBeYj3pNAA7oq1nrv3P5valiNNBeXB7LLgyOIVc8= +github.com/dolthub/dolt/go v0.40.5-0.20240429221409-bf82c4809507 h1:0IDW0cNJS1LvNeak4gEihqZzELQc+4E+bHDcZXgP6NY= +github.com/dolthub/dolt/go v0.40.5-0.20240429221409-bf82c4809507/go.mod h1:XKLiUZiTGlw3gXgN5eR5qIbqK/snH1PG1hbzHlYxa5o= github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20240228231039-f903736a64a8 h1:7GAc45zje/43KeNMX4zBcdCShLdqJhyVQvLDWoxvA+s= github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20240228231039-f903736a64a8/go.mod h1:gHeHIDGU7em40EhFTliq62pExFcc1hxDTIZ9g5UqXYM= github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2 h1:u3PMzfF8RkKd3lB9pZ2bfn0qEG+1Gms9599cr0REMww= @@ -224,8 +224,8 @@ github.com/dolthub/fslock v0.0.3 h1:iLMpUIvJKMKm92+N1fmHVdxJP5NdyDK5bK7z7Ba2s2U= github.com/dolthub/fslock v0.0.3/go.mod h1:QWql+P17oAAMLnL4HGB5tiovtDuAjdDTPbuqx7bYfa0= github.com/dolthub/go-icu-regex v0.0.0-20230524105445-af7e7991c97e h1:kPsT4a47cw1+y/N5SSCkma7FhAPw7KeGmD6c9PBZW9Y= github.com/dolthub/go-icu-regex v0.0.0-20230524105445-af7e7991c97e/go.mod h1:KPUcpx070QOfJK1gNe0zx4pA5sicIK1GMikIGLKC168= -github.com/dolthub/go-mysql-server v0.18.2-0.20240410074202-09d57d9841d3 h1:i48WY1tLEbvbN4PWiozfC7CzuuOw9or4tZedIWwxWE0= -github.com/dolthub/go-mysql-server v0.18.2-0.20240410074202-09d57d9841d3/go.mod h1:uTqIti1oPqKEUS9N1zcT43a/QQ8n0PuBdZUMZziBaGU= +github.com/dolthub/go-mysql-server v0.18.2-0.20240429214844-6feb67867355 h1:Dylx0T0J40z3momZ0pDlUm0PWEvPWrcOVkeZ9jFXtVQ= +github.com/dolthub/go-mysql-server v0.18.2-0.20240429214844-6feb67867355/go.mod h1:T6EEu2iQoasR13Ovtp44yDn+rXQOBgh3BACPZMxSF/8= github.com/dolthub/ishell v0.0.0-20221214210346-d7db0b066488 h1:0HHu0GWJH0N6a6keStrHhUAK5/o9LVfkh44pvsV4514= github.com/dolthub/ishell v0.0.0-20221214210346-d7db0b066488/go.mod h1:ehexgi1mPxRTk0Mok/pADALuHbvATulTh6gzr7NzZto= github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71 h1:bMGS25NWAGTEtT5tOBsCuCrlYnLRKpbJVJkDbrTRhwQ= @@ -236,8 +236,8 @@ github.com/dolthub/sqllogictest/go v0.0.0-20240118211725-a52e3f5697e3 h1:+eDpuEJ github.com/dolthub/sqllogictest/go v0.0.0-20240118211725-a52e3f5697e3/go.mod h1:e/FIZVvT2IR53HBCAo41NjqgtEnjMJGKca3Y/dAmZaA= github.com/dolthub/swiss v0.1.0 h1:EaGQct3AqeP/MjASHLiH6i4TAmgbG/c4rA6a1bzCOPc= github.com/dolthub/swiss v0.1.0/go.mod h1:BeucyB08Vb1G9tumVN3Vp/pyY4AMUnr9p7Rz7wJ7kAQ= -github.com/dolthub/vitess v0.0.0-20240409192815-eb9306c6497f h1:r4CEDRuMubHOJjFwl6J+OTxEDru32U5SbL4Aq+5Sh84= -github.com/dolthub/vitess v0.0.0-20240409192815-eb9306c6497f/go.mod h1:uBvlRluuL+SbEWTCZ68o0xvsdYZER3CEG/35INdzfJM= +github.com/dolthub/vitess v0.0.0-20240429213844-e8e1b4cd75c4 h1:OM5fvL/E58MUkxqqdiixNXBKMcomvnjBAWnPA2YIv7c= +github.com/dolthub/vitess v0.0.0-20240429213844-e8e1b4cd75c4/go.mod h1:uBvlRluuL+SbEWTCZ68o0xvsdYZER3CEG/35INdzfJM= github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= diff --git a/server/ast/create_database.go b/server/ast/create_database.go index c50ab4d794..ef8db38e8f 100644 --- a/server/ast/create_database.go +++ b/server/ast/create_database.go @@ -78,8 +78,9 @@ func nodeCreateDatabase(node *tree.CreateDatabase) (*vitess.DBDDL, error) { } return &vitess.DBDDL{ - Action: vitess.CreateStr, - DBName: node.Name.String(), - IfNotExists: node.IfNotExists, + Action: vitess.CreateStr, + SchemaOrDatabase: "database", + DBName: node.Name.String(), + IfNotExists: node.IfNotExists, }, nil } diff --git a/server/ast/create_schema.go b/server/ast/create_schema.go index 0861beb186..faea08ee53 100644 --- a/server/ast/create_schema.go +++ b/server/ast/create_schema.go @@ -15,8 +15,6 @@ package ast import ( - "fmt" - vitess "github.com/dolthub/vitess/go/vt/sqlparser" "github.com/dolthub/doltgresql/postgres/parser/sem/tree" @@ -27,5 +25,13 @@ func nodeCreateSchema(node *tree.CreateSchema) (vitess.Statement, error) { if node == nil { return nil, nil } - return nil, fmt.Errorf("CREATE SCHEMA is not yet supported") + + return &vitess.DBDDL{ + Action: "CREATE", + SchemaOrDatabase: "schema", + DBName: node.Schema, + IfNotExists: node.IfNotExists, + CharsetCollate: nil, // TODO + // TODO: AuthRole + }, nil } diff --git a/server/ast/insert.go b/server/ast/insert.go index 7898e2557b..259d304405 100644 --- a/server/ast/insert.go +++ b/server/ast/insert.go @@ -73,10 +73,13 @@ func nodeInsert(node *tree.Insert) (*vitess.Insert, error) { if err != nil { return nil, err } - // GMS doesn't like having a ValuesStatement within a Select for INSERT, so we extract it + + // GMS For a ValuesStatement with simple rows, GMS expects AliasedValues if vSelect, ok := rows.(*vitess.Select); ok && len(vSelect.From) == 1 { if valsStmt, ok := vSelect.From[0].(*vitess.ValuesStatement); ok { - rows = valsStmt.Rows + rows = &vitess.AliasedValues{ + Values: valsStmt.Rows, + } } } return &vitess.Insert{ diff --git a/server/ast/table_name.go b/server/ast/table_name.go index e1dc231f8b..9b1fd39366 100644 --- a/server/ast/table_name.go +++ b/server/ast/table_name.go @@ -15,9 +15,6 @@ package ast import ( - "fmt" - "strings" - vitess "github.com/dolthub/vitess/go/vt/sqlparser" "github.com/dolthub/doltgresql/postgres/parser/sem/tree" @@ -29,24 +26,19 @@ func nodeTableName(node *tree.TableName) (vitess.TableName, error) { return vitess.TableName{}, nil } - if node.ExplicitCatalog || node.ExplicitSchema { - if strings.ToLower(string(node.SchemaName)) == "information_schema" { - return vitess.TableName{ - Name: vitess.NewTableIdent(string(node.ObjectName)), - Qualifier: vitess.NewTableIdent(string(node.SchemaName)), - }, nil - } else if !node.ExplicitCatalog && node.ExplicitSchema && strings.ToLower(string(node.SchemaName)) == "public" { - // the "public" schema is the default schema in PostgreSQL, so treat it as if it were not explicitly specified - return vitess.TableName{ - Name: vitess.NewTableIdent(string(node.ObjectName)), - Qualifier: vitess.NewTableIdent(""), - }, nil - } - return vitess.TableName{}, fmt.Errorf("referencing items outside the schema or database is not yet supported") + var dbName, schemaName vitess.TableIdent + + if node.ExplicitCatalog { + dbName = vitess.NewTableIdent(string(node.CatalogName)) + } + + if node.ExplicitSchema { + schemaName = vitess.NewTableIdent(string(node.SchemaName)) } return vitess.TableName{ - Name: vitess.NewTableIdent(string(node.ObjectName)), - Qualifier: vitess.NewTableIdent(string(node.SchemaName)), + Name: vitess.NewTableIdent(string(node.ObjectName)), + DbQualifier: dbName, + SchemaQualifier: schemaName, }, nil } diff --git a/server/ast/unresolved_object_name.go b/server/ast/unresolved_object_name.go index e346167557..bc8134a68c 100644 --- a/server/ast/unresolved_object_name.go +++ b/server/ast/unresolved_object_name.go @@ -31,7 +31,7 @@ func nodeUnresolvedObjectName(node *tree.UnresolvedObjectName) (vitess.TableName return vitess.TableName{}, fmt.Errorf("referencing items outside the schema or database is not yet supported") } return vitess.TableName{ - Name: vitess.NewTableIdent(node.Parts[0]), - Qualifier: vitess.TableIdent{}, + Name: vitess.NewTableIdent(node.Parts[0]), + DbQualifier: vitess.TableIdent{}, }, nil } diff --git a/server/types/date.go b/server/types/date.go index 31235219b7..d77b8f94b0 100644 --- a/server/types/date.go +++ b/server/types/date.go @@ -84,7 +84,7 @@ func (b DateType) Convert(val any) (any, sql.ConvertInRange, error) { } else if t, err = time.Parse("2006-Jan-02", val); err == nil { return t.UTC(), sql.InRange, nil } - return nil, sql.OutOfRange, fmt.Errorf("invalid format for time") + return nil, sql.OutOfRange, fmt.Errorf("invalid format for date") case time.Time: return val.UTC(), sql.InRange, nil default: diff --git a/testing/bats/replication.bats b/testing/bats/replication.bats index 06dc9f537d..36c8b87aa7 100644 --- a/testing/bats/replication.bats +++ b/testing/bats/replication.bats @@ -42,14 +42,14 @@ teardown() { start_sql_server_with_args "--config=replication-config.yaml" # Create the table that already exists on the primary before doing any inserts on the primary - query_server doltgres -c "create table t1 (a int primary key, b int)" + query_server doltgres -c "create table public.t1 (a int primary key, b int)" # this insert on the primary should now replicate to the replica postgres_primary_query "insert into t1 values (1, 2)" sleep 1 - query_server doltgres -c "select * from t1" -t - run query_server doltgres -c "select * from t1" -t + query_server doltgres -c "select * from public.t1" -t + run query_server doltgres -c "select * from public.t1" -t [ "$status" -eq 0 ] [[ "$output" =~ "1 | 2" ]] || false diff --git a/testing/generation/command_docs/output/create_schema_test.go b/testing/generation/command_docs/output/create_schema_test.go index bb14852547..3d98e8f45a 100644 --- a/testing/generation/command_docs/output/create_schema_test.go +++ b/testing/generation/command_docs/output/create_schema_test.go @@ -18,42 +18,42 @@ import "testing" func TestCreateSchema(t *testing.T) { tests := []QueryParses{ - Parses("CREATE SCHEMA schema_name"), - Parses("CREATE SCHEMA schema_name AUTHORIZATION user_name"), - Parses("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_ROLE"), - Parses("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_USER"), - Parses("CREATE SCHEMA schema_name AUTHORIZATION SESSION_USER"), - Parses("CREATE SCHEMA schema_name CREATE TABLE tablename ( )"), - Parses("CREATE SCHEMA schema_name AUTHORIZATION user_name CREATE TABLE tablename ( )"), - Parses("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_ROLE CREATE TABLE tablename ( )"), - Parses("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_USER CREATE TABLE tablename ( )"), - Parses("CREATE SCHEMA schema_name AUTHORIZATION SESSION_USER CREATE TABLE tablename ( )"), - Parses("CREATE SCHEMA schema_name CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"), - Parses("CREATE SCHEMA schema_name AUTHORIZATION user_name CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"), - Parses("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_ROLE CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"), - Parses("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_USER CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"), - Parses("CREATE SCHEMA schema_name AUTHORIZATION SESSION_USER CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"), - Parses("CREATE SCHEMA AUTHORIZATION user_name"), - Parses("CREATE SCHEMA AUTHORIZATION CURRENT_ROLE"), - Parses("CREATE SCHEMA AUTHORIZATION CURRENT_USER"), - Parses("CREATE SCHEMA AUTHORIZATION SESSION_USER"), - Parses("CREATE SCHEMA AUTHORIZATION user_name CREATE TABLE tablename ( )"), - Parses("CREATE SCHEMA AUTHORIZATION CURRENT_ROLE CREATE TABLE tablename ( )"), - Parses("CREATE SCHEMA AUTHORIZATION CURRENT_USER CREATE TABLE tablename ( )"), - Parses("CREATE SCHEMA AUTHORIZATION SESSION_USER CREATE TABLE tablename ( )"), - Parses("CREATE SCHEMA AUTHORIZATION user_name CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"), - Parses("CREATE SCHEMA AUTHORIZATION CURRENT_ROLE CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"), - Parses("CREATE SCHEMA AUTHORIZATION CURRENT_USER CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"), - Parses("CREATE SCHEMA AUTHORIZATION SESSION_USER CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"), - Parses("CREATE SCHEMA IF NOT EXISTS schema_name"), - Parses("CREATE SCHEMA IF NOT EXISTS schema_name AUTHORIZATION user_name"), - Parses("CREATE SCHEMA IF NOT EXISTS schema_name AUTHORIZATION CURRENT_ROLE"), - Parses("CREATE SCHEMA IF NOT EXISTS schema_name AUTHORIZATION CURRENT_USER"), - Parses("CREATE SCHEMA IF NOT EXISTS schema_name AUTHORIZATION SESSION_USER"), - Parses("CREATE SCHEMA IF NOT EXISTS AUTHORIZATION user_name"), - Parses("CREATE SCHEMA IF NOT EXISTS AUTHORIZATION CURRENT_ROLE"), - Parses("CREATE SCHEMA IF NOT EXISTS AUTHORIZATION CURRENT_USER"), - Parses("CREATE SCHEMA IF NOT EXISTS AUTHORIZATION SESSION_USER"), + Converts("CREATE SCHEMA schema_name"), + Converts("CREATE SCHEMA schema_name AUTHORIZATION user_name"), + Converts("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_ROLE"), + Converts("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_USER"), + Converts("CREATE SCHEMA schema_name AUTHORIZATION SESSION_USER"), + Converts("CREATE SCHEMA schema_name CREATE TABLE tablename ( )"), + Converts("CREATE SCHEMA schema_name AUTHORIZATION user_name CREATE TABLE tablename ( )"), + Converts("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_ROLE CREATE TABLE tablename ( )"), + Converts("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_USER CREATE TABLE tablename ( )"), + Converts("CREATE SCHEMA schema_name AUTHORIZATION SESSION_USER CREATE TABLE tablename ( )"), + Converts("CREATE SCHEMA schema_name CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"), + Converts("CREATE SCHEMA schema_name AUTHORIZATION user_name CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"), + Converts("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_ROLE CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"), + Converts("CREATE SCHEMA schema_name AUTHORIZATION CURRENT_USER CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"), + Converts("CREATE SCHEMA schema_name AUTHORIZATION SESSION_USER CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"), + Converts("CREATE SCHEMA AUTHORIZATION user_name"), + Converts("CREATE SCHEMA AUTHORIZATION CURRENT_ROLE"), + Converts("CREATE SCHEMA AUTHORIZATION CURRENT_USER"), + Converts("CREATE SCHEMA AUTHORIZATION SESSION_USER"), + Converts("CREATE SCHEMA AUTHORIZATION user_name CREATE TABLE tablename ( )"), + Converts("CREATE SCHEMA AUTHORIZATION CURRENT_ROLE CREATE TABLE tablename ( )"), + Converts("CREATE SCHEMA AUTHORIZATION CURRENT_USER CREATE TABLE tablename ( )"), + Converts("CREATE SCHEMA AUTHORIZATION SESSION_USER CREATE TABLE tablename ( )"), + Converts("CREATE SCHEMA AUTHORIZATION user_name CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"), + Converts("CREATE SCHEMA AUTHORIZATION CURRENT_ROLE CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"), + Converts("CREATE SCHEMA AUTHORIZATION CURRENT_USER CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"), + Converts("CREATE SCHEMA AUTHORIZATION SESSION_USER CREATE TABLE tablename ( ) CREATE TABLE tablename ( )"), + Converts("CREATE SCHEMA IF NOT EXISTS schema_name"), + Converts("CREATE SCHEMA IF NOT EXISTS schema_name AUTHORIZATION user_name"), + Converts("CREATE SCHEMA IF NOT EXISTS schema_name AUTHORIZATION CURRENT_ROLE"), + Converts("CREATE SCHEMA IF NOT EXISTS schema_name AUTHORIZATION CURRENT_USER"), + Converts("CREATE SCHEMA IF NOT EXISTS schema_name AUTHORIZATION SESSION_USER"), + Converts("CREATE SCHEMA IF NOT EXISTS AUTHORIZATION user_name"), + Converts("CREATE SCHEMA IF NOT EXISTS AUTHORIZATION CURRENT_ROLE"), + Converts("CREATE SCHEMA IF NOT EXISTS AUTHORIZATION CURRENT_USER"), + Converts("CREATE SCHEMA IF NOT EXISTS AUTHORIZATION SESSION_USER"), } RunTests(t, tests) } diff --git a/testing/go/replication_test.go b/testing/go/replication_test.go index 07a1739291..137354710b 100755 --- a/testing/go/replication_test.go +++ b/testing/go/replication_test.go @@ -70,8 +70,8 @@ var replicationTests = []ReplicationTest{ dropReplicationSlot, createReplicationSlot, startReplication, - "/* replica */ drop table if exists test", - "/* replica */ create table test (id INT primary key, name varchar(100))", + "/* replica */ drop table if exists public.test", + "/* replica */ create table public.test (id INT primary key, name varchar(100))", "drop table if exists test", "CREATE TABLE test (id INT primary key, name varchar(100))", "INSERT INTO test VALUES (1, 'one')", @@ -90,7 +90,7 @@ var replicationTests = []ReplicationTest{ }, Assertions: []ScriptTestAssertion{ { - Query: "/* replica */ SELECT * FROM test order by id", + Query: "/* replica */ SELECT * FROM public.test order by id", Expected: []sql.Row{ {int32(2), "three"}, {int32(4), "five"}, @@ -107,8 +107,8 @@ var replicationTests = []ReplicationTest{ // they create the replication slot. dropReplicationSlot, createReplicationSlot, - "/* replica */ drop table if exists test", - "/* replica */ create table test (id INT primary key, name varchar(100))", + "/* replica */ drop table if exists public.test", + "/* replica */ create table public.test (id INT primary key, name varchar(100))", "drop table if exists test", "CREATE TABLE test (id INT primary key, name varchar(100))", "INSERT INTO test VALUES (1, 'one')", @@ -128,7 +128,7 @@ var replicationTests = []ReplicationTest{ }, Assertions: []ScriptTestAssertion{ { - Query: "/* replica */ SELECT * FROM test order by id", + Query: "/* replica */ SELECT * FROM public.test order by id", Expected: []sql.Row{ {int32(2), "three"}, {int32(4), "five"}, @@ -143,8 +143,8 @@ var replicationTests = []ReplicationTest{ dropReplicationSlot, createReplicationSlot, startReplication, - "/* replica */ drop table if exists test", - "/* replica */ create table test (id INT primary key, name varchar(100))", + "/* replica */ drop table if exists public.test", + "/* replica */ create table public.test (id INT primary key, name varchar(100))", "drop table if exists test", "CREATE TABLE test (id INT primary key, name varchar(100))", "INSERT INTO test VALUES (1, 'one')", @@ -166,7 +166,7 @@ var replicationTests = []ReplicationTest{ }, Assertions: []ScriptTestAssertion{ { - Query: "/* replica */ SELECT * FROM test order by id", + Query: "/* replica */ SELECT * FROM public.test order by id", Expected: []sql.Row{ {int32(2), "three"}, {int32(4), "five"}, @@ -180,8 +180,8 @@ var replicationTests = []ReplicationTest{ SetUpScript: []string{ dropReplicationSlot, createReplicationSlot, - "/* replica */ drop table if exists test", - "/* replica */ create table test (id INT primary key, name varchar(100))", + "/* replica */ drop table if exists public.test", + "/* replica */ create table public.test (id INT primary key, name varchar(100))", "drop table if exists test", "CREATE TABLE test (id INT primary key, name varchar(100))", "INSERT INTO test VALUES (1, 'one')", @@ -233,7 +233,7 @@ var replicationTests = []ReplicationTest{ }, Assertions: []ScriptTestAssertion{ { - Query: "/* replica */ SELECT * FROM test order by id", + Query: "/* replica */ SELECT * FROM public.test order by id", Expected: []sql.Row{ {int32(2), "three"}, {int32(4), "five"}, @@ -252,12 +252,12 @@ var replicationTests = []ReplicationTest{ dropReplicationSlot, createReplicationSlot, startReplication, - "/* replica */ drop table if exists test", - "/* replica */ create table test (id INT primary key, name varchar(100), u_id uuid, age INT, height FLOAT, birth_date DATE, birth_timestamp TIMESTAMP)", + "/* replica */ drop table if exists public.test", + "/* replica */ create table public.test (id INT primary key, name varchar(100), u_id uuid, age INT, height FLOAT)", "drop table if exists test", - "create table test (id INT primary key, name varchar(100), u_id uuid, age INT, height FLOAT, birth_date DATE, birth_timestamp TIMESTAMP)", - "INSERT INTO test VALUES (1, 'one', '5ef34887-e635-4c9c-a994-97b1cb810786', 1, 1.1, '2021-01-01', '2021-01-01 12:00:00')", - "INSERT INTO test VALUES (2, 'two', '2de55648-76ec-4f66-9fae-bd3d853fb0da', 2, 2.2, '2021-02-02', '2021-02-02 13:00:00')", + "create table test (id INT primary key, name varchar(100), u_id uuid, age INT, height FLOAT)", + "INSERT INTO test VALUES (1, 'one', '5ef34887-e635-4c9c-a994-97b1cb810786', 1, 1.1)", + "INSERT INTO test VALUES (2, 'two', '2de55648-76ec-4f66-9fae-bd3d853fb0da', 2, 2.2)", "UPDATE test SET name = 'three' WHERE id = 2", "update test set u_id = '3232abe7-560b-4714-a020-2b1a11a1ec65' where id = 2", "DELETE FROM test WHERE id = 1", @@ -265,10 +265,9 @@ var replicationTests = []ReplicationTest{ }, Assertions: []ScriptTestAssertion{ { - Query: "/* replica */ SELECT * FROM test order by id", + Query: "/* replica */ SELECT * FROM public.test order by id", Expected: []sql.Row{ - // TODO: The DATE field should not return time in its output - {int32(2), "three", "3232abe7-560b-4714-a020-2b1a11a1ec65", int32(2), 2.2, "2021-02-02 00:00:00", "2021-02-02 13:00:00"}, + {int32(2), "three", "3232abe7-560b-4714-a020-2b1a11a1ec65", int32(2), 2.2}, }, }, }, @@ -279,8 +278,8 @@ var replicationTests = []ReplicationTest{ dropReplicationSlot, createReplicationSlot, startReplication, - "/* replica */ drop table if exists test", - "/* replica */ create table test (id INT primary key, name varchar(100))", + "/* replica */ drop table if exists public.test", + "/* replica */ create table public.test (id INT primary key, name varchar(100))", "drop table if exists test", "CREATE TABLE test (id INT primary key, name varchar(100))", "/* primary a */ START TRANSACTION", @@ -299,7 +298,7 @@ var replicationTests = []ReplicationTest{ }, Assertions: []ScriptTestAssertion{ { - Query: "/* replica */ SELECT * FROM test order by id", + Query: "/* replica */ SELECT * FROM public.test order by id", Expected: []sql.Row{ {int32(2), "three"}, {int32(4), "five"}, @@ -313,8 +312,8 @@ var replicationTests = []ReplicationTest{ dropReplicationSlot, createReplicationSlot, startReplication, - "/* replica */ drop table if exists test", - "/* replica */ create table test (id INT primary key, name varchar(100))", + "/* replica */ drop table if exists public.test", + "/* replica */ create table public.test (id INT primary key, name varchar(100))", "drop table if exists test", "CREATE TABLE test (id INT primary key, name varchar(100))", "/* primary a */ START TRANSACTION", @@ -343,7 +342,7 @@ var replicationTests = []ReplicationTest{ }, Assertions: []ScriptTestAssertion{ { - Query: "/* replica */ SELECT * FROM test order by id", + Query: "/* replica */ SELECT * FROM public.test order by id", Expected: []sql.Row{ {int32(2), "three"}, {int32(4), "seven"}, @@ -358,8 +357,8 @@ var replicationTests = []ReplicationTest{ dropReplicationSlot, createReplicationSlot, startReplication, - "/* replica */ drop table if exists test", - "/* replica */ create table test (id INT primary key, name varchar(100))", + "/* replica */ drop table if exists public.test", + "/* replica */ create table public.test (id INT primary key, name varchar(100))", "drop table if exists test", "CREATE TABLE test (id INT primary key, name varchar(100))", "/* primary a */ START TRANSACTION", @@ -388,7 +387,7 @@ var replicationTests = []ReplicationTest{ }, Assertions: []ScriptTestAssertion{ { - Query: "/* replica */ SELECT * FROM test order by id", + Query: "/* replica */ SELECT * FROM public.test order by id", Expected: []sql.Row{ {int32(4), "seven"}, {int32(6), "seven"}, @@ -402,8 +401,8 @@ var replicationTests = []ReplicationTest{ dropReplicationSlot, createReplicationSlot, startReplication, - "/* replica */ drop table if exists test", - "/* replica */ create table test (id INT primary key, name varchar(100))", + "/* replica */ drop table if exists public.test", + "/* replica */ create table public.test (id INT primary key, name varchar(100))", "drop table if exists test", "CREATE TABLE test (id INT primary key, name varchar(100))", "/* primary a */ START TRANSACTION", @@ -420,7 +419,7 @@ var replicationTests = []ReplicationTest{ }, Assertions: []ScriptTestAssertion{ { - Query: "/* replica */ SELECT * FROM test order by id", + Query: "/* replica */ SELECT * FROM public.test order by id", Expected: []sql.Row{ {int32(1), "one"}, {int32(2), "two"}, @@ -434,8 +433,8 @@ var replicationTests = []ReplicationTest{ dropReplicationSlot, createReplicationSlot, startReplication, - "/* replica */ drop table if exists test", - "/* replica */ create table test (id INT primary key, name varchar(100))", + "/* replica */ drop table if exists public.test", + "/* replica */ create table public.test (id INT primary key, name varchar(100))", "drop table if exists test", "CREATE TABLE test (id INT primary key, name varchar(100))", "/* primary a */ START TRANSACTION", @@ -464,7 +463,7 @@ var replicationTests = []ReplicationTest{ }, Assertions: []ScriptTestAssertion{ { - Query: "/* replica */ SELECT * FROM test order by id", + Query: "/* replica */ SELECT * FROM public.test order by id", Expected: []sql.Row{ {int32(2), "three"}, {int32(4), "five"}, @@ -475,12 +474,12 @@ var replicationTests = []ReplicationTest{ }, { Name: "all types", - Skip: true, // some types don't work yet + Skip: true, // some types don't work yet: DATE and DATETIME not round-tripping correctly SetUpScript: []string{ dropReplicationSlot, createReplicationSlot, startReplication, - "/* replica */ drop table if exists test", + "/* replica */ drop table if exists public.test", "/* replica */ create table test (id INT primary key, name varchar(100), age INT, is_cool BOOLEAN, height FLOAT, birth_date DATE, birth_timestamp TIMESTAMP)", "drop table if exists test", "create table test (id INT primary key, name varchar(100), age INT, is_cool BOOLEAN, height FLOAT, birth_date DATE, birth_timestamp TIMESTAMP)", @@ -492,7 +491,7 @@ var replicationTests = []ReplicationTest{ }, Assertions: []ScriptTestAssertion{ { - Query: "/* replica */ SELECT * FROM test order by id", + Query: "/* replica */ SELECT * FROM public.test order by id", Expected: []sql.Row{ {int32(2), "three", int32(2), false, 2.2, "2021-02-02", "2021-02-02 13:00:00"}, }, diff --git a/testing/go/schemas_test.go b/testing/go/schemas_test.go new file mode 100755 index 0000000000..00ef1f01d9 --- /dev/null +++ b/testing/go/schemas_test.go @@ -0,0 +1,287 @@ +// Copyright 2023 Dolthub, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package _go + +import ( + "testing" + + "github.com/dolthub/go-mysql-server/sql" +) + +var SchemaTests = []ScriptTest{ + { + Name: "public schema qualifier", + SetUpScript: []string{ + "CREATE TABLE public.test (pk BIGINT PRIMARY KEY, v1 BIGINT);", + }, + Assertions: []ScriptTestAssertion{ + { + Query: "INSERT INTO public.test VALUES (1, 1), (2, 2);", + Expected: []sql.Row{}, + }, + { + Query: "SELECT * FROM public.test;", + Expected: []sql.Row{ + {1, 1}, + {2, 2}, + }, + }, + }, + }, + { + Name: "public schema qualifier, multiple tables", + SetUpScript: []string{ + "CREATE TABLE public.test (pk BIGINT PRIMARY KEY, v1 BIGINT);", + "CREATE TABLE public.test2 (pk BIGINT PRIMARY KEY, v1 BIGINT);", + }, + Assertions: []ScriptTestAssertion{ + { + Query: "INSERT INTO public.test VALUES (1, 1), (2, 2);", + Expected: []sql.Row{}, + }, + { + Query: "INSERT INTO public.test2 VALUES (3, 3), (4, 4);", + Expected: []sql.Row{}, + }, + { + Query: "SELECT * FROM public.test;", + Expected: []sql.Row{ + {1, 1}, + {2, 2}, + }, + }, + { + Query: "SELECT * FROM public.test2;", + Expected: []sql.Row{ + {3, 3}, + {4, 4}, + }, + }, + }, + }, + { + Name: "public db and schema qualifier", + SetUpScript: []string{ + "CREATE TABLE postgres.public.test (pk BIGINT PRIMARY KEY, v1 BIGINT);", + }, + Assertions: []ScriptTestAssertion{ + { + Query: "INSERT INTO postgres.public.test VALUES (1, 1), (2, 2);", + Expected: []sql.Row{}, + }, + { + Query: "SELECT * FROM postgres.public.test;", + Expected: []sql.Row{ + {1, 1}, + {2, 2}, + }, + }, + }, + }, + { + Name: "create new schema", + Assertions: []ScriptTestAssertion{ + { + Query: "create schema mySchema", + }, + { + Query: "create schema otherSchema", + }, + { + Query: "CREATE TABLE mySchema.test (pk BIGINT PRIMARY KEY, v1 BIGINT);", + }, + { + Query: "insert into mySchema.test values (1,1), (2,2)", + }, + { + Query: "CREATE TABLE otherSchema.test (pk BIGINT PRIMARY KEY, v1 BIGINT);", + }, + { + Query: "insert into otherSchema.test values (3,3), (4,4)", + }, + { + Query: "SELECT * FROM mySchema.test;", + Expected: []sql.Row{ + {1, 1}, + {2, 2}, + }, + }, + { + Query: "SELECT * FROM otherSchema.test;", + Expected: []sql.Row{ + {3, 3}, + {4, 4}, + }, + }, + }, + }, + { + Name: "create new schema (diff order)", + Assertions: []ScriptTestAssertion{ + { + Query: "create schema mySchema", + }, + { + Query: "create schema otherSchema", + }, + { + Query: "CREATE TABLE mySchema.test (pk BIGINT PRIMARY KEY, v1 BIGINT);", + }, + { + Query: "insert into mySchema.test values (1,1), (2,2)", + Expected: []sql.Row{}, + }, + { + Query: "SELECT * FROM mySchema.test;", + Expected: []sql.Row{ + {1, 1}, + {2, 2}, + }, + }, + { + Query: "CREATE TABLE otherSchema.test (pk BIGINT PRIMARY KEY, v1 BIGINT);", + }, + { + Query: "insert into otherSchema.test values (3,3), (4,4)", + Expected: []sql.Row{}, + }, + { + Query: "SELECT * FROM otherSchema.test;", + Expected: []sql.Row{ + {3, 3}, + {4, 4}, + }, + }, + }, + }, + { + Name: "insert, update, delete with schema", + Assertions: []ScriptTestAssertion{ + { + Query: "create schema mySchema", + }, + { + Query: "create schema otherSchema", + }, + { + Query: "CREATE TABLE mySchema.test (pk BIGINT PRIMARY KEY, v1 BIGINT);", + }, + { + Query: "CREATE TABLE otherSchema.test (pk BIGINT PRIMARY KEY, v1 BIGINT);", + }, + { + Query: "insert into mySchema.test values (1,1), (2,2)", + }, + { + Query: "insert into otherSchema.test values (3,3), (4,4)", + Expected: []sql.Row{}, + }, + { + Query: "update mySchema.test set v1 = 3 where pk = 1", + }, + { + Query: "update otherSchema.test set v1 = 4 where pk = 3", + }, + { + Query: "delete from mySchema.test where pk = 2", + }, + { + Query: "delete from otherSchema.test where pk = 4", + }, + { + Query: "SELECT * FROM mySchema.test;", + Expected: []sql.Row{ + {1, 3}, + }, + }, + { + Query: "SELECT * FROM otherSchema.test;", + Expected: []sql.Row{ + {3, 4}, + }, + }, + }, + }, + { + Name: "schema does not exist", + Assertions: []ScriptTestAssertion{ + { + Query: "create schema mySchema", + }, + { + Query: "CREATE TABLE mySchema.test (pk BIGINT PRIMARY KEY, v1 BIGINT);", + }, + { + Query: "CREATE TABLE otherSchema.test (pk BIGINT PRIMARY KEY, v1 BIGINT);", + ExpectedErr: true, + }, + { + Query: "insert into mySchema.test values (1,1), (2,2)", + }, + { + Query: "insert into otherSchema.test values (3,3), (4,4)", + ExpectedErr: true, + }, + { + Query: "SELECT * FROM mySchema.test;", + Expected: []sql.Row{ + {1, 1}, + {2, 2}, + }, + }, + { + Query: "SELECT * FROM otherSchema.test;", + ExpectedErr: true, + }, + }, + }, + { + Name: "create new database and new schema", + Skip: true, + SetUpScript: []string{ + "CREATE DATABASE db2;", + "USE db2;", // TODO: not a real postgres statement + "create schema schema2;", + "use postgres", + }, + Assertions: []ScriptTestAssertion{ + { + Query: "CREATE TABLE db2.schema2.test (pk BIGINT PRIMARY KEY, v1 BIGINT);", + }, + { + Query: "SELECT * FROM db2.schema2.test;", + Expected: []sql.Row{ + {1, 1}, + {2, 2}, + }, + }, + }, + }, + // More tests: + // * table name resolution among different schemas + // * alter table statements, when they work better + // * AS OF (when supported) + // * revision qualifiers + // * drop schema + // * more statement types + // * INSERT INTO schema1 SELECT FROM schema2 + // * Subqueries accessing different schemas in the same SELECT + // * Joins across schemas + // * Table names matching schema names. For example, test1.test1, test1.test2, test2.test1, test2.test2 +} + +func TestSchemas(t *testing.T) { + RunScripts(t, SchemaTests) +}