Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: skip auto-increment in dump schema when it's the end of a statement (#2725)
Danny Xu148dbb0e22dc
diff --git a/plugin/db/mysql/dump.go b/plugin/db/mysql/dump.goindex fbdbfcf945..8ba1bf5091 100644--- a/plugin/db/mysql/dump.go+++ b/plugin/db/mysql/dump.go@@ -81,7 +81,7 @@ const ()var (- excludeAutoIncrement = regexp.MustCompile(`AUTO_INCREMENT=\d+ `)+ excludeAutoIncrement = regexp.MustCompile(` AUTO_INCREMENT=\d+`))// Dump dumps the database.@@ -416,11 +416,6 @@ type triggerSchema struct {statement string}-// getTablesTx gets all tables of a database using the provided transaction.-func getTablesTx(txn *sql.Tx, dbName string) ([]*TableSchema, error) {- return getTablesImpl(txn, dbName)-}-// getTables gets all tables of a database.func getTables(ctx context.Context, conn *sql.Conn, dbName string) ([]*TableSchema, error) {txn, err := conn.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})@@ -428,10 +423,11 @@ func getTables(ctx context.Context, conn *sql.Conn, dbName string) ([]*TableSchereturn nil, err}defer txn.Rollback()- return getTablesImpl(txn, dbName)+ return getTablesTx(txn, dbName)}-func getTablesImpl(txn *sql.Tx, dbName string) ([]*TableSchema, error) {+// getTablesTx gets all tables of a database using the provided transaction.+func getTablesTx(txn *sql.Tx, dbName string) ([]*TableSchema, error) {var tables []*TableSchemaquery := fmt.Sprintf("SHOW FULL TABLES FROM `%s`;", dbName)rows, err := txn.Query(query)diff --git a/plugin/db/mysql/dump_test.go b/plugin/db/mysql/dump_test.goindex f1f9cc0149..63ae6619a9 100644--- a/plugin/db/mysql/dump_test.go+++ b/plugin/db/mysql/dump_test.go@@ -12,3 +12,36 @@ func TestGetTemporaryView(t *testing.T) {want := "--\n-- Temporary view structure for `db1`\n--\nCREATE VIEW `db1` AS SELECT\n 1 AS `col1`,\n 1 AS `col2`;\n\n"a.Equal(want, got)}++func TestExcludeSchemaAutoIncrementValue(t *testing.T) {+ tests := []struct {+ stmt string+ want string+ }{+ {+ `CREATE TABLE world (+ id int NOT NULL AUTO_INCREMENT,+ PRIMARY KEY (id)+ ) ENGINE=InnoDB AUTO_INCREMENT=12345 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;`,+ `CREATE TABLE world (+ id int NOT NULL AUTO_INCREMENT,+ PRIMARY KEY (id)+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;`,+ },+ {+ `CREATE TABLE world (+ id int NOT NULL AUTO_INCREMENT,+ PRIMARY KEY (id)+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci AUTO_INCREMENT=12345;`,+ `CREATE TABLE world (+ id int NOT NULL AUTO_INCREMENT,+ PRIMARY KEY (id)+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;`,+ },+ }++ for _, test := range tests {+ got := excludeSchemaAutoIncrementValue(test.stmt)+ require.Equal(t, test.want, got)+ }+}