From 2622984508bd15fab256e4247a4025efae5a6ce9 Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Mon, 28 Nov 2022 15:17:42 +0800 Subject: [PATCH 1/6] init Signed-off-by: crazycs520 --- br/tests/br_foreign_key/run.sh | 44 ++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 br/tests/br_foreign_key/run.sh diff --git a/br/tests/br_foreign_key/run.sh b/br/tests/br_foreign_key/run.sh new file mode 100644 index 0000000000000..e3345f5d065af --- /dev/null +++ b/br/tests/br_foreign_key/run.sh @@ -0,0 +1,44 @@ +#!/bin/sh +# +# Copyright 2022 PingCAP, 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. + +set -eu +DB="$TEST_NAME" + +run_sql "set @@global.tidb_enable_foreign_key=1;" +run_sql "set @@global.foreign_key_checks=1;" +run_sql "set @@foreign_key_checks=1;" +run_sql "create schema $DB;" +run_sql "create table $DB.t1 (id int key);" +run_sql "create table $DB.t2 (id int key, a int, b int, foreign key fk_1 (a) references t1(id) ON UPDATE SET NULL, foreign key fk_2 (b) references t1(id) ON DELETE CASCADE);" +run_sql "insert into $DB.t1 values (1), (2), (3);" +run_sql "insert into $DB.t2 values (1, 1, 1), (2, 2, 2), (3, 3, 3);" +run_sql "update $DB.t1 set id=id+10 where id in (1, 3);" +run_sql "delete from $DB.t1 where id = 2;" + +echo "backup start..." +run_br backup db --db "$DB" -s "local://$TEST_DIR/$DB" --pd $PD_ADDR + +run_sql "drop schema $DB;" + +echo "restore start..." +run_br restore db --db $DB -s "local://$TEST_DIR/$DB" --pd $PD_ADDR + +set -x + +run_sql "select count(*) from $DB.cache_1;" +check_contains 'count(*): 3' + +run_sql "drop schema $DB" From 09a085ecffe8b3eaa32964b26d80a0fd5286142e Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Mon, 28 Nov 2022 17:14:46 +0800 Subject: [PATCH 2/6] add check --- br/tests/br_foreign_key/run.sh | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/br/tests/br_foreign_key/run.sh b/br/tests/br_foreign_key/run.sh index e3345f5d065af..77620e789ad4a 100644 --- a/br/tests/br_foreign_key/run.sh +++ b/br/tests/br_foreign_key/run.sh @@ -22,7 +22,7 @@ run_sql "set @@global.foreign_key_checks=1;" run_sql "set @@foreign_key_checks=1;" run_sql "create schema $DB;" run_sql "create table $DB.t1 (id int key);" -run_sql "create table $DB.t2 (id int key, a int, b int, foreign key fk_1 (a) references t1(id) ON UPDATE SET NULL, foreign key fk_2 (b) references t1(id) ON DELETE CASCADE);" +run_sql "create table $DB.t2 (id int key, a int, b int, foreign key fk_1 (a) references t1(id) ON UPDATE SET NULL ON DELETE SET NULL, foreign key fk_2 (b) references t1(id) ON DELETE CASCADE ON UPDATE CASCADE);" run_sql "insert into $DB.t1 values (1), (2), (3);" run_sql "insert into $DB.t2 values (1, 1, 1), (2, 2, 2), (3, 3, 3);" run_sql "update $DB.t1 set id=id+10 where id in (1, 3);" @@ -38,7 +38,10 @@ run_br restore db --db $DB -s "local://$TEST_DIR/$DB" --pd $PD_ADDR set -x -run_sql "select count(*) from $DB.cache_1;" -check_contains 'count(*): 3' +run_sql "select count(*) from $DB.t1;" +check_contains 'count(*): 2' + +run_sql "select count(*) from $DB.t2;" +check_contains 'count(*): 2' run_sql "drop schema $DB" From d628c09e9fce08c3223b5e6c51b6d4260ff9f96e Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Tue, 29 Nov 2022 15:22:50 +0800 Subject: [PATCH 3/6] fix ci Signed-off-by: crazycs520 --- ddl/ddl_api.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 505d413fff8c2..c1cfbb75979b0 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -2619,7 +2619,8 @@ func (d *ddl) BatchCreateTableWithInfo(ctx sessionctx.Context, return nil } jobs.Args = append(jobs.Args, args) - jobs.Args = append(jobs.Args, ctx.GetSessionVars().ForeignKeyChecks) + // Disable foreign key check when batch create tables. + jobs.Args = append(jobs.Args, false) err = d.DoDDLJob(ctx, jobs) if err != nil { From 64c9e0585f1afe5af8d1706cf171f1783f400695 Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Tue, 29 Nov 2022 16:15:03 +0800 Subject: [PATCH 4/6] fix test Signed-off-by: crazycs520 --- br/pkg/gluetidb/glue.go | 2 ++ ddl/ddl_api.go | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/br/pkg/gluetidb/glue.go b/br/pkg/gluetidb/glue.go index 38bf4114e7694..6e16825601251 100644 --- a/br/pkg/gluetidb/glue.go +++ b/br/pkg/gluetidb/glue.go @@ -231,6 +231,8 @@ func (gs *tidbSession) CreateTables(ctx context.Context, tables map[string][]*mo cloneTables = append(cloneTables, table) } gs.se.SetValue(sessionctx.QueryString, queryBuilder.String()) + // Disable foreign key check when batch create tables. + gs.se.GetSessionVars().ForeignKeyChecks = false err := d.BatchCreateTableWithInfo(gs.se, dbName, cloneTables, append(cs, ddl.OnExistIgnore)...) if err != nil { //It is possible to failure when TiDB does not support model.ActionCreateTables. diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index c1cfbb75979b0..505d413fff8c2 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -2619,8 +2619,7 @@ func (d *ddl) BatchCreateTableWithInfo(ctx sessionctx.Context, return nil } jobs.Args = append(jobs.Args, args) - // Disable foreign key check when batch create tables. - jobs.Args = append(jobs.Args, false) + jobs.Args = append(jobs.Args, ctx.GetSessionVars().ForeignKeyChecks) err = d.DoDDLJob(ctx, jobs) if err != nil { From 2da5825b0c1b182464e1a24f4cbadd04f8b95990 Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Wed, 30 Nov 2022 13:38:37 +0800 Subject: [PATCH 5/6] add test Signed-off-by: crazycs520 --- br/tests/br_foreign_key/run.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/br/tests/br_foreign_key/run.sh b/br/tests/br_foreign_key/run.sh index 77620e789ad4a..1eaaa168f7bbe 100644 --- a/br/tests/br_foreign_key/run.sh +++ b/br/tests/br_foreign_key/run.sh @@ -44,4 +44,12 @@ check_contains 'count(*): 2' run_sql "select count(*) from $DB.t2;" check_contains 'count(*): 2' +run_sql "select id, a, b from $DB.t2;" +check_contains 'id: 1' +check_contains 'id: 3' +check_contains 'a: NULL' +check_contains 'b: 11' +check_contains 'b: 13' +check_contains 'b: 14' + run_sql "drop schema $DB" From 08a75b89ede7b164a6e33ac242b4c0096172e929 Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Wed, 30 Nov 2022 15:34:46 +0800 Subject: [PATCH 6/6] address comment Signed-off-by: crazycs520 --- br/pkg/gluetidb/glue.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/br/pkg/gluetidb/glue.go b/br/pkg/gluetidb/glue.go index 6e16825601251..3b1675921afd3 100644 --- a/br/pkg/gluetidb/glue.go +++ b/br/pkg/gluetidb/glue.go @@ -208,6 +208,8 @@ func (gs *tidbSession) CreateTables(ctx context.Context, tables map[string][]*mo d := domain.GetDomain(gs.se).DDL() var dbName model.CIStr + // Disable foreign key check when batch create tables. + gs.se.GetSessionVars().ForeignKeyChecks = false for db, tablesInDB := range tables { dbName = model.NewCIStr(db) queryBuilder := strings.Builder{} @@ -231,8 +233,6 @@ func (gs *tidbSession) CreateTables(ctx context.Context, tables map[string][]*mo cloneTables = append(cloneTables, table) } gs.se.SetValue(sessionctx.QueryString, queryBuilder.String()) - // Disable foreign key check when batch create tables. - gs.se.GetSessionVars().ForeignKeyChecks = false err := d.BatchCreateTableWithInfo(gs.se, dbName, cloneTables, append(cs, ddl.OnExistIgnore)...) if err != nil { //It is possible to failure when TiDB does not support model.ActionCreateTables.