Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: implement non-transactional version of GetTables (#1444)

Yilong Li9fc21ac9d91a
diff --git a/plugin/db/mysql/mysql.go b/plugin/db/mysql/mysql.go
index e89d145e9e..407d3a6aec 100644
--- a/plugin/db/mysql/mysql.go
+++ b/plugin/db/mysql/mysql.go
@@ -1000,7 +1000,7 @@ func flushTablesWithReadLock(ctx context.Context, conn *sql.Conn, database strin
}
defer txn.Rollback()
- tables, err := GetTables(txn, database)
+ tables, err := GetTablesTx(txn, database)
if err != nil {
return err
}
@@ -1074,7 +1074,7 @@ func dumpTxn(ctx context.Context, txn *sql.Tx, database string, out io.Writer, s
}
// Table and view statement.
- tables, err := GetTables(txn, dbName)
+ tables, err := GetTablesTx(txn, dbName)
if err != nil {
return fmt.Errorf("failed to get tables of database %q, error[%w]", dbName, err)
}
@@ -1218,9 +1218,22 @@ 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.
-// TODO(dragonly): Refactor to 2 variants that one takes a txn arg and one does not, for different use cases.
-func GetTables(txn *sql.Tx, dbName string) ([]*TableSchema, error) {
+func GetTables(ctx context.Context, db *sql.DB, dbName string) ([]*TableSchema, error) {
+ txn, err := db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
+ if err != nil {
+ return nil, err
+ }
+ defer txn.Rollback()
+ return getTablesImpl(txn, dbName)
+}
+
+func getTablesImpl(txn *sql.Tx, dbName string) ([]*TableSchema, error) {
var tables []*TableSchema
query := fmt.Sprintf("SHOW FULL TABLES FROM `%s`;", dbName)
rows, err := txn.Query(query)
diff --git a/plugin/restore/mysql/mysql.go b/plugin/restore/mysql/mysql.go
index d7fdfaeafe..6f352a4581 100644
--- a/plugin/restore/mysql/mysql.go
+++ b/plugin/restore/mysql/mysql.go
@@ -225,23 +225,16 @@ func (r *Restore) SwapPITRDatabase(ctx context.Context, database string, suffixT
}
}
- // TODO(dragonly): Remove the transactions, because they do not have a clear semantic / are not necessary.
- txn, err := db.BeginTx(ctx, nil)
- if err != nil {
- return pitrDatabaseName, pitrOldDatabase, err
- }
- defer txn.Rollback()
-
- tables, err := mysql.GetTables(txn, database)
+ tables, err := mysql.GetTables(ctx, db, database)
if err != nil {
return pitrDatabaseName, pitrOldDatabase, fmt.Errorf("failed to get tables of database %q, error[%w]", database, err)
}
- tablesPITR, err := mysql.GetTables(txn, pitrDatabaseName)
+ tablesPITR, err := mysql.GetTables(ctx, db, pitrDatabaseName)
if err != nil {
return pitrDatabaseName, pitrOldDatabase, fmt.Errorf("failed to get tables of database %q, error[%w]", pitrDatabaseName, err)
}
- if _, err := txn.ExecContext(ctx, fmt.Sprintf("CREATE DATABASE `%s`", pitrOldDatabase)); err != nil {
+ if _, err := db.ExecContext(ctx, fmt.Sprintf("CREATE DATABASE `%s`", pitrOldDatabase)); err != nil {
return pitrDatabaseName, pitrOldDatabase, err
}
@@ -254,11 +247,7 @@ func (r *Restore) SwapPITRDatabase(ctx context.Context, database string, suffixT
}
renameStmt := fmt.Sprintf("RENAME TABLE %s;", strings.Join(tableRenames, ", "))
- if _, err := txn.ExecContext(ctx, renameStmt); err != nil {
- return pitrDatabaseName, pitrOldDatabase, err
- }
-
- if err := txn.Commit(); err != nil {
+ if _, err := db.ExecContext(ctx, renameStmt); err != nil {
return pitrDatabaseName, pitrOldDatabase, err
}