Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: enable tidb_enable_foreign_key variable default #39677

Merged
merged 15 commits into from
Dec 7, 2022
Merged
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;
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;
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{&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
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
2 changes: 1 addition & 1 deletion sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,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 @@ -1185,7 +1185,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