From 74960d9fc8bfb0c41b1624e5919f61a218445881 Mon Sep 17 00:00:00 2001 From: lxk955 <164191365+lxk955@users.noreply.github.com> Date: Thu, 8 Oct 2026 11:07:29 +0800 Subject: [PATCH] feat(import): allow schema selection for target table --- .../components/import/TableImportDialog.vue | 126 +++++++++++++++-- .../TableImportDialog.existingTarget.spec.ts | 130 +++++++++++++++++- 2 files changed, 244 insertions(+), 12 deletions(-) diff --git a/apps/desktop/src/components/import/TableImportDialog.vue b/apps/desktop/src/components/import/TableImportDialog.vue index 3715ce3202..a7fee97c32 100644 --- a/apps/desktop/src/components/import/TableImportDialog.vue +++ b/apps/desktop/src/components/import/TableImportDialog.vue @@ -32,6 +32,8 @@ import { import { importPreviewInput, importSourceDisplayName, uploadedImportSourceFromPreview } from "@/lib/import/importSource"; import { getDataTypeOptions } from "@/lib/table/tableStructureEditorState"; import { metadataSchemaForConnection, tableStructureDatabaseTypeForConnection } from "@/lib/database/jdbcDialect"; +import { isSchemaAware } from "@/lib/database/databaseFeatureSupport"; +import { schemaOptionsForConnection } from "@/composables/useSchemaOptions"; import type { ColumnInfo, DatabaseType } from "@/types/database"; import * as api from "@/lib/backend/api"; @@ -185,7 +187,21 @@ const importFileExtensions = computed(() => ["csv", "tsv", "txt", "json", "xlsx" // ordinary INSERT/error behavior instead of approximating an upsert. const IMPORT_CONFLICT_DATABASE_TYPES = new Set(["postgres", "kingbase", "opengauss", "sqlite", "cloudflare-d1", "duckdb", "mysql", "doris", "starrocks"]); const supportsImportConflictPolicy = computed(() => structureDatabaseType.value !== undefined && IMPORT_CONFLICT_DATABASE_TYPES.has(structureDatabaseType.value)); -const targetSchema = computed(() => metadataSchemaForConnection(selectedConnection.value, props.prefillDatabase || "", props.prefillSchema)); +function defaultInitialSchema() { + if (props.prefillSchema !== undefined) return props.prefillSchema; + return metadataSchemaForConnection(selectedConnection.value, props.prefillDatabase || "", props.prefillSchema) || ""; +} + +const isSchemaCapable = computed(() => isSchemaAware(structureDatabaseType.value)); +const showSchemaSelector = computed(() => isSchemaCapable.value && !!props.prefillConnectionId); +const selectedSchema = ref(defaultInitialSchema()); +const schemaOptions = ref([]); +const loadingSchemas = ref(false); +let schemaOptionsRequestId = 0; +const targetSchema = computed(() => { + if (selectedSchema.value) return selectedSchema.value; + return metadataSchemaForConnection(selectedConnection.value, props.prefillDatabase || "", props.prefillSchema) || ""; +}); const dataTypeOptions = computed(() => mergeDataTypeOptions(dynamicDataTypeOptions.value, getDataTypeOptions(structureDatabaseType.value), Object.values(columnDataTypes.value))); const hasExistingTarget = computed(() => !!props.prefillTable || loadingExistingTables.value || existingTableNames.value.length > 0); const targetTableName = computed(() => (targetMode.value === "create" ? newTableName.value.trim() : selectedExistingTable.value)); @@ -258,7 +274,10 @@ const progressPercentFloor = ref(0); const progressPercent = computed(() => Math.max(rawProgressPercent.value, progressPercentFloor.value)); const currentStepIndex = computed(() => wizardSteps.findIndex((step) => step.value === wizardStep.value)); const targetLabel = computed(() => { - const pieces = [selectedConnection.value?.name, props.prefillDatabase, props.prefillSchema, targetTableName.value].filter(Boolean); + // Database-as-schema engines (MySQL/SQLite family) resolve targetSchema to + // the database name; keep it out of the label so it does not render twice. + const schemaPiece = targetSchema.value && targetSchema.value !== props.prefillDatabase ? targetSchema.value : ""; + const pieces = [selectedConnection.value?.name, props.prefillDatabase, schemaPiece, targetTableName.value].filter(Boolean); return pieces.join(" / "); }); const selectedSourceName = computed(() => { @@ -383,6 +402,10 @@ function resetState() { batchEncodingRequestId++; existingTablesRequestId++; targetColumnsRequestId++; + schemaOptionsRequestId++; + schemaOptions.value = []; + loadingSchemas.value = false; + selectedSchema.value = defaultInitialSchema(); if (previewReloadTimer) { clearTimeout(previewReloadTimer); previewReloadTimer = null; @@ -622,8 +645,60 @@ async function loadDataTypeOptions() { } } -async function loadExistingTables() { - if (props.prefillTable || loadingExistingTables.value || existingTableNames.value.length || !props.prefillConnectionId || !props.prefillDatabase) return; +async function loadSchemaOptions() { + const connectionId = props.prefillConnectionId; + const database = props.prefillDatabase || ""; + if (!connectionId || !isSchemaCapable.value) { + schemaOptions.value = []; + loadingSchemas.value = false; + return; + } + const requestId = ++schemaOptionsRequestId; + loadingSchemas.value = true; + try { + await store.ensureConnected(connectionId); + const rawSchemas = await api.listSchemas(connectionId, database); + if (requestId !== schemaOptionsRequestId) return; + const filtered = schemaOptionsForConnection(rawSchemas, selectedConnection.value, database); + const active = selectedSchema.value || targetSchema.value; + if (active && !filtered.includes(active)) { + schemaOptions.value = [active, ...filtered]; + } else { + schemaOptions.value = filtered; + } + } catch { + if (requestId === schemaOptionsRequestId) { + const active = selectedSchema.value || targetSchema.value; + schemaOptions.value = active ? [active] : []; + } + } finally { + if (requestId === schemaOptionsRequestId) { + loadingSchemas.value = false; + } + } +} + +function handleSchemaChange(newSchema: string) { + const trimmed = newSchema.trim(); + if (selectedSchema.value === trimmed) return; + selectedSchema.value = trimmed; + if (trimmed && !schemaOptions.value.includes(trimmed)) { + schemaOptions.value = [trimmed, ...schemaOptions.value]; + } + existingTableNames.value = []; + selectedExistingTable.value = ""; + targetColumns.value = []; + loadedTargetTableName.value = ""; + if (targetMode.value === "existing") { + columnMapping.value = {}; + } + if (targetMode.value === "existing" || wizardStep.value === "options") { + void loadExistingTables(true); + } +} + +async function loadExistingTables(force = false) { + if (props.prefillTable || (!force && (loadingExistingTables.value || existingTableNames.value.length > 0)) || !props.prefillConnectionId || !props.prefillDatabase) return; const requestId = ++existingTablesRequestId; loadingExistingTables.value = true; errorMessage.value = ""; @@ -994,9 +1069,9 @@ async function startImport() { ); progress.value = { importId: summary.importId, status: "done", rowsImported: summary.rowsImported, totalRows: summary.totalRows, elapsedMs: summary.elapsedMs }; toast(t("tableImport.success", { count: summary.rowsImported }), 2500); - store.invalidateMetadataCache(props.prefillConnectionId, props.prefillDatabase || "", props.prefillSchema || undefined, tableName); + store.invalidateMetadataCache(props.prefillConnectionId, props.prefillDatabase || "", targetSchema.value || undefined, tableName); if (targetMode.value === "create") { - store.refreshObjectListTreeNode(props.prefillConnectionId, props.prefillDatabase || "", props.prefillSchema || undefined).catch((error) => { + store.refreshObjectListTreeNode(props.prefillConnectionId, props.prefillDatabase || "", targetSchema.value || undefined).catch((error) => { console.warn("[DBX][table-import:refresh-created-table-failed]", error); }); } @@ -1102,12 +1177,12 @@ async function startBatchImport() { task.rowsImported = summary.rowsImported; completedRows += summary.rowsImported; completedBytes += task.preview.sizeBytes; - store.invalidateMetadataCache(props.prefillConnectionId, props.prefillDatabase || "", props.prefillSchema || undefined, task.tableName); + store.invalidateMetadataCache(props.prefillConnectionId, props.prefillDatabase || "", targetSchema.value || undefined, task.tableName); } refreshImportElapsedClock(); progress.value = { importId: importId.value, status: "done", phase: "done", rowsImported: completedRows, totalRows: completedRows, totalRowsExact: true, bytesRead: totalBytes, totalBytes, elapsedMs: liveElapsedMs.value }; toast(t("tableImport.success", { count: completedRows }), 2500); - store.refreshObjectListTreeNode(props.prefillConnectionId, props.prefillDatabase || "", props.prefillSchema || undefined).catch((error) => { + store.refreshObjectListTreeNode(props.prefillConnectionId, props.prefillDatabase || "", targetSchema.value || undefined).catch((error) => { console.warn("[DBX][table-import:refresh-created-table-failed]", error); }); } catch (e: any) { @@ -1212,6 +1287,7 @@ watch( (value) => { if (value) { resetState(); + if (showSchemaSelector.value) void loadSchemaOptions(); void loadTargetColumns(); void loadDataTypeOptions(); } else { @@ -1231,12 +1307,16 @@ watch([textEncoding, decimalSeparator], schedulePreviewReloadAfterEncodingChange watch([newTableName, columnMapping, columnDataTypes], saveActiveBatchTask, { deep: true }); watch(wizardStep, (step) => { if (step !== "mapping") closeDataTypePicker(); - if (step === "options") void loadExistingTables(); + if (step === "options") { + if (showSchemaSelector.value) void loadSchemaOptions(); + void loadExistingTables(); + } }); watch(targetMode, (mode) => { if (mode === "existing") { columnDataTypes.value = {}; dynamicDataTypeOptions.value = []; + if (!existingTableNames.value.length) void loadExistingTables(); void loadTargetColumns(); } else { targetColumnsRequestId++; @@ -1402,7 +1482,7 @@ watch(rawProgressPercent, (percent) => { -
+
@@ -1416,6 +1496,31 @@ watch(rawProgressPercent, (percent) => {
+
+ +
+ {{ targetSchema || props.prefillSchema || "-" }} +
+ +
@@ -1424,6 +1529,7 @@ watch(rawProgressPercent, (percent) => {
({ ensureConnected: vi.fn().mockResolvedValue(undefined), + listSchemas: vi.fn().mockResolvedValue(["dbo", "sales", "analytics"]), listTables: vi.fn().mockResolvedValue([ { name: "existing_target", table_type: "TABLE" }, { name: "archived_target", table_type: "TABLE" }, @@ -36,6 +37,7 @@ vi.mock("@/stores/connectionStore", () => ({ getConfig: (id: string) => { if (id === "connection-1") return { id, name: "SQLite", db_type: "sqlite" }; if (id === "postgres-1") return { id, name: "PostgreSQL", db_type: "postgres" }; + if (id === "sqlserver-1") return { id, name: "SQL Server", db_type: "sqlserver" }; return undefined; }, ensureConnected: mocks.ensureConnected, @@ -49,6 +51,7 @@ vi.mock("@/stores/settingsStore", () => ({ })); vi.mock("@/lib/backend/api", () => ({ + listSchemas: mocks.listSchemas, listTables: mocks.listTables, getColumns: mocks.getColumns, listDataTypes: mocks.listDataTypes, @@ -146,12 +149,13 @@ vi.mock("@/components/ui/searchable-select", async () => { disabled: Boolean, }, emits: ["update:modelValue"], - setup(props, { emit }) { + setup(props, { attrs, emit }) { return () => h( "select", { - class: "existing-table-select-stub", + ...attrs, + class: attrs["data-testid"] === "target-schema-select" ? "existing-schema-select-stub" : "existing-table-select-stub", value: props.modelValue, disabled: props.disabled, onChange: (event: Event) => emit("update:modelValue", (event.target as HTMLSelectElement).value), @@ -228,12 +232,24 @@ async function selectExistingTarget(tableName: string) { await nextTick(); } +async function selectSchemaTarget(schemaName: string) { + const schemaSelect = document.body.querySelector("select.existing-schema-select-stub"); + expect(schemaSelect).toBeTruthy(); + if (schemaSelect) { + schemaSelect.value = schemaName; + schemaSelect.dispatchEvent(new Event("change", { bubbles: true })); + } + await nextTick(); + await flushAsyncUpdates(); +} + function buttonContaining(text: string) { return [...document.body.querySelectorAll("button")].find((button) => button.textContent?.includes(text)); } beforeEach(() => { mocks.ensureConnected.mockReset().mockResolvedValue(undefined); + mocks.listSchemas.mockReset().mockResolvedValue(["dbo", "sales", "analytics"]); mocks.listTables.mockReset().mockResolvedValue([ { name: "existing_target", table_type: "TABLE" }, { name: "archived_target", table_type: "TABLE" }, @@ -452,4 +468,114 @@ describe("TableImportDialog existing targets", () => { expect(updateOption?.disabled).toBe(true); expect(document.body.textContent).toContain("requires primary-key metadata"); }); + + it("allows selecting target schema on schema-aware databases and reloads tables", async () => { + i18n.global.locale.value = "en"; + await mountDialog({ connectionId: "sqlserver-1", database: "mydb" }); + await selectWorkbook(); + + await vi.waitFor(() => { + expect(mocks.listSchemas).toHaveBeenCalledWith("sqlserver-1", "mydb"); + expect(mocks.listTables).toHaveBeenCalledWith("sqlserver-1", "mydb", "dbo", undefined, undefined, undefined, ["TABLE"]); + }); + + const schemaSelect = document.body.querySelector("select.existing-schema-select-stub"); + expect(schemaSelect).toBeTruthy(); + expect(schemaSelect?.value).toBe("dbo"); + + mocks.listTables.mockClear(); + mocks.listTables.mockResolvedValueOnce([{ name: "sales_orders", table_type: "TABLE" }]); + + await selectSchemaTarget("sales"); + + await vi.waitFor(() => { + expect(mocks.listTables).toHaveBeenCalledWith("sqlserver-1", "mydb", "sales", undefined, undefined, undefined, ["TABLE"]); + }); + + buttonContaining("Existing table")?.click(); + await flushAsyncUpdates(); + await selectExistingTarget("sales_orders"); + + await vi.waitFor(() => { + expect(mocks.getColumns).toHaveBeenCalledWith("sqlserver-1", "mydb", "sales", "sales_orders"); + expect(buttonContaining("Next")?.disabled).toBe(false); + }); + + buttonContaining("Next")?.click(); + await flushAsyncUpdates(); + buttonContaining("Next")?.click(); + await flushAsyncUpdates(); + buttonContaining("Start Import")?.click(); + await flushAsyncUpdates(); + + expect(mocks.importTableFile).toHaveBeenCalledWith( + expect.objectContaining({ + connectionId: "sqlserver-1", + database: "mydb", + schema: "sales", + table: "sales_orders", + }), + expect.any(Function), + ); + }); + + it("uses the selected schema when creating a new table on a schema-aware database", async () => { + i18n.global.locale.value = "en"; + await mountDialog({ connectionId: "sqlserver-1", database: "mydb" }); + await selectWorkbook(); + + await vi.waitFor(() => expect(mocks.listSchemas).toHaveBeenCalledWith("sqlserver-1", "mydb")); + + await selectSchemaTarget("analytics"); + + const tableNameInput = document.body.querySelector("input.font-mono"); + expect(tableNameInput).toBeTruthy(); + tableNameInput!.value = "analytics_summary"; + tableNameInput!.dispatchEvent(new Event("input", { bubbles: true })); + await flushAsyncUpdates(); + + buttonContaining("Next")?.click(); + await flushAsyncUpdates(); + buttonContaining("Next")?.click(); + await flushAsyncUpdates(); + buttonContaining("Start Import")?.click(); + await flushAsyncUpdates(); + + expect(mocks.importTableFile).toHaveBeenCalledWith( + expect.objectContaining({ + connectionId: "sqlserver-1", + database: "mydb", + schema: "analytics", + table: "analytics_summary", + createTable: true, + }), + expect.any(Function), + ); + }); + + it("renders read-only schema when prefillTable is provided on schema-aware connection", async () => { + i18n.global.locale.value = "en"; + await mountDialog({ connectionId: "sqlserver-1", database: "mydb", schema: "sales", prefillTable: "existing_target" }); + await selectWorkbook(); + + expect(document.body.querySelector("select.existing-schema-select-stub")).toBeNull(); + expect(document.body.textContent).toContain("sales"); + }); + + it("does not render schema selector for non-schema-aware connections", async () => { + i18n.global.locale.value = "en"; + await mountDialog({ connectionId: "connection-1", database: "main" }); + await selectWorkbook(); + + expect(document.body.querySelector("select.existing-schema-select-stub")).toBeNull(); + }); + + it("does not repeat the database as schema in the target label", async () => { + i18n.global.locale.value = "en"; + await mountDialog({ connectionId: "connection-1", database: "main", prefillTable: "existing_target" }); + await selectWorkbook(); + + expect(document.body.textContent).toContain("SQLite / main / existing_target"); + expect(document.body.textContent).not.toContain("main / main"); + }); });