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: consolidate execute options (#10127)
Danny Xub847799f81ee
diff --git a/backend/bin/bb/cmd/migrate.go b/backend/bin/bb/cmd/migrate.goindex cd6f028b06..77605cb2a1 100644--- a/backend/bin/bb/cmd/migrate.go+++ b/backend/bin/bb/cmd/migrate.go@@ -72,7 +72,7 @@ func migrateDatabase(ctx context.Context, u *dburl.URL, sqlReader io.Reader) errif _, err := io.Copy(&buf, sqlReader); err != nil {return errors.Wrap(err, "failed to read sql file")}- if _, err := driver.Execute(ctx, buf.String(), false /* createDatabase */, db.ExecuteOptions{}); err != nil {+ if _, err := driver.Execute(ctx, buf.String(), db.ExecuteOptions{}); err != nil {return errors.Wrap(err, "failed to migrate database")}return nildiff --git a/backend/migrator/migrator_test.go b/backend/migrator/migrator_test.goindex 7332ff2bd2..e6234bd47a 100644--- a/backend/migrator/migrator_test.go+++ b/backend/migrator/migrator_test.go@@ -168,7 +168,7 @@ func TestMigrationCompatibility(t *testing.T) {defer defaultDriver.Close(ctx)// Create a database with release latest schema.databaseName := "hidb"- _, err = defaultDriver.Execute(ctx, fmt.Sprintf("CREATE DATABASE %s", databaseName), true, dbdriver.ExecuteOptions{})+ _, err = defaultDriver.Execute(ctx, fmt.Sprintf("CREATE DATABASE %s", databaseName), dbdriver.ExecuteOptions{CreateDatabase: true})require.NoError(t, err)metadataConnConfig := connCfgdiff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.goindex 3d6046caf9..d87823840b 100644--- a/backend/plugin/advisor/utils_for_tests.go+++ b/backend/plugin/advisor/utils_for_tests.go@@ -241,7 +241,7 @@ func (*MockDriver) GetDB() *sql.DB {}// Execute implements the Driver interface.-func (*MockDriver) Execute(_ context.Context, _ string, _ bool, _ database.ExecuteOptions) (int64, error) {+func (*MockDriver) Execute(_ context.Context, _ string, _ database.ExecuteOptions) (int64, error) {return 0, nil}diff --git a/backend/plugin/db/clickhouse/clickhouse.go b/backend/plugin/db/clickhouse/clickhouse.goindex 3c35253e29..e123ea9dbd 100644--- a/backend/plugin/db/clickhouse/clickhouse.go+++ b/backend/plugin/db/clickhouse/clickhouse.go@@ -129,7 +129,7 @@ func (driver *Driver) getVersion(ctx context.Context) (string, error) {}// Execute executes a SQL statement.-func (driver *Driver) Execute(ctx context.Context, statement string, _ bool, _ db.ExecuteOptions) (int64, error) {+func (driver *Driver) Execute(ctx context.Context, statement string, _ db.ExecuteOptions) (int64, error) {tx, err := driver.db.BeginTx(ctx, nil)if err != nil {return 0, errdiff --git a/backend/plugin/db/dm/dm.go b/backend/plugin/db/dm/dm.goindex 63be5fbae0..735817cb51 100644--- a/backend/plugin/db/dm/dm.go+++ b/backend/plugin/db/dm/dm.go@@ -85,7 +85,7 @@ func (driver *Driver) GetDB() *sql.DB {//// Callers can use `beforeCommitTx` to do some extra work before transaction commit, like get the transaction id.// Any error returned by `beforeCommitTx` will rollback the transaction, so it is the callers' responsibility to return nil if the error occurs in `beforeCommitTx` is not fatal.-func (driver *Driver) Execute(ctx context.Context, statement string, _ bool, opts db.ExecuteOptions) (int64, error) {+func (driver *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) {conn, err := driver.db.Conn(ctx)if err != nil {return 0, errors.Wrapf(err, "failed to get connection")diff --git a/backend/plugin/db/driver.go b/backend/plugin/db/driver.goindex 49271cc7d3..298e328d9a 100644--- a/backend/plugin/db/driver.go+++ b/backend/plugin/db/driver.go@@ -410,7 +410,7 @@ type Driver interface {GetType() storepb.EngineGetDB() *sql.DB// Execute will execute the statement.- Execute(ctx context.Context, statement string, createDatabase bool, opts ExecuteOptions) (int64, error)+ Execute(ctx context.Context, statement string, opts ExecuteOptions) (int64, error)// Used for execute readonly SELECT statementQueryConn(ctx context.Context, conn *sql.Conn, statement string, queryContext *QueryContext) ([]*v1pb.QueryResult, error)// RunStatement will execute the statement and return the result, for both SELECT and non-SELECT statements.@@ -484,6 +484,7 @@ func Open(ctx context.Context, dbType storepb.Engine, driverConfig DriverConfig,// ExecuteOptions is the options for execute.type ExecuteOptions struct {+ CreateDatabase boolBeginFunc func(ctx context.Context, conn *sql.Conn) errorEndTransactionFunc func(tx *sql.Tx) error// ChunkedSubmission is the flag to indicate if we should use chunked submission for the statement.diff --git a/backend/plugin/db/mongodb/mongodb.go b/backend/plugin/db/mongodb/mongodb.goindex 1c165b53a8..a0b1923fd1 100644--- a/backend/plugin/db/mongodb/mongodb.go+++ b/backend/plugin/db/mongodb/mongodb.go@@ -90,7 +90,7 @@ func (*Driver) GetDB() *sql.DB {}// Execute executes a statement, always returns 0 as the number of rows affected because we execute the statement by mongosh, it's hard to catch the row effected number.-func (driver *Driver) Execute(ctx context.Context, statement string, _ bool, _ db.ExecuteOptions) (int64, error) {+func (driver *Driver) Execute(ctx context.Context, statement string, _ db.ExecuteOptions) (int64, error) {connectionURI := getMongoDBConnectionURI(driver.connCfg)// For MongoDB, we execute the statement in mongosh, which is a shell for MongoDB.// There are some ways to execute the statement in mongosh:diff --git a/backend/plugin/db/mssql/mssql.go b/backend/plugin/db/mssql/mssql.goindex d32f851a1f..3a91b75417 100644--- a/backend/plugin/db/mssql/mssql.go+++ b/backend/plugin/db/mssql/mssql.go@@ -92,8 +92,8 @@ func (driver *Driver) GetDB() *sql.DB {}// Execute executes a SQL statement and returns the affected rows.-func (driver *Driver) Execute(ctx context.Context, statement string, createDatabase bool, _ db.ExecuteOptions) (int64, error) {- if createDatabase {+func (driver *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) {+ if opts.CreateDatabase {if _, err := driver.db.ExecContext(ctx, statement); err != nil {return 0, err}diff --git a/backend/plugin/db/mysql/mysql.go b/backend/plugin/db/mysql/mysql.goindex fb21c33b47..d5ef371250 100644--- a/backend/plugin/db/mysql/mysql.go+++ b/backend/plugin/db/mysql/mysql.go@@ -163,7 +163,7 @@ func parseVersion(version string) (string, string, error) {}// Execute executes a SQL statement.-func (driver *Driver) Execute(ctx context.Context, statement string, _ bool, opts db.ExecuteOptions) (int64, error) {+func (driver *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) {statement, err := mysqlparser.DealWithDelimiter(statement)if err != nil {return 0, errors.Wrapf(err, "failed to deal with delimiter")diff --git a/backend/plugin/db/obo/obo.go b/backend/plugin/db/obo/obo.goindex e54d6fbe3b..3fbfa605da 100644--- a/backend/plugin/db/obo/obo.go+++ b/backend/plugin/db/obo/obo.go@@ -79,8 +79,8 @@ func (driver *Driver) GetDB() *sql.DB {return driver.db}-func (driver *Driver) Execute(ctx context.Context, statement string, createDatabase bool, opts db.ExecuteOptions) (int64, error) {- if createDatabase {+func (driver *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) {+ if opts.CreateDatabase {return 0, errors.New("create database is not supported for OceanBase Oracle mode")}diff --git a/backend/plugin/db/oracle/oracle.go b/backend/plugin/db/oracle/oracle.goindex 20fe1d38ce..7fb1c7af6c 100644--- a/backend/plugin/db/oracle/oracle.go+++ b/backend/plugin/db/oracle/oracle.go@@ -100,7 +100,11 @@ func (driver *Driver) GetDB() *sql.DB {//// Callers can use `beforeCommitTx` to do some extra work before transaction commit, like get the transaction id.// Any error returned by `beforeCommitTx` will rollback the transaction, so it is the callers' responsibility to return nil if the error occurs in `beforeCommitTx` is not fatal.-func (driver *Driver) Execute(ctx context.Context, statement string, _ bool, opts db.ExecuteOptions) (int64, error) {+func (driver *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) {+ if opts.CreateDatabase {+ return 0, errors.New("create database is not supported for Oracle")+ }+conn, err := driver.db.Conn(ctx)if err != nil {return 0, errors.Wrapf(err, "failed to get connection")diff --git a/backend/plugin/db/pg/pg.go b/backend/plugin/db/pg/pg.goindex a7a27ff9ca..97f2894273 100644--- a/backend/plugin/db/pg/pg.go+++ b/backend/plugin/db/pg/pg.go@@ -271,8 +271,8 @@ func (driver *Driver) getPGStatStatementsVersion(ctx context.Context) (string, e// Execute will execute the statement. For CREATE DATABASE statement, some types of databases such as Postgres// will not use transactions to execute the statement but will still use transactions to execute the rest of statements.-func (driver *Driver) Execute(ctx context.Context, statement string, createDatabase bool, _ db.ExecuteOptions) (int64, error) {- if createDatabase {+func (driver *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) {+ if opts.CreateDatabase {if err := driver.createDatabaseExecute(ctx, statement); err != nil {return 0, err}diff --git a/backend/plugin/db/redis/redis.go b/backend/plugin/db/redis/redis.goindex f4543c5e99..21dc99c027 100644--- a/backend/plugin/db/redis/redis.go+++ b/backend/plugin/db/redis/redis.go@@ -143,8 +143,8 @@ func (*Driver) GetDB() *sql.DB {// Execute will execute the statement. For CREATE DATABASE statement, some types of databases such as Postgres// will not use transactions to execute the statement but will still use transactions to execute the rest of statements.-func (d *Driver) Execute(ctx context.Context, statement string, createDatabase bool, _ db.ExecuteOptions) (int64, error) {- if createDatabase {+func (d *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) {+ if opts.CreateDatabase {return 0, errors.New("redis: cannot create database")}diff --git a/backend/plugin/db/redshift/redshift.go b/backend/plugin/db/redshift/redshift.goindex 0a012391af..5a41eb3fa0 100644--- a/backend/plugin/db/redshift/redshift.go+++ b/backend/plugin/db/redshift/redshift.go@@ -162,11 +162,11 @@ func (driver *Driver) GetDB() *sql.DB {// Execute will execute the statement. For CREATE DATABASE statement, some types of databases such as Postgres// will not use transactions to execute the statement but will still use transactions to execute the rest of statements.-func (driver *Driver) Execute(ctx context.Context, statement string, createDatabase bool, _ db.ExecuteOptions) (int64, error) {+func (driver *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) {if driver.datashare {return 0, errors.Errorf("datashare database cannot be updated")}- if createDatabase {+ if opts.CreateDatabase {if err := driver.createDatabaseExecute(ctx, statement); err != nil {return 0, err}diff --git a/backend/plugin/db/risingwave/risingwave.go b/backend/plugin/db/risingwave/risingwave.goindex 49c260dba5..a6b3adf6c9 100644--- a/backend/plugin/db/risingwave/risingwave.go+++ b/backend/plugin/db/risingwave/risingwave.go@@ -236,8 +236,8 @@ func (driver *Driver) getVersion(ctx context.Context) (string, error) {// Execute will execute the statement. For CREATE DATABASE statement, some types of databases such as Postgres// will not use transactions to execute the statement but will still use transactions to execute the rest of statements.-func (driver *Driver) Execute(ctx context.Context, statement string, createDatabase bool, _ db.ExecuteOptions) (int64, error) {- if createDatabase {+func (driver *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) {+ if opts.CreateDatabase {if err := driver.createDatabaseExecute(ctx, statement); err != nil {return 0, err}diff --git a/backend/plugin/db/snowflake/snowflake.go b/backend/plugin/db/snowflake/snowflake.goindex e34f766676..36e4044ae3 100644--- a/backend/plugin/db/snowflake/snowflake.go+++ b/backend/plugin/db/snowflake/snowflake.go@@ -218,7 +218,7 @@ func getDatabasesTxn(ctx context.Context, tx *sql.Tx) ([]string, error) {}// Execute executes a SQL statement and returns the affected rows.-func (driver *Driver) Execute(ctx context.Context, statement string, _ bool, _ db.ExecuteOptions) (int64, error) {+func (driver *Driver) Execute(ctx context.Context, statement string, _ db.ExecuteOptions) (int64, error) {count := 0f := func(stmt string) error {count++diff --git a/backend/plugin/db/spanner/spanner.go b/backend/plugin/db/spanner/spanner.goindex c754896e5e..261571d4fe 100644--- a/backend/plugin/db/spanner/spanner.go+++ b/backend/plugin/db/spanner/spanner.go@@ -115,8 +115,8 @@ func (*Driver) GetDB() *sql.DB {}// Execute executes a SQL statement.-func (d *Driver) Execute(ctx context.Context, statement string, createDatabase bool, _ db.ExecuteOptions) (int64, error) {- if createDatabase {+func (d *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) {+ if opts.CreateDatabase {stmts, err := sanitizeSQL(statement)if err != nil {return 0, errors.Wrapf(err, "failed to sanitize %v", statement)diff --git a/backend/plugin/db/sqlite/sqlite.go b/backend/plugin/db/sqlite/sqlite.goindex d91a0e5145..18dc846a99 100644--- a/backend/plugin/db/sqlite/sqlite.go+++ b/backend/plugin/db/sqlite/sqlite.go@@ -114,8 +114,8 @@ func (driver *Driver) getDatabases() ([]string, error) {}// Execute executes a SQL statement.-func (driver *Driver) Execute(ctx context.Context, statement string, createDatabase bool, _ db.ExecuteOptions) (int64, error) {- if createDatabase {+func (driver *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) {+ if opts.CreateDatabase {parts := strings.Split(statement, `'`)if len(parts) != 3 {return 0, errors.Errorf("invalid statement %q", statement)diff --git a/backend/plugin/db/starrocks/starrocks.go b/backend/plugin/db/starrocks/starrocks.goindex d1fd4ba212..a8c0fe75e4 100644--- a/backend/plugin/db/starrocks/starrocks.go+++ b/backend/plugin/db/starrocks/starrocks.go@@ -158,7 +158,7 @@ func parseVersion(version string) (string, string, error) {}// Execute executes a SQL statement.-func (driver *Driver) Execute(ctx context.Context, statement string, _ bool, opts db.ExecuteOptions) (int64, error) {+func (driver *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) {statement, err := mysqlparser.DealWithDelimiter(statement)if err != nil {return 0, errors.Wrapf(err, "failed to deal with delimiter")diff --git a/backend/plugin/db/tidb/tidb.go b/backend/plugin/db/tidb/tidb.goindex 231515d132..744da635b8 100644--- a/backend/plugin/db/tidb/tidb.go+++ b/backend/plugin/db/tidb/tidb.go@@ -160,7 +160,7 @@ func parseVersion(version string) (string, string, error) {}// Execute executes a SQL statement.-func (driver *Driver) Execute(ctx context.Context, statement string, _ bool, opts db.ExecuteOptions) (int64, error) {+func (driver *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteOptions) (int64, error) {statement, err := mysqlparser.DealWithDelimiter(statement)if err != nil {return 0, errors.Wrapf(err, "failed to deal with delimiter")diff --git a/backend/resources/postgres/sample_instance.go b/backend/resources/postgres/sample_instance.goindex e85036ab82..c44343f0a4 100644--- a/backend/resources/postgres/sample_instance.go+++ b/backend/resources/postgres/sample_instance.go@@ -253,7 +253,7 @@ func prepareSampleDatabase(ctx context.Context, pgUser, host, port, database strdefer driver.Close(ctx)// Create the sample database.- if _, err := driver.Execute(ctx, fmt.Sprintf("CREATE DATABASE %s", database), true, db.ExecuteOptions{}); err != nil {+ if _, err := driver.Execute(ctx, fmt.Sprintf("CREATE DATABASE %s", database), db.ExecuteOptions{CreateDatabase: true}); err != nil {return errors.Wrapf(err, "failed to create sample database")}slog.Info(fmt.Sprintf("Successfully created database %s", database))@@ -279,7 +279,7 @@ func createPGStatStatementsExtension(ctx context.Context, pgUser, host, port, da}defer driver.Close(ctx)- if _, err := driver.Execute(ctx, "CREATE EXTENSION IF NOT EXISTS pg_stat_statements;", false, db.ExecuteOptions{}); err != nil {+ if _, err := driver.Execute(ctx, "CREATE EXTENSION IF NOT EXISTS pg_stat_statements;", db.ExecuteOptions{}); err != nil {return errors.Wrapf(err, "failed to create pg_stat_statements extension")}slog.Info("Successfully created pg_stat_statements extension")@@ -305,7 +305,7 @@ func dropDefaultPostgresDatabase(ctx context.Context, pgUser, host, port, connecdefer driver.Close(ctx)// Drop the default postgres database.- if _, err := driver.Execute(ctx, "DROP DATABASE IF EXISTS postgres", true, db.ExecuteOptions{}); err != nil {+ if _, err := driver.Execute(ctx, "DROP DATABASE IF EXISTS postgres", db.ExecuteOptions{}); err != nil {return errors.Wrapf(err, "failed to drop default postgres database")}diff --git a/backend/runner/taskrun/data_update_executor.go b/backend/runner/taskrun/data_update_executor.goindex 919adab01d..638834e08e 100644--- a/backend/runner/taskrun/data_update_executor.go+++ b/backend/runner/taskrun/data_update_executor.go@@ -119,7 +119,7 @@ func (exec *DataUpdateExecutor) backupData(suffix := time.Now().Format("20060102150405")selectIntoStatement, targetTableName := updateToSelect(statement, backupDatabaseName, suffix, issue.UID)- if _, err := driver.Execute(driverCtx, selectIntoStatement, false /* createDatabase */, db.ExecuteOptions{}); err != nil {+ if _, err := driver.Execute(driverCtx, selectIntoStatement, db.ExecuteOptions{}); err != nil {return err}createActivityPayload := api.ActivityPipelineTaskPriorBackupPayload{diff --git a/backend/runner/taskrun/database_create_executor.go b/backend/runner/taskrun/database_create_executor.goindex 2749b11019..387a9af477 100644--- a/backend/runner/taskrun/database_create_executor.go+++ b/backend/runner/taskrun/database_create_executor.go@@ -156,7 +156,7 @@ func (exec *DatabaseCreateExecutor) RunOnce(ctx context.Context, driverCtx conteUpdateTime: time.Now(),})- if _, err := defaultDBDriver.Execute(driverCtx, statement, true /* createDatabase */, db.ExecuteOptions{}); err != nil {+ if _, err := defaultDBDriver.Execute(driverCtx, statement, db.ExecuteOptions{CreateDatabase: true}); err != nil {return true, nil, err}diff --git a/backend/store/pg_engine.go b/backend/store/pg_engine.goindex 8d339e0376..69f31a656e 100644--- a/backend/store/pg_engine.go+++ b/backend/store/pg_engine.go@@ -63,7 +63,7 @@ func (db *DB) Open(ctx context.Context, createDB bool) error {}defer defaultDriver.Close(ctx)// Underlying driver handles the case where database already exists.- if _, err := defaultDriver.Execute(ctx, fmt.Sprintf("CREATE DATABASE %s", db.ConnCfg.Database), true, dbdriver.ExecuteOptions{}); err != nil {+ if _, err := defaultDriver.Execute(ctx, fmt.Sprintf("CREATE DATABASE %s", db.ConnCfg.Database), dbdriver.ExecuteOptions{CreateDatabase: true}); err != nil {return err}}diff --git a/backend/tests/grouping_change_test.go b/backend/tests/grouping_change_test.goindex bfeb83d800..f06ee1a6b3 100644--- a/backend/tests/grouping_change_test.go+++ b/backend/tests/grouping_change_test.go@@ -412,11 +412,11 @@ func TestCreateTableGroup(t *testing.T) {a.NoError(err)for _, preCreateTable := range prepareInstance.matchDatabasesNameTableName[preCreateDatabase].matched {- _, err = dbDriver.Execute(ctx, fmt.Sprintf(`CREATE TABLE %s (id INT);`, preCreateTable), false, db.ExecuteOptions{})+ _, err = dbDriver.Execute(ctx, fmt.Sprintf(`CREATE TABLE %s (id INT);`, preCreateTable), db.ExecuteOptions{})a.NoError(err)}for _, preCreateTable := range prepareInstance.matchDatabasesNameTableName[preCreateDatabase].unmatched {- _, err = dbDriver.Execute(ctx, fmt.Sprintf(`CREATE TABLE %s (id INT);`, preCreateTable), false, db.ExecuteOptions{})+ _, err = dbDriver.Execute(ctx, fmt.Sprintf(`CREATE TABLE %s (id INT);`, preCreateTable), db.ExecuteOptions{})a.NoError(err)}err = dbDriver.Close(ctx)diff --git a/backend/utils/utils.go b/backend/utils/utils.goindex fb21211487..b6ad0d31b9 100644--- a/backend/utils/utils.go+++ b/backend/utils/utils.go@@ -221,7 +221,7 @@ func MergeTaskCreateLists(taskCreateLists [][]*store.TaskMessage, taskIndexDAGLi// ExecuteMigrationDefault executes migration.func ExecuteMigrationDefault(ctx context.Context, driverCtx context.Context, store *store.Store, stateCfg *state.State, taskRunUID int, driver db.Driver, mi *db.MigrationInfo, statement string, sheetID *int, opts db.ExecuteOptions) (migrationHistoryID string, updatedSchema string, resErr error) {execFunc := func(ctx context.Context, execStatement string) error {- if _, err := driver.Execute(ctx, execStatement, false /* createDatabase */, opts); err != nil {+ if _, err := driver.Execute(ctx, execStatement, opts); err != nil {return err}return nil