fix(sql-file): preserve Oracle schema during file execution

Closes #10502
This commit is contained in:
t8y2 authored and t8y2 committed 2026-09-29 01:58:25 +08:00
1 parent 9f2f5c525d
commit 597b134d21
20 files changed
+269 -41

No files matched your search

@@ -268,6 +268,7 @@ watch(
v-model:open="dialogs.showSqlFileDialog.value"
:prefill-connection-id="dialogs.sqlFilePrefillConnectionId.value"
:prefill-database="dialogs.sqlFilePrefillDatabase.value"
:prefill-schema="dialogs.sqlFilePrefillSchema.value"
:prefill-file-path="dialogs.sqlFilePrefillFilePath.value"
:prefill-preview="dialogs.sqlFilePrefillPreview.value"
/>
@@ -275,6 +275,7 @@ function executeFile(path: string) {
connectionStore.sqlFileSource = {
connectionId: target.connectionId,
database: target.database,
...(target.schema ? { schema: target.schema } : {}),
filePath: path,
};
}
@@ -26,6 +26,7 @@ const mocks = vi.hoisted(() => ({
connections: [{ id: "mysql-1", name: "MySQL", db_type: "mysql", driver_profile: "mysql", database: "" }] as Array<{ id: string; name: string; db_type: string; driver_profile: string; database: string }>,
getConfig: vi.fn(),
refreshDatabaseTreeNode: vi.fn(),
refreshObjectListTreeNode: vi.fn(),
requestConfirmation: vi.fn(),
toast: vi.fn(),
trackerTask: undefined as any,
@@ -33,7 +34,7 @@ const mocks = vi.hoisted(() => ({
updateSqlFileTask: vi.fn(),
uuid: vi.fn(),
queryStore: {
tabs: [{ id: "mysql-tab", connectionId: "mysql-1", database: "" }],
tabs: [{ id: "mysql-tab", connectionId: "mysql-1", database: "" }] as Array<{ id: string; connectionId: string; database: string; schema?: string; catalog?: string }>,
activeTabId: "mysql-tab" as string | null,
},
}));
@@ -63,7 +64,10 @@ vi.mock("@/composables/useToast", () => ({ useToast: () => ({ toast: mocks.toast
vi.mock("@/composables/useExportTracker", () => ({
useExportTracker: () => ({ addSqlFileTask: mocks.addSqlFileTask, updateSqlFileTask: mocks.updateSqlFileTask }),
}));
vi.mock("@/composables/useDatabaseOptions", () => ({ fetchSqlFileTargetOptions: mocks.fetchSqlFileTargetOptions }));
vi.mock("@/composables/useDatabaseOptions", () => ({
fetchSqlFileTargetOptions: mocks.fetchSqlFileTargetOptions,
namespaceOptionsAreSchemas: (connection: { db_type?: string } | undefined) => ["oracle", "dameng", "oceanbase-oracle"].includes(connection?.db_type ?? ""),
}));
vi.mock("@/lib/connection/connectionLevelDatabaseBootstrap", () => ({ requiresSqlFileTargetDatabaseSelection: () => false, supportsConnectionLevelDatabaseBootstrap: (connection: any) => connection?.db_type === "mysql" }));
vi.mock("@/lib/database/productionSafety", () => ({ productionContextForDatabase: () => ({ active: false, databases: [] }) }));
vi.mock("@/stores/productionSafetyStore", () => ({
@@ -77,6 +81,7 @@ vi.mock("@/stores/connectionStore", () => ({
ensureConnected: mocks.ensureConnected,
getConfig: mocks.getConfig,
refreshDatabaseTreeNode: mocks.refreshDatabaseTreeNode,
refreshObjectListTreeNode: mocks.refreshObjectListTreeNode,
}),
}));
vi.mock("@/stores/queryStore", () => ({ useQueryStore: () => mocks.queryStore }));
@@ -257,6 +262,7 @@ beforeEach(() => {
});
mocks.cancelSqlFileExecution.mockResolvedValue(true);
mocks.refreshDatabaseTreeNode.mockResolvedValue(undefined);
mocks.refreshObjectListTreeNode.mockResolvedValue(undefined);
mocks.requestConfirmation.mockResolvedValue(true);
mocks.uuid.mockReset().mockReturnValueOnce("run-1").mockReturnValueOnce("run-2");
mocks.queryStore.tabs = [{ id: "mysql-tab", connectionId: "mysql-1", database: "" }];
@@ -636,6 +642,93 @@ describe("SqlFileExecutionDialog retries", () => {
expect(mocks.ensureConnected).not.toHaveBeenCalledWith("mysql-1");
});
it.each([
["MySQL", "mysql"],
["PostgreSQL", "postgres"],
])("keeps the selected %s namespace in the database field", async (name, dbType) => {
mocks.connections = [{ id: "database-1", name, db_type: dbType, driver_profile: dbType, database: "configured_db" }];
mocks.queryStore.tabs = [{ id: "database-tab", connectionId: "database-1", database: "selected_db", schema: "remembered_schema" }];
mocks.queryStore.activeTabId = "database-tab";
mocks.fetchSqlFileTargetOptions.mockResolvedValue(["configured_db", "selected_db"]);
await mountReadyDialog();
await completeFirstExecution();
expect(mocks.executeSqlFiles.mock.calls[0]![0]).toMatchObject({ connectionId: "database-1", database: "selected_db" });
expect(mocks.executeSqlFiles.mock.calls[0]![0]).not.toHaveProperty("schema");
});
it("restores and executes a remembered OceanBase Oracle schema without replacing the connection database", async () => {
mocks.connections = [
{ id: "mysql-1", name: "MySQL", db_type: "mysql", driver_profile: "mysql", database: "first" },
{ id: "ob-oracle-1", name: "OceanBase Oracle", db_type: "oceanbase-oracle", driver_profile: "oceanbase-oracle", database: "tenant_service" },
];
rememberExternalSqlFileTarget("/tmp/first.sql", { connectionId: "ob-oracle-1", database: "APP", schema: "APP" });
mocks.fetchSqlFileTargetOptions.mockResolvedValue(["SYS", "APP"]);
await mountReadyDialog();
await completeFirstExecution();
expect(mocks.executeSqlFiles.mock.calls[0]![0]).toMatchObject({
connectionId: "ob-oracle-1",
database: "tenant_service",
schema: "APP",
});
expect(mocks.refreshObjectListTreeNode).toHaveBeenCalledWith("ob-oracle-1", "tenant_service", "APP");
});
it("restores a legacy root-schema target that stored the schema in database", async () => {
mocks.connections = [{ id: "ob-oracle-1", name: "OceanBase Oracle", db_type: "oceanbase-oracle", driver_profile: "oceanbase-oracle", database: "tenant_service" }];
mocks.queryStore.tabs = [{ id: "ob-tab", connectionId: "ob-oracle-1", database: "tenant_service" }];
mocks.queryStore.activeTabId = "ob-tab";
rememberExternalSqlFileTarget("/tmp/first.sql", { connectionId: "ob-oracle-1", database: "APP" });
mocks.fetchSqlFileTargetOptions.mockResolvedValue(["SYS", "APP"]);
await mountReadyDialog();
await completeFirstExecution();
expect(mocks.executeSqlFiles.mock.calls[0]![0]).toMatchObject({
connectionId: "ob-oracle-1",
database: "tenant_service",
schema: "APP",
});
});
it("keeps an empty configured Oracle-family database empty when restoring a legacy schema target", async () => {
mocks.connections = [{ id: "ob-oracle-1", name: "OceanBase Oracle", db_type: "oceanbase-oracle", driver_profile: "oceanbase-oracle", database: "" }];
mocks.queryStore.tabs = [{ id: "ob-tab", connectionId: "ob-oracle-1", database: "" }];
mocks.queryStore.activeTabId = "ob-tab";
rememberExternalSqlFileTarget("/tmp/first.sql", { connectionId: "ob-oracle-1", database: "APP" });
mocks.fetchSqlFileTargetOptions.mockResolvedValue(["SYS", "APP"]);
await mountReadyDialog();
await completeFirstExecution();
expect(mocks.executeSqlFiles.mock.calls[0]![0]).toMatchObject({
connectionId: "ob-oracle-1",
database: "",
schema: "APP",
});
});
it("uses the same OceanBase Oracle database and schema for manual SQL-file execution", async () => {
mocks.connections = [{ id: "ob-oracle-1", name: "OceanBase Oracle", db_type: "oceanbase-oracle", driver_profile: "oceanbase-oracle", database: "tenant_service" }];
mocks.queryStore.tabs = [{ id: "ob-tab", connectionId: "ob-oracle-1", database: "APP", schema: "APP" }];
mocks.queryStore.activeTabId = "ob-tab";
mocks.fetchSqlFileTargetOptions.mockResolvedValue(["SYS", "APP"]);
await enableManualTransaction();
await completeFirstExecution();
expect(mocks.beginManualTransaction).toHaveBeenCalledWith("ob-oracle-1", "tenant_service", "APP");
expect(mocks.executeSqlFiles.mock.calls[0]![0]).toMatchObject({
connectionId: "ob-oracle-1",
database: "tenant_service",
schema: "APP",
txnSessionId: "txn-1",
});
});
it("does not select a connection when the active tab is non-SQL", async () => {
mocks.connections = [
{ id: "mysql-1", name: "MySQL", db_type: "mysql", driver_profile: "mysql", database: "first" },
@@ -20,7 +20,7 @@ import { productionContextForDatabase } from "@/lib/database/productionSafety";
import { connectionIsEffectivelyReadOnly, ensureReadOnlyWriteAccess } from "@/lib/database/readOnlyWriteAccess";
import { supportsSqlFileExecution, supportsTransaction } from "@/lib/database/databaseFeatureSupport";
import { formatError, isManualTransactionSessionExpired } from "@/lib/backend/errorUtils";
import { fetchSqlFileTargetOptions } from "@/composables/useDatabaseOptions";
import { fetchSqlFileTargetOptions, namespaceOptionsAreSchemas } from "@/composables/useDatabaseOptions";
import { requiresSqlFileTargetDatabaseSelection, supportsConnectionLevelDatabaseBootstrap } from "@/lib/connection/connectionLevelDatabaseBootstrap";
import { beginManualTransaction, commitManualTransaction, rollbackManualTransaction, cancelSqlFileExecution, executeSqlFiles, inspectSqlFileTables, listenSqlFileProgress, previewSqlFile, type SqlFilePreview, type SqlFileProgress, type SqlFileStatus, type SqlFileTable } from "@/lib/backend/api";
import { activeTabExternalSqlFileTarget, resolveExternalSqlFileTargetForActiveTab, type ExternalSqlFileTarget } from "@/lib/sql/externalSqlFileTarget";
@@ -41,6 +41,7 @@ const open = defineModel<boolean>("open", { default: false });
const props = defineProps<{
prefillConnectionId?: string;
prefillDatabase?: string;
prefillSchema?: string;
prefillFilePath?: string;
prefillPreview?: SqlFilePreview;
}>();
@@ -117,7 +118,7 @@ function tooltipText(item: SqlFilePreview): string {
const selectingFile = ref(false);
const loadingPreview = ref(false);
const connectionId = ref("");
const database = ref("");
const targetNamespace = ref("");
const databaseOptions = ref<string[]>([]);
const loadingDatabases = ref(false);
const preferredTarget = ref<ExternalSqlFileTarget>();
@@ -165,6 +166,8 @@ interface PerFileSummary {
failureCount: number;
affectedRows: number;
}
type SqlFileExecutionTarget = { database: string; schema?: string };
const perFileResults = ref<PerFileSummary[]>([]);
const currentFileIndex = ref(-1);
const currentFileName = ref("");
@@ -224,7 +227,7 @@ async function finishTransaction(commit: boolean) {
terminalStatus.value = outcomeUnknown ? "error" : committed ? "done" : "cancelled";
if (progress.value) updateSqlFileTask(executionId.value, { ...progress.value, status: terminalStatus.value });
}
await refreshTargetAfterImport();
await refreshTargetAfterImport(sqlFileExecutionTarget());
return true;
} catch (error: any) {
toast(error?.message || String(error), 5000);
@@ -314,7 +317,7 @@ const canStart = computed(() => {
hasDatabaseContext ||= item.establishesDatabaseContext === true;
return true;
});
return !!database.value.trim() || !requiresSqlFileTargetDatabaseSelection(connection, canExecuteWithoutSelectedDatabase);
return !!targetNamespace.value.trim() || !requiresSqlFileTargetDatabaseSelection(connection, canExecuteWithoutSelectedDatabase);
});
const statusTone = computed(() => {
@@ -383,27 +386,62 @@ function statusLabel(status: SqlFileStatus | "idle") {
function resolveInitialTarget(): ExternalSqlFileTarget {
if (props.prefillConnectionId && sqlConnections.value.some((c) => c.id === props.prefillConnectionId)) {
return { connectionId: props.prefillConnectionId, database: props.prefillDatabase ?? "" };
return {
connectionId: props.prefillConnectionId,
database: props.prefillDatabase ?? "",
schema: props.prefillSchema,
};
}
if (props.prefillFilePath) return { connectionId: "", database: "" };
return activeTabExternalSqlFileTarget(queryStore.tabs, queryStore.activeTabId, (connectionId) => store.getConfig(connectionId));
}
function applyTarget(target: ExternalSqlFileTarget) {
preferredTarget.value = target;
connectionId.value = target.connectionId;
database.value = target.database;
function normalizeTargetForConnection(target: ExternalSqlFileTarget): ExternalSqlFileTarget {
const connection = store.getConfig(target.connectionId);
if (!namespaceOptionsAreSchemas(connection)) return target;
const configuredDatabase = connection?.database?.trim() || "";
const requestedDatabase = target.database.trim();
const explicitSchema = target.schema?.trim() || undefined;
const legacySchema = explicitSchema || (requestedDatabase && requestedDatabase !== configuredDatabase ? requestedDatabase : undefined);
return {
...target,
database: configuredDatabase,
...(legacySchema ? { schema: legacySchema } : { schema: undefined }),
};
}
function chooseDatabase(names: string[], id: string) {
const configDatabase = store.getConfig(id)?.database ?? "";
const preferredDatabase = preferredTarget.value?.connectionId === id ? preferredTarget.value.database : props.prefillDatabase;
function applyTarget(target: ExternalSqlFileTarget) {
const normalizedTarget = normalizeTargetForConnection(target);
preferredTarget.value = normalizedTarget;
connectionId.value = normalizedTarget.connectionId;
targetNamespace.value = namespaceOptionsAreSchemas(store.getConfig(normalizedTarget.connectionId)) ? (normalizedTarget.schema ?? "") : normalizedTarget.database;
}
function chooseNamespace(names: string[], id: string) {
const connection = store.getConfig(id);
const optionsAreSchemas = namespaceOptionsAreSchemas(connection);
const preferred = preferredTarget.value?.connectionId === id ? preferredTarget.value : undefined;
const preferredNamespace = preferred ? (optionsAreSchemas ? preferred.schema : preferred.database) : optionsAreSchemas ? (id === props.prefillConnectionId ? props.prefillSchema : undefined) : props.prefillDatabase;
const configuredNamespace = optionsAreSchemas ? (connection?.default_schema ?? "") : (connection?.database ?? "");
if (names.length > 0) {
if (preferredDatabase && names.includes(preferredDatabase)) return preferredDatabase;
if (configDatabase && names.includes(configDatabase)) return configDatabase;
if (preferredNamespace && names.includes(preferredNamespace)) return preferredNamespace;
if (configuredNamespace && names.includes(configuredNamespace)) return configuredNamespace;
return names.length === 1 ? names[0] : "";
}
return preferredDatabase ?? configDatabase;
return preferredNamespace ?? configuredNamespace;
}
function sqlFileExecutionTarget(): SqlFileExecutionTarget {
const namespace = targetNamespace.value.trim();
const connection = store.getConfig(connectionId.value);
if (namespaceOptionsAreSchemas(connection)) {
return {
database: connection?.database?.trim() || "",
...(namespace ? { schema: namespace } : {}),
};
}
return { database: namespace };
}
function resetExecution() {
@@ -444,7 +482,7 @@ async function loadDatabasesForConnection(id: string) {
databaseOptions.value = [];
if (!sqlConnections.value.some((c) => c.id === id)) {
database.value = "";
targetNamespace.value = "";
return;
}
@@ -456,11 +494,11 @@ async function loadDatabasesForConnection(id: string) {
const names = await fetchSqlFileTargetOptions(id, connection);
if (token !== databaseLoadToken) return;
databaseOptions.value = names;
database.value = chooseDatabase(names, id);
targetNamespace.value = chooseNamespace(names, id);
} catch {
if (token !== databaseLoadToken) return;
databaseOptions.value = [];
database.value = chooseDatabase([], id);
targetNamespace.value = chooseNamespace([], id);
} finally {
if (token === databaseLoadToken) {
loadingDatabases.value = false;
@@ -566,11 +604,15 @@ function isTerminalProgress(status: SqlFileStatus): boolean {
return status === "done" || status === "error" || status === "cancelled";
}
async function refreshTargetAfterImport() {
async function refreshTargetAfterImport(target: SqlFileExecutionTarget) {
if (refreshedTarget.value) return;
refreshedTarget.value = true;
try {
await store.refreshDatabaseTreeNode(connectionId.value, database.value.trim());
if (target.schema) {
await store.refreshObjectListTreeNode(connectionId.value, target.database, target.schema);
} else {
await store.refreshDatabaseTreeNode(connectionId.value, target.database);
}
} catch (e: any) {
toast(e?.message || String(e), 5000);
}
@@ -578,13 +620,14 @@ async function refreshTargetAfterImport() {
async function startExecution() {
if (!canStart.value || previews.value.length === 0) return;
const target = sqlFileExecutionTarget();
// Await the unlock guard only when it can actually prompt/block (effectively
// read-only); writable or already-unlocked connections stay synchronous so
// the running state flips in the same tick as the click.
if (connectionIsEffectivelyReadOnly(selectedConnection.value)) {
if (!(await ensureReadOnlyWriteAccess({ connection: selectedConnection.value, source: t("readOnlyUnlock.sourceSqlFile"), treatAsMutation: true }))) return;
}
const productionContext = productionContextForDatabase(selectedConnection.value, database.value);
const productionContext = productionContextForDatabase(selectedConnection.value, target.database);
if (productionContext.active) {
// File previews are truncated, so production file execution is always reviewed instead of inferring safety from a partial preview.
const confirmed = await productionSafetyStore.requestConfirmation({
@@ -593,7 +636,7 @@ async function startExecution() {
.join("\n\n")
.slice(0, 20_000),
connectionName: selectedConnection.value?.name,
database: database.value,
database: target.database,
productionDatabases: productionContext.databases,
source: t("production.sourceSqlFile"),
});
@@ -625,7 +668,7 @@ async function startExecution() {
}
if (manualTransaction.value) {
const sessionId = await beginManualTransaction(connectionId.value, database.value.trim());
const sessionId = target.schema ? await beginManualTransaction(connectionId.value, target.database, target.schema) : await beginManualTransaction(connectionId.value, target.database);
txnSessionId.value = sessionId;
commitUncertain.value = false;
if (disposed || cancelRequested.value) {
@@ -701,7 +744,8 @@ async function startExecution() {
{
executionId: batchId,
connectionId: connectionId.value,
database: database.value.trim(),
database: target.database,
...(target.schema ? { schema: target.schema } : {}),
filePath: executionPaths[0]!,
continueOnError: continueOnError.value,
...(txnSessionId.value ? { txnSessionId: txnSessionId.value } : {}),
@@ -724,7 +768,7 @@ async function startExecution() {
unlisten();
}
if (completedSuccessfully && !txnSessionId.value) await refreshTargetAfterImport();
if (completedSuccessfully && !txnSessionId.value) await refreshTargetAfterImport(target);
} catch (e: any) {
terminalStatus.value = cancelRequested.value ? "cancelled" : "error";
terminalError.value = e?.message || String(e);
@@ -920,7 +964,7 @@ watch(
<div class="space-y-1.5">
<Label class="text-xs">{{ t("sqlFile.database") }}</Label>
<Select v-if="databaseOptions.length" v-model="database" :disabled="executionLocked || loadingDatabases">
<Select v-if="databaseOptions.length" v-model="targetNamespace" :disabled="executionLocked || loadingDatabases">
<SelectTrigger class="h-8 text-xs">
<SelectValue :placeholder="t('sqlFile.selectDatabase')" />
</SelectTrigger>
@@ -929,7 +973,7 @@ watch(
</SelectContent>
</Select>
<div v-else class="relative">
<Input v-model="database" class="h-8 text-xs" :disabled="executionLocked || loadingDatabases" :placeholder="t('sqlFile.databasePlaceholder')" />
<Input v-model="targetNamespace" class="h-8 text-xs" :disabled="executionLocked || loadingDatabases" :placeholder="t('sqlFile.databasePlaceholder')" />
<Loader2 v-if="loadingDatabases" class="absolute right-2 top-2 w-3.5 h-3.5 animate-spin text-muted-foreground" />
</div>
</div>
@@ -215,6 +215,7 @@ describe("namespace options", () => {
it("identifies connection-root namespace options as schemas", () => {
expect(namespaceOptionsAreSchemas({ db_type: "dameng" })).toBe(true);
expect(namespaceOptionsAreSchemas({ db_type: "oracle" })).toBe(true);
expect(namespaceOptionsAreSchemas({ db_type: "oceanbase-oracle" })).toBe(true);
expect(
namespaceOptionsAreSchemas({
db_type: "jdbc",
@@ -64,6 +64,7 @@ const dataComparePrefillTable = ref("");
const dataCompareSessionId = ref<string | null>(null);
const sqlFilePrefillConnectionId = ref("");
const sqlFilePrefillDatabase = ref("");
const sqlFilePrefillSchema = ref<string>();
const sqlFilePrefillFilePath = ref("");
const sqlFilePrefillPreview = ref<SqlFilePreview>();
const diagramPrefillConnectionId = ref("");
@@ -221,6 +222,7 @@ export function useDialogSources() {
if (v) {
sqlFilePrefillConnectionId.value = v.connectionId;
sqlFilePrefillDatabase.value = v.database;
sqlFilePrefillSchema.value = v.schema;
sqlFilePrefillFilePath.value = v.filePath ?? "";
sqlFilePrefillPreview.value = v.preview;
showSqlFileDialog.value = true;
@@ -236,6 +238,7 @@ export function useDialogSources() {
if (!open) {
sqlFilePrefillConnectionId.value = "";
sqlFilePrefillDatabase.value = "";
sqlFilePrefillSchema.value = undefined;
sqlFilePrefillFilePath.value = "";
sqlFilePrefillPreview.value = undefined;
}
@@ -629,6 +632,7 @@ export function useDialogSources() {
dataCompareSessionId,
sqlFilePrefillConnectionId,
sqlFilePrefillDatabase,
sqlFilePrefillSchema,
sqlFilePrefillFilePath,
sqlFilePrefillPreview,
diagramPrefillConnectionId,
@@ -21,6 +21,7 @@ export function useLargeSqlFileStreamingFallback() {
connectionStore.sqlFileSource = {
connectionId: target.connectionId,
database: target.database,
...(target.schema ? { schema: target.schema } : {}),
filePath: path,
};
toast(t("sqlFile.largeFileExecutionOpened", { size: formatSqlFileSize(sizeBytes) }), 6000);
@@ -62,6 +62,7 @@ export function useSidebarTreeToolRuntime(options: SidebarTreeToolRuntimeOptions
connectionStore.sqlFileSource = {
connectionId: activeNode.value.connectionId,
database: activeNode.value.database ?? "",
...(activeNode.value.schema ? { schema: activeNode.value.schema } : {}),
};
}
+1
View File
@@ -5267,6 +5267,7 @@ export interface SqlFileRequest {
executionId: string;
connectionId: string;
database: string;
schema?: string;
filePath: string;
continueOnError: boolean;
txnSessionId?: string;
+1 -1
View File
@@ -589,7 +589,7 @@ export const useConnectionStore = defineStore("connection", () => {
schema?: string;
tableName?: string;
} | null>(null);
const sqlFileSource = ref<{ connectionId: string; database: string; filePath?: string; preview?: SqlFilePreview } | null>(null);
const sqlFileSource = ref<{ connectionId: string; database: string; schema?: string; filePath?: string; preview?: SqlFilePreview } | null>(null);
const diagramSource = ref<{
connectionId: string;
database: string;