Skip to content

Commit

Permalink
*: enable tidb_enable_foreign_key variable default (#39677)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 authored Dec 7, 2022
1 parent 442c044 commit b33ff62
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 43 deletions.
1 change: 0 additions & 1 deletion br/tests/br_foreign_key/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
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;"
Expand Down
2 changes: 0 additions & 2 deletions cmd/explaintest/r/explain_foreign_key.result
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
set @@global.tidb_enable_foreign_key=1;
set @@foreign_key_checks=1;
use test;
drop table if exists t1,t2;
Expand Down Expand Up @@ -165,5 +164,4 @@ Delete N/A root N/A
└─TableReader(Probe) 3333.33 root data:TableRangeScan
└─TableRangeScan 3333.33 cop[tikv] table:t_1 range:(0,+inf], keep order:true, stats:pseudo
drop table if exists t_1,t_2,t_3,t_4;
set @@global.tidb_enable_foreign_key=0;
set @@foreign_key_checks=0;
17 changes: 5 additions & 12 deletions cmd/explaintest/r/tpch.result
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,13 @@ S_NATIONKEY INTEGER NOT NULL,
S_PHONE CHAR(15) NOT NULL,
S_ACCTBAL DECIMAL(15,2) NOT NULL,
S_COMMENT VARCHAR(101) NOT NULL,
PRIMARY KEY (S_SUPPKEY),
CONSTRAINT FOREIGN KEY SUPPLIER_FK1 (S_NATIONKEY) references nation(N_NATIONKEY));
PRIMARY KEY (S_SUPPKEY));
CREATE TABLE IF NOT EXISTS partsupp ( PS_PARTKEY INTEGER NOT NULL,
PS_SUPPKEY INTEGER NOT NULL,
PS_AVAILQTY INTEGER NOT NULL,
PS_SUPPLYCOST DECIMAL(15,2) NOT NULL,
PS_COMMENT VARCHAR(199) NOT NULL,
PRIMARY KEY (PS_PARTKEY,PS_SUPPKEY),
CONSTRAINT FOREIGN KEY PARTSUPP_FK1 (PS_SUPPKEY) references supplier(S_SUPPKEY),
CONSTRAINT FOREIGN KEY PARTSUPP_FK2 (PS_PARTKEY) references part(P_PARTKEY));
PRIMARY KEY (PS_PARTKEY,PS_SUPPKEY));
CREATE TABLE IF NOT EXISTS customer ( C_CUSTKEY INTEGER NOT NULL,
C_NAME VARCHAR(25) NOT NULL,
C_ADDRESS VARCHAR(40) NOT NULL,
Expand All @@ -45,8 +42,7 @@ C_PHONE CHAR(15) NOT NULL,
C_ACCTBAL DECIMAL(15,2) NOT NULL,
C_MKTSEGMENT CHAR(10) NOT NULL,
C_COMMENT VARCHAR(117) NOT NULL,
PRIMARY KEY (C_CUSTKEY),
CONSTRAINT FOREIGN KEY CUSTOMER_FK1 (C_NATIONKEY) references nation(N_NATIONKEY));
PRIMARY KEY (C_CUSTKEY));
CREATE TABLE IF NOT EXISTS orders ( O_ORDERKEY INTEGER NOT NULL,
O_CUSTKEY INTEGER NOT NULL,
O_ORDERSTATUS CHAR(1) NOT NULL,
Expand All @@ -56,8 +52,7 @@ O_ORDERPRIORITY CHAR(15) NOT NULL,
O_CLERK CHAR(15) NOT NULL,
O_SHIPPRIORITY INTEGER NOT NULL,
O_COMMENT VARCHAR(79) NOT NULL,
PRIMARY KEY (O_ORDERKEY),
CONSTRAINT FOREIGN KEY ORDERS_FK1 (O_CUSTKEY) references customer(C_CUSTKEY));
PRIMARY KEY (O_ORDERKEY));
CREATE TABLE IF NOT EXISTS lineitem ( L_ORDERKEY INTEGER NOT NULL,
L_PARTKEY INTEGER NOT NULL,
L_SUPPKEY INTEGER NOT NULL,
Expand All @@ -74,9 +69,7 @@ L_RECEIPTDATE DATE NOT NULL,
L_SHIPINSTRUCT CHAR(25) NOT NULL,
L_SHIPMODE CHAR(10) NOT NULL,
L_COMMENT VARCHAR(44) NOT NULL,
PRIMARY KEY (L_ORDERKEY,L_LINENUMBER),
CONSTRAINT FOREIGN KEY LINEITEM_FK1 (L_ORDERKEY) references orders(O_ORDERKEY),
CONSTRAINT FOREIGN KEY LINEITEM_FK2 (L_PARTKEY,L_SUPPKEY) references partsupp(PS_PARTKEY, PS_SUPPKEY));
PRIMARY KEY (L_ORDERKEY,L_LINENUMBER));
load stats 's/tpch_stats/nation.json';
load stats 's/tpch_stats/region.json';
load stats 's/tpch_stats/part.json';
Expand Down
2 changes: 0 additions & 2 deletions cmd/explaintest/t/explain_foreign_key.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
set @@global.tidb_enable_foreign_key=1;
set @@foreign_key_checks=1;
use test;
drop table if exists t1,t2;
Expand Down Expand Up @@ -43,5 +42,4 @@ set @@foreign_key_checks=0;
explain format = 'brief' update t_1,t_2 set t_1.id=2,t_2.id=2 where t_1.id=t_2.id and t_1.id=1;
explain format = 'brief' delete t_1,t_2 from t_1 join t_2 where t_1.id=t_2.id and t_1.id > 0;
drop table if exists t_1,t_2,t_3,t_4;
set @@global.tidb_enable_foreign_key=0;
set @@foreign_key_checks=0;
17 changes: 5 additions & 12 deletions cmd/explaintest/t/tpch.test
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,14 @@ CREATE TABLE IF NOT EXISTS supplier ( S_SUPPKEY INTEGER NOT NULL,
S_PHONE CHAR(15) NOT NULL,
S_ACCTBAL DECIMAL(15,2) NOT NULL,
S_COMMENT VARCHAR(101) NOT NULL,
PRIMARY KEY (S_SUPPKEY),
CONSTRAINT FOREIGN KEY SUPPLIER_FK1 (S_NATIONKEY) references nation(N_NATIONKEY));
PRIMARY KEY (S_SUPPKEY));

