Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(PITR): add check functions and tests for binlog (#1488)
Yilong Li1859d7748dcf
diff --git a/plugin/restore/mysql/mysql.go b/plugin/restore/mysql/mysql.goindex 289c2157c1..ec2a41e63e 100644--- a/plugin/restore/mysql/mysql.go+++ b/plugin/restore/mysql/mysql.go@@ -673,6 +673,18 @@ func checkVersionForPITR(version string) error {return nil}+// CheckServerVersionForPITR checks that the MySQL server version meets the requirements of PITR.+func (r *Restore) CheckServerVersionForPITR(ctx context.Context) error {+ value, err := r.getServerVariable(ctx, "version")+ if err != nil {+ return err+ }+ if err := checkVersionForPITR(value); err != nil {+ return err+ }+ return nil+}+// CheckEngineInnoDB checks that the tables in the database is all using InnoDB as the storage engine.func (r *Restore) CheckEngineInnoDB(ctx context.Context, database string) error {db, err := r.driver.GetDbConnection(ctx, "")@@ -702,6 +714,53 @@ func (r *Restore) CheckEngineInnoDB(ctx context.Context, database string) errorreturn nil}+func (r *Restore) getServerVariable(ctx context.Context, varName string) (string, error) {+ db, err := r.driver.GetDbConnection(ctx, "")+ if err != nil {+ return "", err+ }++ rows, err := db.QueryContext(ctx, fmt.Sprintf("SHOW VARIABLES LIKE '%s';", varName))+ if err != nil {+ return "", err+ }+ if ok := rows.Next(); !ok {+ return "", fmt.Errorf("SHOW VARIABLES LIKE '%s' returns empty set", varName)+ }+ var varNameFound, value string+ if err := rows.Scan(&varNameFound, &value); err != nil {+ return "", err+ }+ if varName != varNameFound {+ return "", fmt.Errorf("expecting variable %s, but got %s", varName, varNameFound)+ }+ return value, nil+}++// CheckBinlogEnabled checks whether binlog is enabled for the current instance.+func (r *Restore) CheckBinlogEnabled(ctx context.Context) error {+ value, err := r.getServerVariable(ctx, "log_bin")+ if err != nil {+ return err+ }+ if strings.ToUpper(value) != "ON" {+ return fmt.Errorf("binlog is not enabled")+ }+ return nil+}++// CheckBinlogRowFormat checks whether the binlog format is ROW.+func (r *Restore) CheckBinlogRowFormat(ctx context.Context) error {+ value, err := r.getServerVariable(ctx, "binlog_format")+ if err != nil {+ return err+ }+ if strings.ToUpper(value) != "ROW" {+ return fmt.Errorf("binlog format is not ROW but %s", value)+ }+ return nil+}+// getDateTime returns converts the targetTs to the local date-time.func getDateTime(targetTs int64) string {t := time.Unix(targetTs, 0)diff --git a/tests/mysql_test.go b/tests/mysql_test.goindex 752c7e0604..e387d0b762 100644--- a/tests/mysql_test.go+++ b/tests/mysql_test.go@@ -9,7 +9,6 @@ import (pluginmysql "github.com/bytebase/bytebase/plugin/db/mysql"restoremysql "github.com/bytebase/bytebase/plugin/restore/mysql"resourcemysql "github.com/bytebase/bytebase/resources/mysql"- "github.com/bytebase/bytebase/resources/mysqlutil""github.com/stretchr/testify/require")@@ -20,11 +19,6 @@ func TestCheckEngineInnoDB(t *testing.T) {port := getTestPort(t.Name())- t.Log("install mysqlbinlog binary")- tmpDir := t.TempDir()- mysqlutilInstance, err := mysqlutil.Install(tmpDir)- a.NoError(err)-t.Run("success", func(t *testing.T) {port := portt.Parallel()@@ -51,7 +45,7 @@ func TestCheckEngineInnoDB(t *testing.T) {mysqlDriver, ok := driver.(*pluginmysql.Driver)a.Equal(true, ok)- mysqlRestore := restoremysql.New(mysqlDriver, mysqlutilInstance, connCfg, "" /*binlog directory*/)+ mysqlRestore := restoremysql.New(mysqlDriver, nil, connCfg, "" /*binlog directory*/)err = mysqlRestore.CheckEngineInnoDB(ctx, database)a.NoError(err)})@@ -85,9 +79,44 @@ func TestCheckEngineInnoDB(t *testing.T) {mysqlDriver, ok := driver.(*pluginmysql.Driver)a.Equal(true, ok)- mysqlRestore := restoremysql.New(mysqlDriver, mysqlutilInstance, connCfg, "" /*binlog directory*/)+ mysqlRestore := restoremysql.New(mysqlDriver, nil, connCfg, "" /*binlog directory*/)err = mysqlRestore.CheckEngineInnoDB(ctx, database)a.Error(err)})}++func TestCheckServerVersionAndBinlogForPITR(t *testing.T) {+ t.Parallel()+ a := require.New(t)+ ctx := context.Background()++ port := getTestPort(t.Name())+ _, stopFn := resourcemysql.SetupTestInstance(t, port)+ defer stopFn()++ db, err := connectTestMySQL(port, "")+ a.NoError(err)+ defer db.Close()++ driver, err := getTestMySQLDriver(ctx, strconv.Itoa(port), "")+ a.NoError(err)+ defer driver.Close(ctx)++ connCfg := getMySQLConnectionConfig(strconv.Itoa(port), "")+ mysqlDriver, ok := driver.(*pluginmysql.Driver)+ a.Equal(true, ok)+ mysqlRestore := restoremysql.New(mysqlDriver, nil, connCfg, "" /*binlog directory*/)++ // the test MySQL instance is 8.0.+ err = mysqlRestore.CheckServerVersionForPITR(ctx)+ a.NoError(err)++ // binlog is default to ON in MySQL 8.0.+ err = mysqlRestore.CheckBinlogEnabled(ctx)+ a.NoError(err)++ // binlog format is default to ROW in MySQL 8.0.+ err = mysqlRestore.CheckBinlogRowFormat(ctx)+ a.NoError(err)+}diff --git a/tests/tests.go b/tests/tests.goindex e10a2e32f2..dc87e17412 100644--- a/tests/tests.go+++ b/tests/tests.go@@ -127,6 +127,8 @@ func getTestPort(testName string) int {"TestBootWithExternalPg","TestSheetVCS","TestCheckEngineInnoDB",+ "TestCheckServerVersionForPITR",+ "TestCheckServerVersionAndBinlogForPITR","TestSchemaSystem",}port := 1234