Skip to content

Commit

Permalink
ddl: allow StatePublic in createTableWithForeignKeys for BR (ping…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored May 7, 2024
1 parent 1e19051 commit df53876
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 36 deletions.
71 changes: 37 additions & 34 deletions br/tests/br_foreign_key/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,40 @@
set -eu
DB="$TEST_NAME"

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 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);"
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.t1;"
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'

run_sql "drop schema $DB"
for DDL_BATCH_SIZE in 1 2;
do
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 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);"
run_sql "delete from $DB.t1 where id = 2;"

echo "backup start..."
run_br backup db --db "$DB" -s "local://$TEST_DIR/$DB-$DDL_BATCH_SIZE" --pd $PD_ADDR

run_sql "drop schema $DB;"

echo "restore start..."
run_br restore db --db $DB -s "local://$TEST_DIR/$DB-$DDL_BATCH_SIZE" --pd $PD_ADDR --ddl-batch-size=$DDL_BATCH_SIZE

set -x

run_sql "select count(*) from $DB.t1;"
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'

run_sql "drop schema $DB"
done
7 changes: 5 additions & 2 deletions pkg/ddl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,11 @@ func onCreateTable(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ error)

func createTableWithForeignKeys(d *ddlCtx, t *meta.Meta, job *model.Job, tbInfo *model.TableInfo, fkCheck bool) (ver int64, err error) {
switch tbInfo.State {
case model.StateNone:
// create table in non-public state
case model.StateNone, model.StatePublic:
// create table in non-public or public state. The function `createTable` will always reset
// the `tbInfo.State` with `model.StateNone`, so it's fine to just call the `createTable` with
// public state.
// when `br` restores table, the state of `tbInfo` will be public.
tbInfo, err = createTable(d, t, job, fkCheck)
if err != nil {
return ver, errors.Trace(err)
Expand Down

0 comments on commit df53876

Please sign in to comment.