Skip to content

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

chore: refactor pg execute (#10132)

Danny Xud178ba89d720
diff --git a/backend/plugin/db/pg/pg.go b/backend/plugin/db/pg/pg.go
index b6c34e53c4..5d6dd54896 100644
--- a/backend/plugin/db/pg/pg.go
+++ b/backend/plugin/db/pg/pg.go
@@ -296,23 +296,26 @@ func (driver *Driver) Execute(ctx context.Context, statement string, opts db.Exe
var nonTransactionStmts []string
totalRowsAffected := int64(0)
for _, singleSQL := range singleSQLs {
- stmt := singleSQL.Text
- if isSuperuserStatement(stmt) {
+ if isIgnoredStatement(singleSQL.Text) {
+ continue
+ }
+ if isNonTransactionStatement(singleSQL.Text) {
+ nonTransactionStmts = append(nonTransactionStmts, singleSQL.Text)
+ continue
+ }
+
+ if isSuperuserStatement(singleSQL.Text) {
// CREATE EVENT TRIGGER statement only supports EXECUTE PROCEDURE in version 10 and before, while newer version supports both EXECUTE { FUNCTION | PROCEDURE }.
// Since we use pg_dump version 14, the dump uses a new style even for an old version of PostgreSQL.
// We should convert EXECUTE FUNCTION to EXECUTE PROCEDURE to make the restoration work on old versions.
// https://www.postgresql.org/docs/14/sql-createeventtrigger.html
- if strings.Contains(strings.ToUpper(stmt), "CREATE EVENT TRIGGER") {
- stmt = strings.ReplaceAll(stmt, "EXECUTE FUNCTION", "EXECUTE PROCEDURE")
+ if strings.Contains(strings.ToUpper(singleSQL.Text), "CREATE EVENT TRIGGER") {
+ singleSQL.Text = strings.ReplaceAll(singleSQL.Text, "EXECUTE FUNCTION", "EXECUTE PROCEDURE")
}
// Use superuser privilege to run privileged statements.
- stmt = fmt.Sprintf("SET LOCAL ROLE NONE;%sSET LOCAL ROLE '%s';", stmt, owner)
- remainingStmts = append(remainingStmts, stmt)
- } else if isNonTransactionStatement(stmt) {
- nonTransactionStmts = append(nonTransactionStmts, stmt)
- } else if !isIgnoredStatement(stmt) {
- remainingStmts = append(remainingStmts, stmt)
+ singleSQL.Text = fmt.Sprintf("SET LOCAL ROLE NONE;%sSET LOCAL ROLE '%s';", singleSQL.Text, owner)
}
+ remainingStmts = append(remainingStmts, singleSQL.Text)
}
if len(remainingStmts) != 0 {
diff --git a/backend/plugin/db/redshift/redshift.go b/backend/plugin/db/redshift/redshift.go
index 00c9b60daf..3a56d66440 100644
--- a/backend/plugin/db/redshift/redshift.go
+++ b/backend/plugin/db/redshift/redshift.go
@@ -190,23 +190,23 @@ func (driver *Driver) Execute(ctx context.Context, statement string, opts db.Exe
var nonTransactionStmts []string
totalRowsAffected := int64(0)
for _, singleSQL := range singleSQLs {
- stmt := singleSQL.Text
- if isSuperuserStatement(stmt) {
+ if isNonTransactionStatement(singleSQL.Text) {
+ nonTransactionStmts = append(nonTransactionStmts, singleSQL.Text)
+ continue
+ }
+
+ if isSuperuserStatement(singleSQL.Text) {
// CREATE EVENT TRIGGER statement only supports EXECUTE PROCEDURE in version 10 and before, while newer version supports both EXECUTE { FUNCTION | PROCEDURE }.
// Since we use pg_dump version 14, the dump uses a new style even for an old version of PostgreSQL.
// We should convert EXECUTE FUNCTION to EXECUTE PROCEDURE to make the restoration work on old versions.
// https://www.postgresql.org/docs/14/sql-createeventtrigger.html
- if strings.Contains(strings.ToUpper(stmt), "CREATE EVENT TRIGGER") {
- stmt = strings.ReplaceAll(stmt, "EXECUTE FUNCTION", "EXECUTE PROCEDURE")
+ if strings.Contains(strings.ToUpper(singleSQL.Text), "CREATE EVENT TRIGGER") {
+ singleSQL.Text = strings.ReplaceAll(singleSQL.Text, "EXECUTE FUNCTION", "EXECUTE PROCEDURE")
}
// Use superuser privilege to run privileged statements.
- stmt = fmt.Sprintf("SET SESSION AUTHORIZATION NONE;%sSET SESSION AUTHORIZATION '%s';", stmt, owner)
- remainingStmts = append(remainingStmts, stmt)
- } else if isNonTransactionStatement(stmt) {
- nonTransactionStmts = append(nonTransactionStmts, stmt)
- } else if !isIgnoredStatement(stmt) {
- remainingStmts = append(remainingStmts, stmt)
+ singleSQL.Text = fmt.Sprintf("SET SESSION AUTHORIZATION NONE;%sSET SESSION AUTHORIZATION '%s';", singleSQL.Text, owner)
}
+ remainingStmts = append(remainingStmts, singleSQL.Text)
}
if len(remainingStmts) != 0 {
@@ -283,13 +283,6 @@ func isSuperuserStatement(stmt string) bool {
return false
}
-func isIgnoredStatement(stmt string) bool {
- // Extensions created in AWS Aurora PostgreSQL are owned by rdsadmin.
- // We don't have privileges to comment on the extension and have to ignore it.
- upperCaseStmt := strings.ToUpper(stmt)
- return strings.HasPrefix(upperCaseStmt, "COMMENT ON EXTENSION")
-}
-
func isNonTransactionStatement(stmt string) bool {
// CREATE INDEX CONCURRENTLY cannot run inside a transaction block.
// CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ [ IF NOT EXISTS ] name ] ON [ ONLY ] table_name [ USING method ] ...
diff --git a/backend/plugin/db/risingwave/risingwave.go b/backend/plugin/db/risingwave/risingwave.go
index 3a2231f28f..20e4f9bdab 100644
--- a/backend/plugin/db/risingwave/risingwave.go
+++ b/backend/plugin/db/risingwave/risingwave.go
@@ -257,14 +257,12 @@ func (driver *Driver) Execute(ctx context.Context, statement string, opts db.Exe
var nonTransactionStmts []string
totalRowsAffected := int64(0)
for _, singleSQL := range singleSQLs {
- stmt := singleSQL.Text
- if isSuperuserStatement(stmt) {
- remainingStmts = append(remainingStmts, stmt)
- } else if isNonTransactionStatement(stmt) {
- nonTransactionStmts = append(nonTransactionStmts, stmt)
- } else if !isIgnoredStatement(stmt) {
- remainingStmts = append(remainingStmts, stmt)
+ if isNonTransactionStatement(singleSQL.Text) {
+ nonTransactionStmts = append(nonTransactionStmts, singleSQL.Text)
+ continue
}
+
+ remainingStmts = append(remainingStmts, singleSQL.Text)
}
if len(remainingStmts) != 0 {
@@ -323,21 +321,6 @@ func (driver *Driver) createDatabaseExecute(ctx context.Context, statement strin
return nil
}
-func isSuperuserStatement(stmt string) bool {
- upperCaseStmt := strings.ToUpper(stmt)
- if strings.HasPrefix(upperCaseStmt, "GRANT") || strings.HasPrefix(upperCaseStmt, "CREATE EXTENSION") || strings.HasPrefix(upperCaseStmt, "CREATE EVENT TRIGGER") || strings.HasPrefix(upperCaseStmt, "COMMENT ON EVENT TRIGGER") {
- return true
- }
- return false
-}
-
-func isIgnoredStatement(stmt string) bool {
- // Extensions created in AWS Aurora PostgreSQL are owned by rdsadmin.
- // We don't have privileges to comment on the extension and have to ignore it.
- upperCaseStmt := strings.ToUpper(stmt)
- return strings.HasPrefix(upperCaseStmt, "COMMENT ON EXTENSION")
-}
-
func isNonTransactionStatement(stmt string) bool {
// CREATE INDEX CONCURRENTLY cannot run inside a transaction block.
// CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ [ IF NOT EXISTS ] name ] ON [ ONLY ] table_name [ USING method ] ...