Skip to content

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

fix: transaction mode switch initialization and api calls (#17419)

boojack9b55432eb46a
diff --git a/frontend/src/components/Plan/components/Configuration/TransactionModeSection/TransactionModeSwitch.vue b/frontend/src/components/Plan/components/Configuration/TransactionModeSection/TransactionModeSwitch.vue
index 695f7d4faf..e8b30c34d2 100644
--- a/frontend/src/components/Plan/components/Configuration/TransactionModeSection/TransactionModeSwitch.vue
+++ b/frontend/src/components/Plan/components/Configuration/TransactionModeSection/TransactionModeSwitch.vue
@@ -8,8 +8,12 @@
<script setup lang="tsx">
import { NSwitch } from "naive-ui";
-import { computed, watch } from "vue";
-import { getDefaultTransactionMode } from "@/utils";
+import { computed, nextTick, ref, watch } from "vue";
+import {
+ usePlanContext,
+ updateSpecSheetWithStatement,
+} from "@/components/Plan/logic";
+import { getDefaultTransactionMode, setSheetStatement } from "@/utils";
import { useSelectedSpec } from "../../SpecDetailView/context";
import {
parseStatement,
@@ -21,7 +25,11 @@ import { useTransactionModeSettingContext } from "./context";
const { allowChange, transactionMode, events } =
useTransactionModeSettingContext();
const selectedSpec = useSelectedSpec();
-const { sheetStatement, updateSheetStatement } = useSpecSheet(selectedSpec);
+const { sheetStatement, sheet, sheetReady } = useSpecSheet(selectedSpec);
+const { plan, isCreating } = usePlanContext();
+
+// Flag to prevent circular updates
+const isUpdatingFromUI = ref(false);
// Default transaction mode
const getDefaultForCurrentTask = () => {
@@ -31,13 +39,24 @@ const getDefaultForCurrentTask = () => {
const isTransactionOn = computed({
get: () => transactionMode.value === "on",
set: (value: boolean) => {
+ isUpdatingFromUI.value = true;
transactionMode.value = value ? "on" : "off";
updateStatementWithTransactionMode();
+ // Reset the flag after Vue's next tick to allow statement updates to propagate
+ nextTick(() => {
+ isUpdatingFromUI.value = false;
+ });
},
});
// Initialize transaction mode from statement
const initializeFromStatement = () => {
+ if (!sheetReady.value || !sheetStatement.value) {
+ // If sheet is not ready or statement is empty, use default
+ transactionMode.value = getDefaultForCurrentTask() ? "on" : "off";
+ return;
+ }
+
const parsed = parseStatement(sheetStatement.value);
if (parsed.transactionMode !== undefined) {
transactionMode.value = parsed.transactionMode;
@@ -55,11 +74,35 @@ watch(
{ immediate: true }
);
+// Watch for sheet statement changes to update transaction mode
+// Only update when statement changes externally, not when we change it ourselves
+watch(
+ [sheetStatement, sheetReady],
+ () => {
+ if (!isUpdatingFromUI.value) {
+ initializeFromStatement();
+ }
+ },
+ { immediate: true }
+);
+
// Update statement when transaction mode changes
-const updateStatementWithTransactionMode = () => {
+const updateStatementWithTransactionMode = async () => {
const mode = transactionMode.value;
const updatedStatement = updateTransactionMode(sheetStatement.value, mode);
- updateSheetStatement(updatedStatement);
+
+ if (isCreating.value) {
+ // When creating a plan, update the local sheet directly.
+ if (!sheet.value) return;
+ setSheetStatement(sheet.value, updatedStatement);
+ } else {
+ // For created plans, create new sheet and update plan/spec
+ await updateSpecSheetWithStatement(
+ plan.value,
+ selectedSpec.value,
+ updatedStatement
+ );
+ }
events.emit("update");
};
</script>
diff --git a/frontend/src/components/Plan/components/StatementSection/EditorView/EditorView.vue b/frontend/src/components/Plan/components/StatementSection/EditorView/EditorView.vue
index 5a2eb73f40..f02e8205f7 100644
--- a/frontend/src/components/Plan/components/StatementSection/EditorView/EditorView.vue
+++ b/frontend/src/components/Plan/components/StatementSection/EditorView/EditorView.vue
@@ -106,7 +106,6 @@
:readonly="isEditorReadonly"
:dialect="dialect"
:advices="isEditorReadonly || isCreating ? markers : []"
- :auto-height="false"
:auto-complete-context="{
instance: database.instance,
database: database.name,
diff --git a/frontend/src/components/Plan/logic/utils.ts b/frontend/src/components/Plan/logic/utils.ts
index e5ea2be0d3..ef9ed2326b 100644
--- a/frontend/src/components/Plan/logic/utils.ts
+++ b/frontend/src/components/Plan/logic/utils.ts
@@ -1,9 +1,18 @@
+import { create } from "@bufbuild/protobuf";
+import { cloneDeep } from "lodash-es";
+import { planServiceClientConnect } from "@/grpcweb";
import { t } from "@/plugins/i18n";
+import { projectNamePrefix, useSheetV1Store } from "@/store";
import { useProjectV1Store } from "@/store";
import type { Plan, Plan_Spec } from "@/types/proto-es/v1/plan_service_pb";
-import { Plan_ChangeDatabaseConfig_Type } from "@/types/proto-es/v1/plan_service_pb";
+import {
+ Plan_ChangeDatabaseConfig_Type,
+ UpdatePlanRequestSchema,
+} from "@/types/proto-es/v1/plan_service_pb";
import type { Project } from "@/types/proto-es/v1/project_service_pb";
-import { extractProjectResourceName } from "@/utils";
+import { SheetSchema } from "@/types/proto-es/v1/sheet_service_pb";
+import { extractProjectResourceName, setSheetStatement } from "@/utils";
+import { createEmptyLocalSheet, databaseEngineForSpec } from ".";
export const projectOfPlan = (plan: Plan): Project => {
const project = `projects/${extractProjectResourceName(plan.name)}`;
@@ -39,3 +48,46 @@ export const getSpecTitle = (spec: Plan_Spec): string => {
}
return title;
};
+
+export const updateSpecSheetWithStatement = async (
+ plan: Plan,
+ spec: Plan_Spec,
+ statement: string
+): Promise<void> => {
+ const planPatch = cloneDeep(plan);
+ const specToPatch = planPatch.specs.find((s) => s.id === spec.id);
+
+ if (!specToPatch) {
+ throw new Error(
+ `Cannot find spec to patch for plan update ${JSON.stringify(spec)}`
+ );
+ }
+
+ if (specToPatch.config.case !== "changeDatabaseConfig") {
+ throw new Error(
+ `Unsupported spec type for plan update ${JSON.stringify(specToPatch)}`
+ );
+ }
+
+ const specEngine = await databaseEngineForSpec(specToPatch);
+ const newSheet = create(SheetSchema, {
+ ...createEmptyLocalSheet(),
+ title: plan.title,
+ engine: specEngine,
+ });
+ setSheetStatement(newSheet, statement);
+
+ const projectName = `${projectNamePrefix}${extractProjectResourceName(plan.name)}`;
+ const createdSheet = await useSheetV1Store().createSheet(
+ projectName,
+ newSheet
+ );
+
+ specToPatch.config.value.sheet = createdSheet.name;
+ const request = create(UpdatePlanRequestSchema, {
+ plan: planPatch,
+ updateMask: { paths: ["specs"] },
+ });
+
+ await planServiceClientConnect.updatePlan(request);
+};