feat(editor): add global default transaction commit mode
This commit is contained in:
1 parent
d3a97164aa
commit
fdfad88590
17 files changed
+229
-9
No files matched your search
@@ -0,0 +1,105 @@
|
||||
import { createPinia, setActivePinia } from "pinia";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
analyzeEditableQueryEditability: vi.fn(),
|
||||
beginManualTransaction: vi.fn(),
|
||||
closeClientConnectionSession: vi.fn(),
|
||||
closeQuerySession: vi.fn(),
|
||||
executeInManualTransaction: vi.fn(),
|
||||
executeMulti: vi.fn(),
|
||||
getConnectionConfig: vi.fn(),
|
||||
prepareQueryPaginationExecutionPlan: vi.fn(),
|
||||
saveOpenTabsState: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/backend/api", () => ({
|
||||
analyzeEditableQueryEditability: mocks.analyzeEditableQueryEditability,
|
||||
beginManualTransaction: mocks.beginManualTransaction,
|
||||
closeClientConnectionSession: mocks.closeClientConnectionSession,
|
||||
closeQuerySession: mocks.closeQuerySession,
|
||||
executeInManualTransaction: mocks.executeInManualTransaction,
|
||||
executeMulti: mocks.executeMulti,
|
||||
prepareQueryPaginationExecutionPlan: mocks.prepareQueryPaginationExecutionPlan,
|
||||
saveOpenTabsState: mocks.saveOpenTabsState,
|
||||
}));
|
||||
|
||||
vi.mock("@/stores/connectionStore", () => ({
|
||||
useConnectionStore: () => ({
|
||||
ensureConnected: vi.fn().mockResolvedValue(undefined),
|
||||
getConfig: mocks.getConnectionConfig,
|
||||
recordConnectionLostError: vi.fn(),
|
||||
}),
|
||||
}));
|
||||
|
||||
// The in-memory editor settings are mutated per test so a fresh tab reflects the
|
||||
// configured default transaction mode. Missing key falls back to auto-commit.
|
||||
const editorSettings: Record<string, unknown> = {
|
||||
autoCalculateTotalRows: false,
|
||||
continueOnErrorOnBatch: false,
|
||||
pageSize: 100,
|
||||
queryResultMaxRowsEnabled: false,
|
||||
queryResultMaxRows: 1000,
|
||||
openTabsRestoreMode: "all",
|
||||
confirmUnsavedSqlClose: false,
|
||||
};
|
||||
|
||||
vi.mock("@/stores/settingsStore", () => ({
|
||||
useSettingsStore: () => ({ editorSettings }),
|
||||
}));
|
||||
|
||||
describe("queryStore default transaction mode", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
setActivePinia(createPinia());
|
||||
mocks.getConnectionConfig.mockReturnValue({
|
||||
id: "oracle-1",
|
||||
name: "Oracle",
|
||||
db_type: "oracle",
|
||||
database: "ORCL",
|
||||
query_timeout_secs: 30,
|
||||
});
|
||||
mocks.prepareQueryPaginationExecutionPlan.mockImplementation(async (options) => ({
|
||||
sqlToExecute: options.sql,
|
||||
pageSql: undefined,
|
||||
pageLimit: undefined,
|
||||
pageOffset: undefined,
|
||||
countSql: undefined,
|
||||
useAgentResultSession: false,
|
||||
}));
|
||||
mocks.analyzeEditableQueryEditability.mockResolvedValue({ editable: false, reason: "not-select" });
|
||||
mocks.saveOpenTabsState.mockResolvedValue(undefined);
|
||||
delete editorSettings.defaultTransactionMode;
|
||||
});
|
||||
|
||||
it("creates a query tab in manual transaction mode when configured", async () => {
|
||||
editorSettings.defaultTransactionMode = "manual";
|
||||
|
||||
const { useQueryStore } = await import("@/stores/queryStore");
|
||||
const store = useQueryStore();
|
||||
const tabId = store.createTab("oracle-1", "ORCL", "Query", "query", "APP");
|
||||
|
||||
const tab = store.tabs.find((item) => item.id === tabId)!;
|
||||
expect(tab.autoCommit).toBe(false);
|
||||
});
|
||||
|
||||
it("creates a query tab in auto-commit mode by default", async () => {
|
||||
const { useQueryStore } = await import("@/stores/queryStore");
|
||||
const store = useQueryStore();
|
||||
const tabId = store.createTab("oracle-1", "ORCL", "Query", "query", "APP");
|
||||
|
||||
const tab = store.tabs.find((item) => item.id === tabId)!;
|
||||
expect(tab.autoCommit).toBe(true);
|
||||
});
|
||||
|
||||
it("creates a query tab in auto-commit mode when explicitly set to auto", async () => {
|
||||
editorSettings.defaultTransactionMode = "auto";
|
||||
|
||||
const { useQueryStore } = await import("@/stores/queryStore");
|
||||
const store = useQueryStore();
|
||||
const tabId = store.createTab("oracle-1", "ORCL", "Query", "query", "APP");
|
||||
|
||||
const tab = store.tabs.find((item) => item.id === tabId)!;
|
||||
expect(tab.autoCommit).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -184,6 +184,12 @@ describe("normalizeEditorSettings", () => {
|
||||
expect(normalizeEditorSettings({ sqlSemanticDiagnosticsEnabled: false } as any).sqlSemanticDiagnosticsMode).toBe("disabled");
|
||||
});
|
||||
|
||||
it("defaults the transaction commit mode to auto and preserves manual", () => {
|
||||
expect(normalizeEditorSettings({}).defaultTransactionMode).toBe("auto");
|
||||
expect(normalizeEditorSettings({ defaultTransactionMode: "manual" }).defaultTransactionMode).toBe("manual");
|
||||
expect(normalizeEditorSettings({ defaultTransactionMode: "bogus" } as any).defaultTransactionMode).toBe("auto");
|
||||
});
|
||||
|
||||
it("defaults update downloads to the official source", () => {
|
||||
expect(normalizeEditorSettings({}).updateDownloadSource).toBe("official");
|
||||
});
|
||||
|
||||
@@ -733,6 +733,9 @@ export const useQueryStore = defineStore("query", () => {
|
||||
const t = getI18nT();
|
||||
const settingsStore = useSettingsStore();
|
||||
const tabs = ref<QueryTab[]>([]);
|
||||
// Default auto-commit for a fresh query tab follows the user's global
|
||||
// "默认事务提交方式" setting (Settings > Editor), not a hard-coded constant.
|
||||
const defaultAutoCommitForDbTypeWithSetting = (dbType?: string) => defaultAutoCommitForDbType(dbType, settingsStore.editorSettings.defaultTransactionMode);
|
||||
// A stable Set of "connectionId\x00database" keys. Computed only from the
|
||||
// minimal tab identity fields so that it does NOT invalidate when other
|
||||
// properties change (isExecuting, result, sql, tableMeta...). Previously
|
||||
@@ -1655,7 +1658,7 @@ export const useQueryStore = defineStore("query", () => {
|
||||
for (const tab of restored.tabs) {
|
||||
const connection = connectionStore.getConfig(tab.connectionId);
|
||||
if (tab.mode === "query" && tab.autoCommit === undefined) {
|
||||
tab.autoCommit = defaultAutoCommitForDbType(connection?.db_type);
|
||||
tab.autoCommit = defaultAutoCommitForDbTypeWithSetting(connection?.db_type);
|
||||
} else if (tab.mode === "data" && connection) {
|
||||
tab.schema = connectionObjectTreeNodeSchema(connection, tab.database, tab.schema);
|
||||
}
|
||||
@@ -1837,7 +1840,7 @@ export const useQueryStore = defineStore("query", () => {
|
||||
isCancelling: false,
|
||||
isExplaining: false,
|
||||
mode,
|
||||
...(mode === "query" ? { autoCommit: defaultAutoCommitForDbType(dbType) } : {}),
|
||||
...(mode === "query" ? { autoCommit: defaultAutoCommitForDbTypeWithSetting(dbType) } : {}),
|
||||
};
|
||||
if (mode === "query") tab.originalSql = initialSql ?? "";
|
||||
const activeIndex = options.insertAfterActive ? tabs.value.findIndex((item) => item.id === activeTabId.value) : -1;
|
||||
@@ -1931,7 +1934,7 @@ export const useQueryStore = defineStore("query", () => {
|
||||
isCancelling: false,
|
||||
isExplaining: false,
|
||||
mode: "query",
|
||||
autoCommit: defaultAutoCommitForDbType(dbType),
|
||||
autoCommit: defaultAutoCommitForDbTypeWithSetting(dbType),
|
||||
};
|
||||
tabs.value.push(tab);
|
||||
activeTabId.value = id;
|
||||
@@ -3286,7 +3289,7 @@ export const useQueryStore = defineStore("query", () => {
|
||||
if (tab.txnSessionId) void rollbackTransaction(tab.id);
|
||||
if (options?.resetAutoCommit) {
|
||||
const dbType = useConnectionStore().getConfig(tab.connectionId)?.db_type;
|
||||
tab.autoCommit = defaultAutoCommitForDbType(dbType);
|
||||
tab.autoCommit = defaultAutoCommitForDbTypeWithSetting(dbType);
|
||||
}
|
||||
clearOracleTxnPossiblyDirty(tab);
|
||||
tab.txnAutoRolledBack = false;
|
||||
@@ -3441,7 +3444,7 @@ export const useQueryStore = defineStore("query", () => {
|
||||
isCancelling: false,
|
||||
isExplaining: false,
|
||||
mode: "query",
|
||||
autoCommit: defaultAutoCommitForDbType(dbType),
|
||||
autoCommit: defaultAutoCommitForDbTypeWithSetting(dbType),
|
||||
editorSelection: restoredPosition.selection,
|
||||
editorViewport: restoredPosition.viewport,
|
||||
};
|
||||
@@ -6361,7 +6364,7 @@ export const useQueryStore = defineStore("query", () => {
|
||||
isCancelling: false,
|
||||
isExplaining: false,
|
||||
mode: "query",
|
||||
autoCommit: defaultAutoCommitForDbType(dbType),
|
||||
autoCommit: defaultAutoCommitForDbTypeWithSetting(dbType),
|
||||
};
|
||||
if (!restoreCachedResultPayload(tab, archive.snapshot)) return undefined;
|
||||
const activeRun = tab.resultRuns?.find((run) => run.id === tab.activeResultRunId) ?? tab.resultRuns?.[0];
|
||||
|
||||
@@ -61,6 +61,7 @@ export type UpdateDownloadSource = "official" | "cnb";
|
||||
export type SqlSemanticDiagnosticsMode = "auto" | "enabled" | "disabled";
|
||||
export type OpenTabsRestoreMode = "all" | "pinned" | "none";
|
||||
export type AppCloseUnsavedTabsMode = "prompt" | "keep-drafts";
|
||||
export type DefaultTransactionMode = "auto" | "manual";
|
||||
|
||||
export const DEFAULT_SIDEBAR_TABLE_PAGE_SIZE = 1000;
|
||||
export const DUCKDB_WORKER_MAX_PROCESSES_MIN = 1;
|
||||
@@ -644,6 +645,7 @@ export interface EditorSettings {
|
||||
continueOnErrorOnBatch: boolean;
|
||||
clickTableNavigationTarget: ClickTableNavigationTarget;
|
||||
completionTriggerMode: SqlCompletionTriggerMode;
|
||||
defaultTransactionMode: DefaultTransactionMode;
|
||||
}
|
||||
|
||||
export interface ToolbarItems {
|
||||
@@ -857,6 +859,7 @@ export const DEFAULT_EDITOR_SETTINGS: EditorSettings = {
|
||||
continueOnErrorOnBatch: false,
|
||||
clickTableNavigationTarget: "data",
|
||||
completionTriggerMode: "positional",
|
||||
defaultTransactionMode: "auto",
|
||||
};
|
||||
|
||||
export const STORAGE_KEY = "dbx-editor-settings";
|
||||
@@ -970,6 +973,10 @@ function normalizeClickTableNavigationTarget(value: unknown): ClickTableNavigati
|
||||
return value === "data" || value === "ddl" ? value : DEFAULT_EDITOR_SETTINGS.clickTableNavigationTarget;
|
||||
}
|
||||
|
||||
function normalizeDefaultTransactionMode(value: unknown): DefaultTransactionMode {
|
||||
return value === "manual" ? value : DEFAULT_EDITOR_SETTINGS.defaultTransactionMode;
|
||||
}
|
||||
|
||||
function normalizeOpenTabsRestoreMode(value: unknown, legacyRestoreOpenTabsOnLaunch?: unknown): OpenTabsRestoreMode {
|
||||
if (value === "all" || value === "pinned" || value === "none") return value;
|
||||
if (typeof legacyRestoreOpenTabsOnLaunch === "boolean") return legacyRestoreOpenTabsOnLaunch ? "all" : "none";
|
||||
@@ -1273,6 +1280,7 @@ export function normalizeEditorSettings(settings: Partial<EditorSettings>, exist
|
||||
continueOnErrorOnBatch: settings.continueOnErrorOnBatch === true,
|
||||
clickTableNavigationTarget: normalizeClickTableNavigationTarget(settings.clickTableNavigationTarget),
|
||||
completionTriggerMode: normalizeCompletionTriggerMode(settings.completionTriggerMode),
|
||||
defaultTransactionMode: normalizeDefaultTransactionMode(settings.defaultTransactionMode),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1852,6 +1860,7 @@ export const useSettingsStore = defineStore("settings", () => {
|
||||
if (partial.continueOnErrorOnBatch !== undefined) editorSettings.value.continueOnErrorOnBatch = partial.continueOnErrorOnBatch === true;
|
||||
if (partial.clickTableNavigationTarget !== undefined) editorSettings.value.clickTableNavigationTarget = normalizeClickTableNavigationTarget(partial.clickTableNavigationTarget);
|
||||
if (partial.completionTriggerMode !== undefined) editorSettings.value.completionTriggerMode = normalizeCompletionTriggerMode(partial.completionTriggerMode);
|
||||
if (partial.defaultTransactionMode !== undefined) editorSettings.value.defaultTransactionMode = normalizeDefaultTransactionMode(partial.defaultTransactionMode);
|
||||
if (partial.flatteningMultiLineText !== undefined) editorSettings.value.flatteningMultiLineText = partial.flatteningMultiLineText;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user