Skip to content

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

feat(db): add generic transaction mode execution support (#16888)

h3n4l3e7ac0cf8562
diff --git a/backend/common/engine.go b/backend/common/engine.go
index 4e2f1e0d96..9dd3dfd132 100644
--- a/backend/common/engine.go
+++ b/backend/common/engine.go
@@ -506,3 +506,23 @@ func EngineDBSchemaReadyToMigrate(e storepb.Engine) bool {
return true
}
}
+
+// TransactionMode represents the transaction execution mode for a migration script.
+type TransactionMode string
+
+const (
+ // TransactionModeOn wraps the script in a single transaction.
+ TransactionModeOn TransactionMode = "on"
+ // TransactionModeOff executes the script's statements sequentially in auto-commit mode.
+ TransactionModeOff TransactionMode = "off"
+ // TransactionModeUnspecified means no explicit mode was specified.
+ TransactionModeUnspecified TransactionMode = ""
+)
+
+// GetDefaultTransactionMode returns the default transaction mode.
+// All engines default to "on" (transactional) for safety and backward compatibility.
+// Users can explicitly set "-- txn-mode = off" when needed for engines with limited transactional DDL support.
+func GetDefaultTransactionMode() TransactionMode {
+ // All engines default to "on" for safety and backward compatibility
+ return TransactionModeOn
+}
diff --git a/backend/plugin/db/redshift/redshift.go b/backend/plugin/db/redshift/redshift.go
index fe7df3e5c7..96a6f50c3e 100644
--- a/backend/plugin/db/redshift/redshift.go
+++ b/backend/plugin/db/redshift/redshift.go
@@ -170,11 +170,9 @@ func (d *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteO
transactionMode, cleanedStatement := base.ParseTransactionMode(statement)
statement = cleanedStatement
- // For Redshift MVP, only enable transaction mode parsing for Redshift instances
- // Default to "off" for DDL if not specified
- if transactionMode == base.TransactionModeUnspecified {
- // We'll handle defaults in the next step
- transactionMode = base.TransactionModeOn // For now, maintain backward compatibility
+ // Apply default when transaction mode is not specified
+ if transactionMode == common.TransactionModeUnspecified {
+ transactionMode = common.GetDefaultTransactionMode()
}
var commands []base.SingleSQL
@@ -198,7 +196,7 @@ func (d *Driver) Execute(ctx context.Context, statement string, opts db.ExecuteO
}
// Execute based on transaction mode
- if transactionMode == base.TransactionModeOff {
+ if transactionMode == common.TransactionModeOff {
return d.executeInAutoCommitMode(ctx, commands, opts)
}
return d.executeInTransactionMode(ctx, commands, opts)
diff --git a/backend/plugin/parser/base/transaction_mode.go b/backend/plugin/parser/base/transaction_mode.go
index fd72e8e7de..3beefcc419 100644
--- a/backend/plugin/parser/base/transaction_mode.go
+++ b/backend/plugin/parser/base/transaction_mode.go
@@ -3,18 +3,8 @@ package base
import (
"regexp"
"strings"
-)
-
-// TransactionMode represents the transaction execution mode for a migration script.
-type TransactionMode string
-const (
- // TransactionModeOn wraps the script in a single transaction.
- TransactionModeOn TransactionMode = "on"
- // TransactionModeOff executes the script's statements sequentially in auto-commit mode.
- TransactionModeOff TransactionMode = "off"
- // TransactionModeUnspecified means no explicit mode was specified.
- TransactionModeUnspecified TransactionMode = ""
+ "github.com/bytebase/bytebase/backend/common"
)
// txnModeDirectiveRegex matches the transaction mode directive at the beginning of a SQL script.
@@ -24,10 +14,10 @@ var txnModeDirectiveRegex = regexp.MustCompile(`(?i)^\s*--\s*txn-mode\s*=\s*(on|
// ParseTransactionMode extracts the transaction mode directive from the SQL script.
// It checks the first line of the script for the -- txn-mode = on|off directive.
// Returns the transaction mode and the SQL script without the directive.
-func ParseTransactionMode(script string) (TransactionMode, string) {
+func ParseTransactionMode(script string) (common.TransactionMode, string) {
lines := strings.Split(script, "\n")
if len(lines) == 0 {
- return TransactionModeUnspecified, script
+ return common.TransactionModeUnspecified, script
}
// Check the first line for the transaction mode directive
@@ -36,8 +26,8 @@ func ParseTransactionMode(script string) (TransactionMode, string) {
mode := strings.ToLower(matches[1])
// Remove the directive line from the script
remainingScript := strings.Join(lines[1:], "\n")
- return TransactionMode(mode), strings.TrimSpace(remainingScript)
+ return common.TransactionMode(mode), strings.TrimSpace(remainingScript)
}
- return TransactionModeUnspecified, script
+ return common.TransactionModeUnspecified, script
}
diff --git a/backend/plugin/parser/base/transaction_mode_test.go b/backend/plugin/parser/base/transaction_mode_test.go
deleted file mode 100644
index 90fdabf859..0000000000
--- a/backend/plugin/parser/base/transaction_mode_test.go
+++ /dev/null
@@ -1,87 +0,0 @@
-package base
-
-import (
- "testing"
-)
-
-func TestParseTransactionMode(t *testing.T) {
- tests := []struct {
- name string
- script string
- expectedMode TransactionMode
- expectedScript string
- }{
- {
- name: "transaction mode on",
- script: "-- txn-mode = on\nCREATE TABLE foo (id INT);",
- expectedMode: TransactionModeOn,
- expectedScript: "CREATE TABLE foo (id INT);",
- },
- {
- name: "transaction mode off",
- script: "-- txn-mode = off\nCREATE INDEX CONCURRENTLY idx ON foo(id);",
- expectedMode: TransactionModeOff,
- expectedScript: "CREATE INDEX CONCURRENTLY idx ON foo(id);",
- },
- {
- name: "transaction mode uppercase",
- script: "-- TXN-MODE = OFF\nVACUUM ANALYZE;",
- expectedMode: TransactionModeOff,
- expectedScript: "VACUUM ANALYZE;",
- },
- {
- name: "transaction mode with extra spaces",
- script: " -- txn-mode = on \nINSERT INTO foo VALUES (1);",
- expectedMode: TransactionModeOn,
- expectedScript: "INSERT INTO foo VALUES (1);",
- },
- {
- name: "no transaction mode directive",
- script: "CREATE TABLE bar (id INT);",
- expectedMode: TransactionModeUnspecified,
- expectedScript: "CREATE TABLE bar (id INT);",
- },
- {
- name: "transaction mode not on first line",
- script: "CREATE TABLE baz (id INT);\n-- txn-mode = on\nINSERT INTO baz VALUES (1);",
- expectedMode: TransactionModeUnspecified,
- expectedScript: "CREATE TABLE baz (id INT);\n-- txn-mode = on\nINSERT INTO baz VALUES (1);",
- },
- {
- name: "invalid transaction mode value",
- script: "-- txn-mode = maybe\nCREATE TABLE qux (id INT);",
- expectedMode: TransactionModeUnspecified,
- expectedScript: "-- txn-mode = maybe\nCREATE TABLE qux (id INT);",
- },
- {
- name: "empty script",
- script: "",
- expectedMode: TransactionModeUnspecified,
- expectedScript: "",
- },
- {
- name: "only directive",
- script: "-- txn-mode = on",
- expectedMode: TransactionModeOn,
- expectedScript: "",
- },
- {
- name: "multiline script with directive",
- script: "-- txn-mode = off\nCREATE TABLE t1 (id INT);\n\nCREATE TABLE t2 (id INT);\n-- This is a comment\nINSERT INTO t1 VALUES (1);",
- expectedMode: TransactionModeOff,
- expectedScript: "CREATE TABLE t1 (id INT);\n\nCREATE TABLE t2 (id INT);\n-- This is a comment\nINSERT INTO t1 VALUES (1);",
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- mode, script := ParseTransactionMode(tt.script)
- if mode != tt.expectedMode {
- t.Errorf("expected mode %q, got %q", tt.expectedMode, mode)
- }
- if script != tt.expectedScript {
- t.Errorf("expected script %q, got %q", tt.expectedScript, script)
- }
- })
- }
-}
diff --git a/frontend/src/components/IssueV1/components/StatementSection/EditorView/TransactionModeToggle.vue b/frontend/src/components/IssueV1/components/StatementSection/EditorView/TransactionModeToggle.vue
index dd59481154..67551d4ca0 100644
--- a/frontend/src/components/IssueV1/components/StatementSection/EditorView/TransactionModeToggle.vue
+++ b/frontend/src/components/IssueV1/components/StatementSection/EditorView/TransactionModeToggle.vue
@@ -36,46 +36,24 @@
import { NSwitch, NTooltip } from "naive-ui";
import { computed, ref, watch } from "vue";
import { useIssueContext } from "@/components/IssueV1/logic";
-import { useCurrentProjectV1 } from "@/store";
-import { Engine } from "@/types/proto-es/v1/common_pb";
-import { Task_Type } from "@/types/proto-es/v1/rollout_service_pb";
-import { databaseForTask } from "@/utils";
+import { getDefaultTransactionMode } from "@/utils";
import { useEditorContext } from "./context";
import { parseStatement, updateTransactionMode } from "./directiveUtils";
const editorContext = useEditorContext();
const { selectedTask } = useIssueContext();
-const { project } = useCurrentProjectV1();
-const database = computed(() => {
- return databaseForTask(project.value, selectedTask.value);
-});
-
-const engine = computed(() => database.value.instanceResource.engine);
-
-// Some engines or task types might not support disabling transactions
+// Transaction mode can be configured for all supported engines
const shouldDisableTransactionMode = computed(() => {
- // For DML, always use transactions
- if (selectedTask.value.type === Task_Type.DATABASE_DATA_UPDATE) {
- return true;
- }
return false;
});
-// Default transaction mode based on engine and task type
-const getDefaultTransactionMode = () => {
- // For Redshift DDL, default to off
- if (
- engine.value === Engine.REDSHIFT &&
- selectedTask.value.type === Task_Type.DATABASE_SCHEMA_UPDATE
- ) {
- return false;
- }
- // For all other cases, default to on
- return true;
+// Default transaction mode
+const getDefaultForCurrentTask = () => {
+ return getDefaultTransactionMode();
};
-const isTransactionOn = ref(getDefaultTransactionMode());
+const isTransactionOn = ref(getDefaultForCurrentTask());
// Watch for task changes to reset defaults
watch(
@@ -87,7 +65,7 @@ watch(
isTransactionOn.value = parsed.transactionMode === "on";
} else {
// Use default if no directive found
- isTransactionOn.value = getDefaultTransactionMode();
+ isTransactionOn.value = getDefaultForCurrentTask();
}
},
{ immediate: true }
diff --git a/frontend/src/utils/v1/instance.ts b/frontend/src/utils/v1/instance.ts
index 3dfc9ce81b..ee7501bd2c 100644
--- a/frontend/src/utils/v1/instance.ts
+++ b/frontend/src/utils/v1/instance.ts
@@ -476,7 +476,24 @@ export const supportGetStringSchema = (engine: Engine) => {
// instanceV1SupportsTransactionMode returns true if the engine supports configurable transaction mode.
export const instanceV1SupportsTransactionMode = (engine: Engine): boolean => {
- // For MVP, only enable for Redshift
- // TODO: Enable for other engines after testing
- return [Engine.REDSHIFT].includes(engine);
+ // Exclude NoSQL databases that don't have traditional SQL transactions
+ const noSQLEngines = [
+ Engine.MONGODB,
+ Engine.REDIS,
+ Engine.DYNAMODB,
+ Engine.COSMOSDB,
+ Engine.CASSANDRA,
+ Engine.ELASTICSEARCH,
+ ];
+
+ return !noSQLEngines.includes(engine);
+};
+
+// getDefaultTransactionMode returns the default transaction mode.
+// This mirrors the backend logic in backend/common/engine.go
+// All engines default to "on" (transactional) for safety and backward compatibility.
+// Users can explicitly set "-- txn-mode = off" when needed for engines with limited transactional DDL support.
+export const getDefaultTransactionMode = (): boolean => {
+ // All engines default to "on" for safety and backward compatibility
+ return true;
};