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

sessionctx: Set foreign_key_checks = OFF #8358

Merged
merged 12 commits into from
Nov 26, 2018
8 changes: 8 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3693,6 +3693,14 @@ func (s *testIntegrationSuite) TestValuesInNonInsertStmt(c *C) {
res.Check(testkit.Rows(`<nil> <nil> <nil> <nil> <nil> <nil> <nil>`))
}

func (s *testIntegrationSuite) TestForeignKeyVar(c *C) {

tk := testkit.NewTestKit(c, s.store)

tk.MustExec("SET FOREIGN_KEY_CHECKS=1")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1105 variable 'foreign_key_checks' does not yet support value: 1"))
}

func (s *testIntegrationSuite) TestUserVarMockWindFunc(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test;`)
Expand Down
19 changes: 10 additions & 9 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,15 @@ const (

// Variable errors
var (
UnknownStatusVar = terror.ClassVariable.New(CodeUnknownStatusVar, "unknown status variable")
UnknownSystemVar = terror.ClassVariable.New(CodeUnknownSystemVar, mysql.MySQLErrName[mysql.ErrUnknownSystemVariable])
ErrIncorrectScope = terror.ClassVariable.New(CodeIncorrectScope, mysql.MySQLErrName[mysql.ErrIncorrectGlobalLocalVar])
ErrUnknownTimeZone = terror.ClassVariable.New(CodeUnknownTimeZone, mysql.MySQLErrName[mysql.ErrUnknownTimeZone])
ErrReadOnly = terror.ClassVariable.New(CodeReadOnly, "variable is read only")
ErrWrongValueForVar = terror.ClassVariable.New(CodeWrongValueForVar, mysql.MySQLErrName[mysql.ErrWrongValueForVar])
ErrWrongTypeForVar = terror.ClassVariable.New(CodeWrongTypeForVar, mysql.MySQLErrName[mysql.ErrWrongTypeForVar])
ErrTruncatedWrongValue = terror.ClassVariable.New(CodeTruncatedWrongValue, mysql.MySQLErrName[mysql.ErrTruncatedWrongValue])
UnknownStatusVar = terror.ClassVariable.New(CodeUnknownStatusVar, "unknown status variable")
UnknownSystemVar = terror.ClassVariable.New(CodeUnknownSystemVar, mysql.MySQLErrName[mysql.ErrUnknownSystemVariable])
ErrIncorrectScope = terror.ClassVariable.New(CodeIncorrectScope, mysql.MySQLErrName[mysql.ErrIncorrectGlobalLocalVar])
ErrUnknownTimeZone = terror.ClassVariable.New(CodeUnknownTimeZone, mysql.MySQLErrName[mysql.ErrUnknownTimeZone])
ErrReadOnly = terror.ClassVariable.New(CodeReadOnly, "variable is read only")
ErrWrongValueForVar = terror.ClassVariable.New(CodeWrongValueForVar, mysql.MySQLErrName[mysql.ErrWrongValueForVar])
ErrWrongTypeForVar = terror.ClassVariable.New(CodeWrongTypeForVar, mysql.MySQLErrName[mysql.ErrWrongTypeForVar])
ErrTruncatedWrongValue = terror.ClassVariable.New(CodeTruncatedWrongValue, mysql.MySQLErrName[mysql.ErrTruncatedWrongValue])
ErrUnsupportedValueForVar = terror.ClassVariable.New(CodeUnknownStatusVar, "variable '%s' does not yet support value: %s")
)

func init() {
Expand Down Expand Up @@ -221,7 +222,7 @@ var defaultSysVars = []*SysVar{
{ScopeNone, "innodb_autoinc_lock_mode", "1"},
{ScopeGlobal, "slave_net_timeout", "3600"},
{ScopeGlobal, "key_buffer_size", "8388608"},
{ScopeGlobal | ScopeSession, ForeignKeyChecks, "1"},
{ScopeGlobal | ScopeSession, ForeignKeyChecks, "OFF"},
{ScopeGlobal, "host_cache_size", "279"},
{ScopeGlobal, DelayKeyWrite, "ON"},
{ScopeNone, "metadata_locks_cache_size", "1024"},
Expand Down
12 changes: 11 additions & 1 deletion sessionctx/variable/varsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ func ValidateSetSystemVar(vars *SessionVars, name string, value string) (string,
return value, ErrWrongValueForVar.GenWithStackByArgs(name, value)
case FlushTime:
return checkUInt64SystemVar(name, value, 0, secondsPerYear, vars)
case ForeignKeyChecks:
if strings.EqualFold(value, "ON") || value == "1" {
// TiDB does not yet support foreign keys.
// For now, resist the change and show a warning.
vars.StmtCtx.AppendWarning(ErrUnsupportedValueForVar.GenWithStackByArgs(name, value))
return "OFF", nil
} else if strings.EqualFold(value, "OFF") || value == "0" {
return "OFF", nil
}
return value, ErrWrongValueForVar.GenWithStackByArgs(name, value)
case GroupConcatMaxLen:
// The reasonable range of 'group_concat_max_len' is 4~18446744073709551615(64-bit platforms)
// See https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_group_concat_max_len for details
Expand Down Expand Up @@ -288,7 +298,7 @@ func ValidateSetSystemVar(vars *SessionVars, name string, value string) (string,
case WarningCount, ErrorCount:
return value, ErrReadOnly.GenWithStackByArgs(name)
case GeneralLog, TiDBGeneralLog, AvoidTemporalUpgrade, BigTables, CheckProxyUsers, CoreFile, EndMakersInJSON, SQLLogBin, OfflineMode,
PseudoSlaveMode, LowPriorityUpdates, SkipNameResolve, ForeignKeyChecks, SQLSafeUpdates, TiDBConstraintCheckInPlace:
PseudoSlaveMode, LowPriorityUpdates, SkipNameResolve, SQLSafeUpdates, TiDBConstraintCheckInPlace:
if strings.EqualFold(value, "ON") || value == "1" {
return "1", nil
} else if strings.EqualFold(value, "OFF") || value == "0" {
Expand Down
10 changes: 10 additions & 0 deletions sessionctx/variable/varsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ func (s *testVarsutilSuite) TestVarsutil(c *C) {
c.Assert(val, Equals, "1")
c.Assert(SetSessionSystemVar(v, "autocommit", types.Datum{}), NotNil)

// 0 converts to OFF
SetSessionSystemVar(v, "foreign_key_checks", types.NewStringDatum("0"))
val, err = GetSessionSystemVar(v, "foreign_key_checks")
c.Assert(val, Equals, "OFF")

// 1/ON is not supported (generates a warning and sets to OFF)
SetSessionSystemVar(v, "foreign_key_checks", types.NewStringDatum("1"))
val, err = GetSessionSystemVar(v, "foreign_key_checks")
c.Assert(val, Equals, "OFF")

SetSessionSystemVar(v, "sql_mode", types.NewStringDatum("strict_trans_tables"))
val, err = GetSessionSystemVar(v, "sql_mode")
c.Assert(err, IsNil)
Expand Down