-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Foreign Keys:
INSERT
planning (#13676)
Signed-off-by: Harshit Gangal <[email protected]> Signed-off-by: Manan Gupta <[email protected]> Co-authored-by: Harshit Gangal <[email protected]>
- Loading branch information
1 parent
54f0b33
commit 98cd2cd
Showing
21 changed files
with
893 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
Copyright 2023 The Vitess Authors. | ||
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 foreignkey | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"vitess.io/vitess/go/test/endtoend/utils" | ||
) | ||
|
||
// TestInsertions tests that insertions work as expected when foreign key management is enabled in Vitess. | ||
func TestInsertions(t *testing.T) { | ||
conn, closer := start(t) | ||
defer closer() | ||
|
||
// insert some data. | ||
utils.Exec(t, conn, `insert into t1(id, col) values (100, 123),(10, 12),(1, 13),(1000, 1234)`) | ||
|
||
// Verify that inserting data into a table that has shard scoped foreign keys works. | ||
utils.Exec(t, conn, `insert into t2(id, col) values (100, 125), (1, 132)`) | ||
// Verify that insertion fails if the data doesn't follow the fk constraint. | ||
_, err := utils.ExecAllowError(t, conn, `insert into t2(id, col) values (1310, 125)`) | ||
require.ErrorContains(t, err, "Cannot add or update a child row: a foreign key constraint fails") | ||
// Verify that insertion fails if the table has cross-shard foreign keys (even if the data follows the constraints). | ||
_, err = utils.ExecAllowError(t, conn, `insert into t3(id, col) values (100, 100)`) | ||
require.ErrorContains(t, err, "VT12002: unsupported: cross-shard foreign keys") | ||
|
||
// insert some data in a table with multicol vindex. | ||
utils.Exec(t, conn, `insert into multicol_tbl1(cola, colb, colc, msg) values (100, 'a', 'b', 'msg'), (101, 'c', 'd', 'msg2')`) | ||
// Verify that inserting data into a table that has shard scoped multi-column foreign keys works. | ||
utils.Exec(t, conn, `insert into multicol_tbl2(cola, colb, colc, msg) values (100, 'a', 'b', 'msg3')`) | ||
// Verify that insertion fails if the data doesn't follow the fk constraint. | ||
_, err = utils.ExecAllowError(t, conn, `insert into multicol_tbl2(cola, colb, colc, msg) values (103, 'c', 'd', 'msg2')`) | ||
require.ErrorContains(t, err, "Cannot add or update a child row: a foreign key constraint fails") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
Copyright 2023 The Vitess Authors. | ||
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 foreignkey | ||
|
||
import ( | ||
"context" | ||
_ "embed" | ||
"flag" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"vitess.io/vitess/go/test/endtoend/utils" | ||
|
||
"vitess.io/vitess/go/mysql" | ||
"vitess.io/vitess/go/test/endtoend/cluster" | ||
) | ||
|
||
var ( | ||
clusterInstance *cluster.LocalProcessCluster | ||
vtParams mysql.ConnParams | ||
shardedKs = "ks" | ||
Cell = "test" | ||
|
||
//go:embed sharded_schema.sql | ||
shardedSchemaSQL string | ||
|
||
//go:embed sharded_vschema.json | ||
shardedVSchema string | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
defer cluster.PanicHandler(nil) | ||
flag.Parse() | ||
|
||
exitCode := func() int { | ||
clusterInstance = cluster.NewCluster(Cell, "localhost") | ||
defer clusterInstance.Teardown() | ||
|
||
// Start topo server | ||
err := clusterInstance.StartTopo() | ||
if err != nil { | ||
return 1 | ||
} | ||
|
||
// Start keyspace | ||
sKs := &cluster.Keyspace{ | ||
Name: shardedKs, | ||
SchemaSQL: shardedSchemaSQL, | ||
VSchema: shardedVSchema, | ||
} | ||
|
||
err = clusterInstance.StartKeyspace(*sKs, []string{"-80", "80-"}, 0, false) | ||
if err != nil { | ||
return 1 | ||
} | ||
|
||
// Start vtgate | ||
err = clusterInstance.StartVtgate() | ||
if err != nil { | ||
return 1 | ||
} | ||
vtParams = mysql.ConnParams{ | ||
Host: clusterInstance.Hostname, | ||
Port: clusterInstance.VtgateMySQLPort, | ||
} | ||
|
||
return m.Run() | ||
}() | ||
os.Exit(exitCode) | ||
} | ||
|
||
func start(t *testing.T) (*mysql.Conn, func()) { | ||
conn, err := mysql.Connect(context.Background(), &vtParams) | ||
require.NoError(t, err) | ||
|
||
deleteAll := func() { | ||
tables := []string{"t3", "t2", "t1", "multicol_tbl2", "multicol_tbl1"} | ||
for _, table := range tables { | ||
_ = utils.Exec(t, conn, "delete from "+table) | ||
} | ||
} | ||
|
||
deleteAll() | ||
|
||
return conn, func() { | ||
deleteAll() | ||
conn.Close() | ||
cluster.PanicHandler(t) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
create table t1 | ||
( | ||
id bigint, | ||
col bigint, | ||
primary key (id) | ||
) Engine = InnoDB; | ||
|
||
create table t2 | ||
( | ||
id bigint, | ||
col bigint, | ||
primary key (id), | ||
foreign key (id) references t1 (id) | ||
) Engine = InnoDB; | ||
|
||
create table t3 | ||
( | ||
id bigint, | ||
col bigint, | ||
primary key (id), | ||
foreign key (col) references t1 (id) | ||
) Engine = InnoDB; | ||
|
||
create table multicol_tbl1 | ||
( | ||
cola bigint, | ||
colb varbinary(50), | ||
colc varchar(50), | ||
msg varchar(50), | ||
primary key (cola, colb, colc) | ||
) Engine = InnoDB; | ||
|
||
create table multicol_tbl2 | ||
( | ||
cola bigint, | ||
colb varbinary(50), | ||
colc varchar(50), | ||
msg varchar(50), | ||
primary key (cola, colb, colc), | ||
foreign key (cola, colb, colc) references multicol_tbl1 (cola, colb, colc) | ||
) Engine = InnoDB; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
{ | ||
"sharded": true, | ||
"foreignKeyMode": "FK_MANAGED", | ||
"vindexes": { | ||
"xxhash": { | ||
"type": "xxhash" | ||
}, | ||
"multicol_vdx": { | ||
"type": "multicol", | ||
"params": { | ||
"column_count": "3", | ||
"column_bytes": "1,3,4", | ||
"column_vindex": "hash,binary,unicode_loose_xxhash" | ||
} | ||
} | ||
}, | ||
"tables": { | ||
"t1": { | ||
"column_vindexes": [ | ||
{ | ||
"column": "id", | ||
"name": "xxhash" | ||
} | ||
] | ||
}, | ||
"t2": { | ||
"column_vindexes": [ | ||
{ | ||
"column": "id", | ||
"name": "xxhash" | ||
} | ||
] | ||
}, | ||
"t3": { | ||
"column_vindexes": [ | ||
{ | ||
"column": "id", | ||
"name": "xxhash" | ||
} | ||
] | ||
}, | ||
"multicol_tbl1": { | ||
"column_vindexes": [ | ||
{ | ||
"columns": [ | ||
"cola", | ||
"colb", | ||
"colc" | ||
], | ||
"name": "multicol_vdx" | ||
} | ||
] | ||
}, | ||
"multicol_tbl2": { | ||
"column_vindexes": [ | ||
{ | ||
"columns": [ | ||
"cola", | ||
"colb", | ||
"colc" | ||
], | ||
"name": "multicol_vdx" | ||
} | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.