CREATE TABLE IF NOT EXISTS partsupp ( PS_PARTKEY INTEGER NOT NULL,
PS_SUPPKEY INTEGER NOT NULL,
PS_AVAILQTY INTEGER NOT NULL,
PS_SUPPLYCOST DECIMAL(15,2) NOT NULL,
PS_COMMENT VARCHAR(199) NOT NULL,
PRIMARY KEY (PS_PARTKEY,PS_SUPPKEY),
CONSTRAINT FOREIGN KEY PARTSUPP_FK1 (PS_SUPPKEY) references supplier(S_SUPPKEY),
CONSTRAINT FOREIGN KEY PARTSUPP_FK2 (PS_PARTKEY) references part(P_PARTKEY));
PRIMARY KEY (PS_PARTKEY,PS_SUPPKEY));

CREATE TABLE IF NOT EXISTS customer ( C_CUSTKEY INTEGER NOT NULL,
C_NAME VARCHAR(25) NOT NULL,
Expand All @@ -51,8 +48,7 @@ CREATE TABLE IF NOT EXISTS customer ( C_CUSTKEY INTEGER NOT NULL,
C_ACCTBAL DECIMAL(15,2) NOT NULL,
C_MKTSEGMENT CHAR(10) NOT NULL,
C_COMMENT VARCHAR(117) NOT NULL,
PRIMARY KEY (C_CUSTKEY),
CONSTRAINT FOREIGN KEY CUSTOMER_FK1 (C_NATIONKEY) references nation(N_NATIONKEY));
PRIMARY KEY (C_CUSTKEY));

CREATE TABLE IF NOT EXISTS orders ( O_ORDERKEY INTEGER NOT NULL,
O_CUSTKEY INTEGER NOT NULL,
Expand All @@ -63,8 +59,7 @@ CREATE TABLE IF NOT EXISTS orders ( O_ORDERKEY INTEGER NOT NULL,
O_CLERK CHAR(15) NOT NULL,
O_SHIPPRIORITY INTEGER NOT NULL,
O_COMMENT VARCHAR(79) NOT NULL,
PRIMARY KEY (O_ORDERKEY),
CONSTRAINT FOREIGN KEY ORDERS_FK1 (O_CUSTKEY) references customer(C_CUSTKEY));
PRIMARY KEY (O_ORDERKEY));

