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

session: enable foreign_key_checks/tidb_enable_foreign_key by default when upgrade #41424

Merged
merged 9 commits into from
Feb 15, 2023
16 changes: 15 additions & 1 deletion session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,11 +794,15 @@ const (
version111 = 111
// version112 modifies the view tidb_mdl_view
version112 = 112
// version113 modifies the following global variables default value:
// - foreign_key_checks: off -> on
// - tidb_enable_foreign_key: off -> on
version113 = 113
)

// currentBootstrapVersion is defined as a variable, so we can modify its value for testing.
// please make sure this is the largest version
var currentBootstrapVersion int64 = version112
var currentBootstrapVersion int64 = version113

// DDL owner key's expired time is ManagerSessionTTL seconds, we should wait the time and give more time to have a chance to finish it.
var internalSQLTimeout = owner.ManagerSessionTTL + 15
Expand Down Expand Up @@ -918,6 +922,7 @@ var (
upgradeToVer110,
upgradeToVer111,
upgradeToVer112,
upgradeToVer113,
}
)

Expand Down Expand Up @@ -2285,6 +2290,15 @@ func upgradeToVer112(s Session, ver int64) {
doReentrantDDL(s, CreateMDLView)
}

func upgradeToVer113(s Session, ver int64) {
if ver >= version113 {
return
}

mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.ForeignKeyChecks, variable.On)
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved
mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableForeignKey, variable.On)
}

func writeOOMAction(s Session) {
comment := "oom-action is `log` by default in v3.0.x, `cancel` by default in v4.0.11+"
mustExecute(s, `INSERT HIGH_PRIORITY INTO %n.%n VALUES (%?, %?, %?) ON DUPLICATE KEY UPDATE VARIABLE_VALUE= %?`,
Expand Down
61 changes: 61 additions & 0 deletions session/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1417,3 +1417,64 @@ func TestTiDBGCAwareUpgradeFrom630To650(t *testing.T) {
require.Equal(t, 2, row.Len())
require.Equal(t, "0", row.GetString(1))
}

func TestTiDBGlobalVariablesDefaultValueUpgradeFrom630To660(t *testing.T) {
ctx := context.Background()
store, _ := createStoreAndBootstrap(t)
defer func() { require.NoError(t, store.Close()) }()

// upgrade from 6.3.0 to 6.6.0.
ver630 := version93
seV630 := createSessionAndSetID(t, store)
txn, err := store.Begin()
require.NoError(t, err)
m := meta.NewMeta(txn)
err = m.FinishBootstrap(int64(ver630))
require.NoError(t, err)
err = txn.Commit(context.Background())
require.NoError(t, err)
mustExec(t, seV630, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver630))
mustExec(t, seV630, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "OFF", variable.TiDBEnableForeignKey))
mustExec(t, seV630, fmt.Sprintf("update mysql.GLOBAL_VARIABLES set variable_value='%s' where variable_name='%s'", "OFF", variable.ForeignKeyChecks))
mustExec(t, seV630, "commit")
unsetStoreBootstrapped(store.UUID())
ver, err := getBootstrapVersion(seV630)
require.NoError(t, err)
require.Equal(t, int64(ver630), ver)

// We are now in 6.3.0.
upgradeVars := []string{variable.TiDBEnableForeignKey, variable.ForeignKeyChecks}
varsValueList := []string{"OFF", "OFF"}
for i := range upgradeVars {
res := mustExecToRecodeSet(t, seV630, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", upgradeVars[i]))
chk := res.NewChunk(nil)
err = res.Next(ctx, chk)
require.NoError(t, err)
require.Equal(t, 1, chk.NumRows())
row := chk.GetRow(0)
require.Equal(t, 2, row.Len())
require.Equal(t, varsValueList[i], row.GetString(1))
}

// Upgrade to 6.6.0.
domCurVer, err := BootstrapSession(store)
require.NoError(t, err)
defer domCurVer.Close()
seV660 := createSessionAndSetID(t, store)
ver, err = getBootstrapVersion(seV660)
require.NoError(t, err)
require.Equal(t, currentBootstrapVersion, ver)

// We are now in 6.6.0.
varsValueList = []string{"ON", "ON"}
for i := range upgradeVars {
res := mustExecToRecodeSet(t, seV660, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", upgradeVars[i]))
chk := res.NewChunk(nil)
err = res.Next(ctx, chk)
require.NoError(t, err)
require.Equal(t, 1, chk.NumRows())
row := chk.GetRow(0)
require.Equal(t, 2, row.Len())
require.Equal(t, varsValueList[i], row.GetString(1))
}
}