Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: refactor mysql dump (#6705)
Danny Xu07e095beccff
diff --git a/backend/plugin/db/mysql/dump.go b/backend/plugin/db/mysql/dump.goindex 976e1bc9d4..b1b52d882a 100644--- a/backend/plugin/db/mysql/dump.go+++ b/backend/plugin/db/mysql/dump.go@@ -26,12 +26,7 @@ import (// Dump and restore.const (- databaseHeaderFmt = "" +- "--\n" +- "-- MySQL database structure for `%s`\n" +- "--\n"- useDatabaseFmt = "USE `%s`;\n\n"- settingsStmt = "" ++ settingsStmt = "" +"SET character_set_client = %s;\n" +"SET character_set_results = %s;\n" +"SET collation_connection = %s;\n" +@@ -142,7 +137,7 @@ func (driver *Driver) Dump(ctx context.Context, out io.Writer, schemaOnly bool)defer txn.Rollback()log.Debug("begin to dump database", zap.String("database", driver.databaseName), zap.Bool("schemaOnly", schemaOnly))- if err := dumpTxn(ctx, txn, driver.dbType, driver.databaseName, out, schemaOnly); err != nil {+ if err := dumpTxn(txn, driver.dbType, driver.databaseName, out, schemaOnly); err != nil {return "", err}@@ -189,164 +184,111 @@ func FlushTablesWithReadLock(ctx context.Context, dbType db.Type, conn *sql.Connreturn txn.Commit()}-func dumpTxn(ctx context.Context, txn *sql.Tx, dbType db.Type, database string, out io.Writer, schemaOnly bool) error {- // Find all dumpable databases- dbNames, err := getDatabases(ctx, txn)- if err != nil {- return errors.Wrap(err, "failed to get databases")+func dumpTxn(txn *sql.Tx, dbType db.Type, database string, out io.Writer, schemaOnly bool) error {+ // Disable foreign key check.+ // mysqldump uses the same mechanism. When there is any schema or data dependency, we have to disable+ // the unique and foreign key check so that the restoring will not fail.+ if _, err := io.WriteString(out, disableUniqueAndForeignKeyCheckStmt); err != nil {+ return err}- var dumpableDbNames []string- if database != "" {- exist := false- for _, n := range dbNames {- if n == database {- exist = true- break- }- }- if !exist {- return common.Errorf(common.NotFound, "database %s not found", database)+ // Table and view statement.+ // We have to dump the table before views because of the structure dependency.+ tables, err := getTablesTx(txn, dbType, database)+ if err != nil {+ return errors.Wrapf(err, "failed to get tables of database %q", database)+ }+ // Construct temporal views.+ // Create a temporary view with the same name as the view and with columns of+ // the same name in order to satisfy views that depend on this view.+ // This temporary view will be removed when the actual view is created.+ // The properties of each column, are not preserved in this temporary+ // view. They are not necessary because other views only need to reference+ // the column name, thus we generate SELECT 1 AS colName1, 1 AS colName2.+ // This will not be necessary once we can determine dependencies+ // between views and can simply dump them in the appropriate order.+ // https://sourcegraph.com/github.com/mysql/mysql-server/-/blob/client/mysqldump.cc?L2781+ for _, tbl := range tables {+ if tbl.TableType != viewTableType {+ continue}- dumpableDbNames = []string{database}- } else {- for _, dbName := range dbNames {- if systemDatabases[dbName] {- continue- }- dumpableDbNames = append(dumpableDbNames, dbName)+ if _, err := io.WriteString(out, fmt.Sprintf("%s\n", getTemporaryView(tbl.Name, tbl.ViewColumns))); err != nil {+ return err}}-- for _, dbName := range dumpableDbNames {- // Include "USE DATABASE xxx" if dumping multiple databases.- if len(dumpableDbNames) > 1 {- // Database header.- header := fmt.Sprintf(databaseHeaderFmt, dbName)- if _, err := io.WriteString(out, header); err != nil {- return err- }- dbStmt, err := getDatabaseStmt(txn, dbName)- if err != nil {- return errors.Wrapf(err, "failed to get database %q", dbName)- }- if _, err := io.WriteString(out, dbStmt); err != nil {- return err- }- // Use database statement.- useStmt := fmt.Sprintf(useDatabaseFmt, dbName)- if _, err := io.WriteString(out, useStmt); err != nil {- return err- }+ // Construct tables.+ for _, tbl := range tables {+ if tbl.TableType == viewTableType {+ continue}-- // Disable foreign key check.- // mysqldump uses the same mechanism. When there is any schema or data dependency, we have to disable- // the unique and foreign key check so that the restoring will not fail.- if _, err := io.WriteString(out, disableUniqueAndForeignKeyCheckStmt); err != nil {- return err+ if schemaOnly {+ tbl.Statement = excludeSchemaAutoIncrementValue(tbl.Statement)}-- // Table and view statement.- // We have to dump the table before views because of the structure dependency.- tables, err := getTablesTx(txn, dbType, dbName)- if err != nil {- return errors.Wrapf(err, "failed to get tables of database %q", dbName)- }- // Construct temporal views.- // Create a temporary view with the same name as the view and with columns of- // the same name in order to satisfy views that depend on this view.- // This temporary view will be removed when the actual view is created.- // The properties of each column, are not preserved in this temporary- // view. They are not necessary because other views only need to reference- // the column name, thus we generate SELECT 1 AS colName1, 1 AS colName2.- // This will not be necessary once we can determine dependencies- // between views and can simply dump them in the appropriate order.- // https://sourcegraph.com/github.com/mysql/mysql-server/-/blob/client/mysqldump.cc?L2781- for _, tbl := range tables {- if tbl.TableType != viewTableType {- continue- }- if _, err := io.WriteString(out, fmt.Sprintf("%s\n", getTemporaryView(tbl.Name, tbl.ViewColumns))); err != nil {- return err- }+ if _, err := io.WriteString(out, fmt.Sprintf("%s\n", tbl.Statement)); err != nil {+ return err}- // Construct tables.- for _, tbl := range tables {- if tbl.TableType == viewTableType {- continue- }- if schemaOnly {- tbl.Statement = excludeSchemaAutoIncrementValue(tbl.Statement)- }- if _, err := io.WriteString(out, fmt.Sprintf("%s\n", tbl.Statement)); err != nil {+ if !schemaOnly && tbl.TableType == baseTableType {+ if err := exportTableData(txn, database, tbl.Name, out); err != nil {return err}- if !schemaOnly && tbl.TableType == baseTableType {- // Include db prefix if dumping multiple databases.- includeDbPrefix := len(dumpableDbNames) > 1- if err := exportTableData(txn, dbName, tbl.Name, includeDbPrefix, out); err != nil {- return err- }- }}- // Construct final views.- for _, tbl := range tables {- if tbl.TableType != viewTableType {- continue- }- // The temporary view just created above were used to satisfy the schema dependency. See comment above.- // We have to drop the temporary and incorrect view here to recreate the final and correct one.- if _, err := io.WriteString(out, fmt.Sprintf("DROP VIEW IF EXISTS `%s`;\n", tbl.Name)); err != nil {- return err- }- if _, err := io.WriteString(out, fmt.Sprintf("%s\n", tbl.Statement)); err != nil {- return err- }+ }+ // Construct final views.+ for _, tbl := range tables {+ if tbl.TableType != viewTableType {+ continue}-- // Procedure and function (routine) statements.- routines, err := getRoutines(txn, dbType, dbName)- if err != nil {- return errors.Wrapf(err, "failed to get routines of database %q", dbName)+ // The temporary view just created above were used to satisfy the schema dependency. See comment above.+ // We have to drop the temporary and incorrect view here to recreate the final and correct one.+ if _, err := io.WriteString(out, fmt.Sprintf("DROP VIEW IF EXISTS `%s`;\n", tbl.Name)); err != nil {+ return err}- for _, rt := range routines {- if _, err := io.WriteString(out, fmt.Sprintf("%s\n", rt.statement)); err != nil {- return err- }+ if _, err := io.WriteString(out, fmt.Sprintf("%s\n", tbl.Statement)); err != nil {+ return err}+ }- // OceanBase doesn't support "Event Scheduler"- if dbType != db.OceanBase {- // Event statements.- events, err := getEvents(txn, dbName)- if err != nil {- return errors.Wrapf(err, "failed to get events of database %q", dbName)- }- for _, et := range events {- if _, err := io.WriteString(out, fmt.Sprintf("%s\n", et.statement)); err != nil {- return err- }- }+ // Procedure and function (routine) statements.+ routines, err := getRoutines(txn, dbType, database)+ if err != nil {+ return errors.Wrapf(err, "failed to get routines of database %q", database)+ }+ for _, rt := range routines {+ if _, err := io.WriteString(out, fmt.Sprintf("%s\n", rt.statement)); err != nil {+ return err}+ }- // Trigger statements.- triggers, err := getTriggers(txn, dbType, dbName)+ // OceanBase doesn't support "Event Scheduler"+ if dbType != db.OceanBase {+ // Event statements.+ events, err := getEvents(txn, database)if err != nil {- return errors.Wrapf(err, "failed to get triggers of database %q", dbName)+ return errors.Wrapf(err, "failed to get events of database %q", database)}- for _, tr := range triggers {- if _, err := io.WriteString(out, fmt.Sprintf("%s\n", tr.statement)); err != nil {+ for _, et := range events {+ if _, err := io.WriteString(out, fmt.Sprintf("%s\n", et.statement)); err != nil {return err}}+ }- // Restore foreign key check.- if _, err := io.WriteString(out, restoreUniqueAndForeignKeyCheckStmt); err != nil {+ // Trigger statements.+ triggers, err := getTriggers(txn, dbType, database)+ if err != nil {+ return errors.Wrapf(err, "failed to get triggers of database %q", database)+ }+ for _, tr := range triggers {+ if _, err := io.WriteString(out, fmt.Sprintf("%s\n", tr.statement)); err != nil {return err}}+ // Restore foreign key check.+ if _, err := io.WriteString(out, restoreUniqueAndForeignKeyCheckStmt); err != nil {+ return err+ }+return nil}@@ -428,19 +370,6 @@ func GetBinlogInfo(ctx context.Context, conn *sql.Conn) (api.BinlogInfo, error)return binlogInfo, nil}-// getDatabaseStmt gets the create statement of a database.-func getDatabaseStmt(txn *sql.Tx, dbName string) (string, error) {- query := fmt.Sprintf("SHOW CREATE DATABASE IF NOT EXISTS `%s`;", dbName)- var stmt, unused string- if err := txn.QueryRow(query).Scan(&unused, &stmt); err != nil {- if err == sql.ErrNoRows {- return "", common.FormatDBErrorEmptyRowWithQuery(query)- }- return "", err- }- return fmt.Sprintf("%s;\n", stmt), nil-}-// TableSchema describes the schema of a table or view.type TableSchema struct {Name string@@ -593,7 +522,7 @@ func getViewColumns(txn *sql.Tx, dbName, tblName string) ([]string, error) {}// exportTableData gets the data of a table.-func exportTableData(txn *sql.Tx, dbName, tblName string, includeDbPrefix bool, out io.Writer) error {+func exportTableData(txn *sql.Tx, dbName, tblName string, out io.Writer) error {query := fmt.Sprintf("SELECT * FROM `%s`.`%s`;", dbName, tblName)rows, err := txn.Query(query)if err != nil {@@ -628,11 +557,7 @@ func exportTableData(txn *sql.Tx, dbName, tblName string, includeDbPrefix bool,tokens[i] = fmt.Sprintf("'%s'", v.String)}}- dbPrefix := ""- if includeDbPrefix {- dbPrefix = fmt.Sprintf("`%s`.", dbName)- }- stmt := fmt.Sprintf("INSERT INTO %s`%s` VALUES (%s);\n", dbPrefix, tblName, strings.Join(tokens, ", "))+ stmt := fmt.Sprintf("INSERT INTO `%s` VALUES (%s);\n", tblName, strings.Join(tokens, ", "))if _, err := io.WriteString(out, stmt); err != nil {return err}diff --git a/backend/plugin/db/mysql/mysql.go b/backend/plugin/db/mysql/mysql.goindex b66610fb83..e40ec45116 100644--- a/backend/plugin/db/mysql/mysql.go+++ b/backend/plugin/db/mysql/mysql.go@@ -141,29 +141,6 @@ func (driver *Driver) GetDB() *sql.DB {return driver.db}-// getDatabases gets all databases of an instance.-func getDatabases(ctx context.Context, txn *sql.Tx) ([]string, error) {- var dbNames []string- query := "SHOW DATABASES"- rows, err := txn.QueryContext(ctx, query)- if err != nil {- return nil, err- }- defer rows.Close()-- for rows.Next() {- var name string- if err := rows.Scan(&name); err != nil {- return nil, err- }- dbNames = append(dbNames, name)- }- if err := rows.Err(); err != nil {- return nil, util.FormatErrorWithQuery(err, query)- }- return dbNames, nil-}-// getVersion gets the version.func (driver *Driver) getVersion(ctx context.Context) (string, error) {query := "SELECT VERSION()"