Skip to content

Commit

Permalink
test: added e2e test
Browse files Browse the repository at this point in the history
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
  • Loading branch information
harshit-gangal committed Aug 9, 2023
1 parent 85009a0 commit 7f382a8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
29 changes: 29 additions & 0 deletions go/test/endtoend/vtgate/foreignkey/fk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,32 @@ func TestInsertions(t *testing.T) {
_, 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")
}

// TestDeletions tests that deletions work as expected when foreign key management is enabled in Vitess.
func TestDeletions(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)`)
utils.Exec(t, conn, `insert into t2(id, col) values (100, 125), (1, 132)`)
utils.Exec(t, conn, `insert into t4(id, col) values (1, 321)`)
utils.Exec(t, conn, `insert into multicol_tbl1(cola, colb, colc, msg) values (100, 'a', 'b', 'msg'), (101, 'c', 'd', 'msg2')`)
utils.Exec(t, conn, `insert into multicol_tbl2(cola, colb, colc, msg) values (100, 'a', 'b', 'msg3')`)

// child foreign key is shard scoped. Query will fail at mysql due to On Delete Restrict.
_, err := utils.ExecAllowError(t, conn, `delete from t2 where col = 132`)
require.ErrorContains(t, err, "Cannot delete or update a parent row: a foreign key constraint fails")

// child row does not exist so query will succeed.
qr := utils.Exec(t, conn, `delete from t2 where col = 125`)
require.EqualValues(t, 1, qr.RowsAffected)

// table's child foreign key has cross shard fk, so query will fail at vtgate.
_, err = utils.ExecAllowError(t, conn, `delete from t1 where id = 42`)
require.ErrorContains(t, err, "VT12002: unsupported: foreign keys management at vitess (errno 1235) (sqlstate 42000)")

// child foreign key is cascade, so query will fail at vtgate.
_, err = utils.ExecAllowError(t, conn, `delete from multicol_tbl1 where cola = 100`)
require.ErrorContains(t, err, "VT12002: unsupported: foreign keys management at vitess (errno 1235) (sqlstate 42000)")
}
2 changes: 1 addition & 1 deletion go/test/endtoend/vtgate/foreignkey/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func start(t *testing.T) (*mysql.Conn, func()) {

deleteAll := func() {
_ = utils.Exec(t, conn, "use `ks/-80`")
tables := []string{"t3", "t2", "t1", "multicol_tbl2", "multicol_tbl1"}
tables := []string{"t4", "t3", "t2", "t1", "multicol_tbl2", "multicol_tbl1"}
for _, table := range tables {
_ = utils.Exec(t, conn, "delete from "+table)
}
Expand Down
14 changes: 11 additions & 3 deletions go/test/endtoend/vtgate/foreignkey/sharded_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ create table t2
id bigint,
col bigint,
primary key (id),
foreign key (id) references t1 (id)
foreign key (id) references t1 (id) on delete restrict
) Engine = InnoDB;

create table t3
(
id bigint,
col bigint,
primary key (id),
foreign key (col) references t1 (id)
foreign key (col) references t1 (id) on delete restrict
) Engine = InnoDB;

create table multicol_tbl1
Expand All @@ -37,5 +37,13 @@ create table multicol_tbl2
colc varchar(50),
msg varchar(50),
primary key (cola, colb, colc),
foreign key (cola, colb, colc) references multicol_tbl1 (cola, colb, colc)
foreign key (cola, colb, colc) references multicol_tbl1 (cola, colb, colc) on delete cascade
) Engine = InnoDB;

create table t4
(
id bigint,
col bigint,
primary key (id),
foreign key (id) references t2 (id) on delete restrict
) Engine = InnoDB;
8 changes: 8 additions & 0 deletions go/test/endtoend/vtgate/foreignkey/sharded_vschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@
}
]
},
"t4": {
"column_vindexes": [
{
"column": "id",
"name": "xxhash"
}
]
},
"multicol_tbl1": {
"column_vindexes": [
{
Expand Down

0 comments on commit 7f382a8

Please sign in to comment.