CREATE TABLE IF NOT EXISTS lineitem ( L_ORDERKEY INTEGER NOT NULL,
L_PARTKEY INTEGER NOT NULL,
Expand All @@ -82,9 +77,7 @@ CREATE TABLE IF NOT EXISTS lineitem ( L_ORDERKEY INTEGER NOT NULL,
L_SHIPINSTRUCT CHAR(25) NOT NULL,
L_SHIPMODE CHAR(10) NOT NULL,
L_COMMENT VARCHAR(44) NOT NULL,
PRIMARY KEY (L_ORDERKEY,L_LINENUMBER),
CONSTRAINT FOREIGN KEY LINEITEM_FK1 (L_ORDERKEY) references orders(O_ORDERKEY),
CONSTRAINT FOREIGN KEY LINEITEM_FK2 (L_PARTKEY,L_SUPPKEY) references partsupp(PS_PARTKEY, PS_SUPPKEY));
PRIMARY KEY (L_ORDERKEY,L_LINENUMBER));
-- load stats.
load stats 's/tpch_stats/nation.json';
load stats 's/tpch_stats/region.json';
Expand Down
2 changes: 1 addition & 1 deletion ddl/cancel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ var allTestCase = []testCancelJob{
{"alter table t modify column c11 char(10)", true, model.StateWriteReorganization, true, true, nil},
{"alter table t modify column c11 char(10)", false, model.StatePublic, false, true, nil},
// Add foreign key.
{"alter table t add constraint fk foreign key a(c1) references t_ref(c1)", true, model.StateNone, true, false, []string{"create table t_ref (c1 int, c2 int, c3 int, c11 tinyint);"}},
{"alter table t add constraint fk foreign key a(c1) references t_ref(c1)", true, model.StateNone, true, false, []string{"create table t_ref (c1 int key, c2 int, c3 int, c11 tinyint);"}},
{"alter table t add constraint fk foreign key a(c1) references t_ref(c1)", false, model.StatePublic, false, true, nil},
// Drop foreign key.
{"alter table t drop foreign key fk", true, model.StatePublic, true, false, nil},
Expand Down
2 changes: 1 addition & 1 deletion ddl/db_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestTableForeignKey(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t1 (a int, b int);")
tk.MustExec("create table t1 (a int, b int, index(a), index(b));")
// test create table with foreign key.
failSQL := "create table t2 (c int, foreign key (a) references t1(a));"
tk.MustGetErrCode(failSQL, errno.ErrKeyColumnDoesNotExits)
Expand Down
13 changes: 12 additions & 1 deletion ddl/foreign_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/tidb/sessiontxn"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/types"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -107,7 +108,17 @@ func TestForeignKey(t *testing.T) {
testCreateSchema(t, testkit.NewTestKit(t, store).Session(), dom.DDL(), dbInfo)
tblInfo, err := testTableInfo(store, "t", 3)
require.NoError(t, err)

tblInfo.Indices = append(tblInfo.Indices, &model.IndexInfo{
ID: 1,
Name: model.NewCIStr("idx_fk"),
Table: model.NewCIStr("t"),
Columns: []*model.IndexColumn{{
Name: model.NewCIStr("c1"),
Offset: 0,
Length: types.UnspecifiedLength,
}},
State: model.StatePublic,
})
testCreateTable(t, testkit.NewTestKit(t, store).Session(), d, dbInfo, tblInfo)

// fix data race
Expand Down
2 changes: 1 addition & 1 deletion ddl/restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestStat(t *testing.T) {
SchemaID: dbInfo.ID,
Type: model.ActionDropSchema,
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{dbInfo.Name},
Args: []interface{}{true},
}

done := make(chan error, 1)
Expand Down
4 changes: 2 additions & 2 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func TestCreateTableWithLikeAtTemporaryMode(t *testing.T) {

// Test foreign key.
tk.MustExec("drop table if exists test_foreign_key, t1")
tk.MustExec("create table t1 (a int, b int)")
tk.MustExec("create table t1 (a int, b int, index(b))")
tk.MustExec("create table test_foreign_key (c int,d int,foreign key (d) references t1 (b))")
defer tk.MustExec("drop table if exists test_foreign_key, t1")
tk.MustExec("create global temporary table test_foreign_key_temp like test_foreign_key on commit delete rows")
Expand Down Expand Up @@ -382,7 +382,7 @@ func TestCreateTableWithLikeAtTemporaryMode(t *testing.T) {
defer tk.MustExec("drop table if exists partition_table, tmp_partition_table")

tk.MustExec("drop table if exists foreign_key_table1, foreign_key_table2, foreign_key_tmp")
tk.MustExec("create table foreign_key_table1 (a int, b int)")
tk.MustExec("create table foreign_key_table1 (a int, b int, index(b))")
tk.MustExec("create table foreign_key_table2 (c int,d int,foreign key (d) references foreign_key_table1 (b))")
tk.MustExec("create temporary table foreign_key_tmp like foreign_key_table2")
is = sessiontxn.GetTxnManager(tk.Session()).GetTxnInfoSchema()
Expand Down
1 change: 1 addition & 0 deletions executor/fktest/foreign_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1969,6 +1969,7 @@ func TestShowCreateTableWithForeignKey(t *testing.T) {
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")

tk.MustExec("set @@global.tidb_enable_foreign_key=0")
tk.MustExec("create table t1 (id int key, leader int, leader2 int, index(leader), index(leader2), constraint fk foreign key (leader) references t1(id) ON DELETE CASCADE ON UPDATE SET NULL);")
tk.MustQuery("show create table t1").Check(testkit.Rows("t1 CREATE TABLE `t1` (\n" +
" `id` int(11) NOT NULL,\n" +
Expand Down
6 changes: 3 additions & 3 deletions executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -809,9 +809,9 @@ func TestSetVar(t *testing.T) {

// test variable 'foreign_key_checks'
// global scope
tk.MustQuery("select @@global.tidb_enable_foreign_key").Check(testkit.Rows("0")) // default value
tk.MustExec("set global tidb_enable_foreign_key = 1")
tk.MustQuery("select @@global.tidb_enable_foreign_key").Check(testkit.Rows("1"))
tk.MustQuery("select @@global.tidb_enable_foreign_key").Check(testkit.Rows("1")) // default value
tk.MustExec("set global tidb_enable_foreign_key = 0")
tk.MustQuery("select @@global.tidb_enable_foreign_key").Check(testkit.Rows("0"))

// test variable 'tidb_opt_force_inline_cte'
tk.MustQuery("select @@session.tidb_opt_force_inline_cte").Check(testkit.Rows("0")) // default value is 0
Expand Down
7 changes: 4 additions & 3 deletions executor/showtest/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func TestShowCreateTable(t *testing.T) {
" `parent_id` int(11) NOT NULL,\n"+
" PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,\n"+
" KEY `par_ind` (`parent_id`),\n"+
" CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `test`.`parent` (`id`) /* FOREIGN KEY INVALID */\n"+
" CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `test`.`parent` (`id`)\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
))

Expand All @@ -345,7 +345,7 @@ func TestShowCreateTable(t *testing.T) {
" `parent_id` int(11) NOT NULL,\n"+
" PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,\n"+
" KEY `par_ind` (`parent_id`),\n"+
" CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `test`.`parent` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE /* FOREIGN KEY INVALID */\n"+
" CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `test`.`parent` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
))

Expand All @@ -358,7 +358,8 @@ func TestShowCreateTable(t *testing.T) {
" `id` int(11) NOT NULL,\n" +
" `b` int(11) DEFAULT NULL,\n" +
" PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,\n" +
" CONSTRAINT `fk` FOREIGN KEY (`b`) REFERENCES `test1`.`t1` (`id`) /* FOREIGN KEY INVALID */\n" +
" KEY `fk` (`b`),\n" +
" CONSTRAINT `fk` FOREIGN KEY (`b`) REFERENCES `test1`.`t1` (`id`)\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))

// Test issue #20327
Expand Down
2 changes: 1 addition & 1 deletion sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,7 @@ var defaultSysVars = []*SysVar{
}
return normalizedValue, ErrWrongValueForVar.GenWithStackByArgs(ForeignKeyChecks, originalValue)
}},
{Scope: ScopeGlobal, Name: TiDBEnableForeignKey, Value: BoolToOnOff(false), Type: TypeBool, SetGlobal: func(_ context.Context, s *SessionVars, val string) error {
{Scope: ScopeGlobal, Name: TiDBEnableForeignKey, Value: BoolToOnOff(true), Type: TypeBool, SetGlobal: func(_ context.Context, s *SessionVars, val string) error {
EnableForeignKey.Store(TiDBOptOn(val))
return nil
}, GetGlobal: func(_ context.Context, s *SessionVars) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ var (
// DDLDiskQuota is the temporary variable for set disk quota for lightning
DDLDiskQuota = atomic.NewUint64(DefTiDBDDLDiskQuota)
// EnableForeignKey indicates whether to enable foreign key feature.
EnableForeignKey = atomic.NewBool(false)
EnableForeignKey = atomic.NewBool(true)
EnableRCReadCheckTS = atomic.NewBool(false)

// DefTiDBServerMemoryLimit indicates the default value of TiDBServerMemoryLimit(TotalMem * 80%).
Expand Down

0 comments on commit b33ff62

Please sign in to comment.