feat(import): allow schema selection for target table

This commit is contained in:
lxk955 authored and GitHub committed 2026-10-08 11:07:29 +08:00
1 parent 33f7c19675
commit 74960d9fc8
2 files changed
+244 -12

No files matched your search

@@ -32,6 +32,8 @@ import {
import { importPreviewInput, importSourceDisplayName, uploadedImportSourceFromPreview } from "@/lib/import/importSource"; import { importPreviewInput, importSourceDisplayName, uploadedImportSourceFromPreview } from "@/lib/import/importSource";
import { getDataTypeOptions } from "@/lib/table/tableStructureEditorState"; import { getDataTypeOptions } from "@/lib/table/tableStructureEditorState";
import { metadataSchemaForConnection, tableStructureDatabaseTypeForConnection } from "@/lib/database/jdbcDialect"; 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 type { ColumnInfo, DatabaseType } from "@/types/database";
import * as api from "@/lib/backend/api"; 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. // ordinary INSERT/error behavior instead of approximating an upsert.
const IMPORT_CONFLICT_DATABASE_TYPES = new Set<DatabaseType>(["postgres", "kingbase", "opengauss", "sqlite", "cloudflare-d1", "duckdb", "mysql", "doris", "starrocks"]); const IMPORT_CONFLICT_DATABASE_TYPES = new Set<DatabaseType>(["postgres", "kingbase", "opengauss", "sqlite", "cloudflare-d1", "duckdb", "mysql", "doris", "starrocks"]);
const supportsImportConflictPolicy = computed(() => structureDatabaseType.value !== undefined && IMPORT_CONFLICT_DATABASE_TYPES.has(structureDatabaseType.value)); 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<string[]>([]);
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 dataTypeOptions = computed(() => mergeDataTypeOptions(dynamicDataTypeOptions.value, getDataTypeOptions(structureDatabaseType.value), Object.values(columnDataTypes.value)));
const hasExistingTarget = computed(() => !!props.prefillTable || loadingExistingTables.value || existingTableNames.value.length > 0); const hasExistingTarget = computed(() => !!props.prefillTable || loadingExistingTables.value || existingTableNames.value.length > 0);
const targetTableName = computed(() => (targetMode.value === "create" ? newTableName.value.trim() : selectedExistingTable.value)); 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 progressPercent = computed(() => Math.max(rawProgressPercent.value, progressPercentFloor.value));
const currentStepIndex = computed(() => wizardSteps.findIndex((step) => step.value === wizardStep.value)); const currentStepIndex = computed(() => wizardSteps.findIndex((step) => step.value === wizardStep.value));
const targetLabel = computed(() => { 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(" / "); return pieces.join(" / ");
}); });
const selectedSourceName = computed(() => { const selectedSourceName = computed(() => {
@@ -383,6 +402,10 @@ function resetState() {
batchEncodingRequestId++; batchEncodingRequestId++;
existingTablesRequestId++; existingTablesRequestId++;
targetColumnsRequestId++; targetColumnsRequestId++;
schemaOptionsRequestId++;
schemaOptions.value = [];
loadingSchemas.value = false;
selectedSchema.value = defaultInitialSchema();
if (previewReloadTimer) { if (previewReloadTimer) {
clearTimeout(previewReloadTimer); clearTimeout(previewReloadTimer);
previewReloadTimer = null; previewReloadTimer = null;
@@ -622,8 +645,60 @@ async function loadDataTypeOptions() {
} }
} }
async function loadExistingTables() { async function loadSchemaOptions() {
if (props.prefillTable || loadingExistingTables.value || existingTableNames.value.length || !props.prefillConnectionId || !props.prefillDatabase) return; 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; const requestId = ++existingTablesRequestId;
loadingExistingTables.value = true; loadingExistingTables.value = true;
errorMessage.value = ""; 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 }; progress.value = { importId: summary.importId, status: "done", rowsImported: summary.rowsImported, totalRows: summary.totalRows, elapsedMs: summary.elapsedMs };
toast(t("tableImport.success", { count: summary.rowsImported }), 2500); 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") { 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); console.warn("[DBX][table-import:refresh-created-table-failed]", error);
}); });
} }
@@ -1102,12 +1177,12 @@ async function startBatchImport() {
task.rowsImported = summary.rowsImported; task.rowsImported = summary.rowsImported;
completedRows += summary.rowsImported; completedRows += summary.rowsImported;
completedBytes += task.preview.sizeBytes; 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(); refreshImportElapsedClock();
progress.value = { importId: importId.value, status: "done", phase: "done", rowsImported: completedRows, totalRows: completedRows, totalRowsExact: true, bytesRead: totalBytes, totalBytes, elapsedMs: liveElapsedMs.value }; 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); 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); console.warn("[DBX][table-import:refresh-created-table-failed]", error);
}); });
} catch (e: any) { } catch (e: any) {
@@ -1212,6 +1287,7 @@ watch(
(value) => { (value) => {
if (value) { if (value) {
resetState(); resetState();
if (showSchemaSelector.value) void loadSchemaOptions();
void loadTargetColumns(); void loadTargetColumns();
void loadDataTypeOptions(); void loadDataTypeOptions();
} else { } else {
@@ -1231,12 +1307,16 @@ watch([textEncoding, decimalSeparator], schedulePreviewReloadAfterEncodingChange
watch([newTableName, columnMapping, columnDataTypes], saveActiveBatchTask, { deep: true }); watch([newTableName, columnMapping, columnDataTypes], saveActiveBatchTask, { deep: true });
watch(wizardStep, (step) => { watch(wizardStep, (step) => {
if (step !== "mapping") closeDataTypePicker(); if (step !== "mapping") closeDataTypePicker();
if (step === "options") void loadExistingTables(); if (step === "options") {
if (showSchemaSelector.value) void loadSchemaOptions();
void loadExistingTables();
}
}); });
watch(targetMode, (mode) => { watch(targetMode, (mode) => {
if (mode === "existing") { if (mode === "existing") {
columnDataTypes.value = {}; columnDataTypes.value = {};
dynamicDataTypeOptions.value = []; dynamicDataTypeOptions.value = [];
if (!existingTableNames.value.length) void loadExistingTables();
void loadTargetColumns(); void loadTargetColumns();
} else { } else {
targetColumnsRequestId++; targetColumnsRequestId++;
@@ -1402,7 +1482,7 @@ watch(rawProgressPercent, (percent) => {
</div> </div>
</div> </div>
<div class="grid grid-cols-[minmax(0,1fr)_minmax(220px,320px)] gap-3 rounded-md border p-3"> <div class="grid gap-3 rounded-md border p-3" :class="showSchemaSelector ? 'grid-cols-1 md:grid-cols-[minmax(0,1.2fr)_minmax(140px,1fr)_minmax(180px,1.2fr)]' : 'grid-cols-[minmax(0,1fr)_minmax(220px,320px)]'">
<div class="space-y-1.5"> <div class="space-y-1.5">
<Label class="text-xs">{{ t("tableImport.targetMode") }}</Label> <Label class="text-xs">{{ t("tableImport.targetMode") }}</Label>
<div class="grid grid-cols-2 gap-2"> <div class="grid grid-cols-2 gap-2">
@@ -1416,6 +1496,31 @@ watch(rawProgressPercent, (percent) => {
</button> </button>
</div> </div>
</div> </div>
<div v-if="showSchemaSelector" class="space-y-1.5">
<Label class="text-xs">{{ t("transfer.targetSchema") }}</Label>
<div v-if="props.prefillTable" class="flex h-8 items-center rounded-md border px-2 text-xs font-mono">
<span class="truncate">{{ targetSchema || props.prefillSchema || "-" }}</span>
</div>
<SearchableSelect
v-else
data-testid="target-schema-select"
:model-value="selectedSchema"
:options="schemaOptions.length ? schemaOptions : selectedSchema ? [selectedSchema] : []"
:placeholder="t('transfer.selectSchema')"
:search-placeholder="t('transfer.searchSchema')"
:empty-text="t('common.noResults')"
:loading-text="t('common.loading')"
:loading="loadingSchemas"
:allow-custom="true"
trigger-class="h-8 font-mono text-xs"
@update:model-value="handleSchemaChange"
@update:open="
(isOpen) => {
if (isOpen) void loadSchemaOptions();
}
"
/>
</div>
<div class="space-y-1.5"> <div class="space-y-1.5">
<Label class="text-xs">{{ t("tableImport.targetTableName") }}</Label> <Label class="text-xs">{{ t("tableImport.targetTableName") }}</Label>
<Input v-if="targetMode === 'create'" v-model="newTableName" class="h-8 text-xs font-mono" /> <Input v-if="targetMode === 'create'" v-model="newTableName" class="h-8 text-xs font-mono" />
@@ -1424,6 +1529,7 @@ watch(rawProgressPercent, (percent) => {
</div> </div>
<SearchableSelect <SearchableSelect
v-else v-else
data-testid="target-table-select"
:model-value="selectedExistingTable" :model-value="selectedExistingTable"
:options="existingTableNames" :options="existingTableNames"
:placeholder="t('tableImport.noExistingTarget')" :placeholder="t('tableImport.noExistingTarget')"
@@ -7,6 +7,7 @@ import type { TableImportProgress, TableImportRequest, TableImportSummary } from
const mocks = vi.hoisted(() => ({ const mocks = vi.hoisted(() => ({
ensureConnected: vi.fn().mockResolvedValue(undefined), ensureConnected: vi.fn().mockResolvedValue(undefined),
listSchemas: vi.fn().mockResolvedValue(["dbo", "sales", "analytics"]),
listTables: vi.fn().mockResolvedValue([ listTables: vi.fn().mockResolvedValue([
{ name: "existing_target", table_type: "TABLE" }, { name: "existing_target", table_type: "TABLE" },
{ name: "archived_target", table_type: "TABLE" }, { name: "archived_target", table_type: "TABLE" },
@@ -36,6 +37,7 @@ vi.mock("@/stores/connectionStore", () => ({
getConfig: (id: string) => { getConfig: (id: string) => {
if (id === "connection-1") return { id, name: "SQLite", db_type: "sqlite" }; if (id === "connection-1") return { id, name: "SQLite", db_type: "sqlite" };
if (id === "postgres-1") return { id, name: "PostgreSQL", db_type: "postgres" }; 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; return undefined;
}, },
ensureConnected: mocks.ensureConnected, ensureConnected: mocks.ensureConnected,
@@ -49,6 +51,7 @@ vi.mock("@/stores/settingsStore", () => ({
})); }));
vi.mock("@/lib/backend/api", () => ({ vi.mock("@/lib/backend/api", () => ({
listSchemas: mocks.listSchemas,
listTables: mocks.listTables, listTables: mocks.listTables,
getColumns: mocks.getColumns, getColumns: mocks.getColumns,
listDataTypes: mocks.listDataTypes, listDataTypes: mocks.listDataTypes,
@@ -146,12 +149,13 @@ vi.mock("@/components/ui/searchable-select", async () => {
disabled: Boolean, disabled: Boolean,
}, },
emits: ["update:modelValue"], emits: ["update:modelValue"],
setup(props, { emit }) { setup(props, { attrs, emit }) {
return () => return () =>
h( h(
"select", "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, value: props.modelValue,
disabled: props.disabled, disabled: props.disabled,
onChange: (event: Event) => emit("update:modelValue", (event.target as HTMLSelectElement).value), onChange: (event: Event) => emit("update:modelValue", (event.target as HTMLSelectElement).value),
@@ -228,12 +232,24 @@ async function selectExistingTarget(tableName: string) {
await nextTick(); await nextTick();
} }
async function selectSchemaTarget(schemaName: string) {
const schemaSelect = document.body.querySelector<HTMLSelectElement>("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) { function buttonContaining(text: string) {
return [...document.body.querySelectorAll<HTMLButtonElement>("button")].find((button) => button.textContent?.includes(text)); return [...document.body.querySelectorAll<HTMLButtonElement>("button")].find((button) => button.textContent?.includes(text));
} }
beforeEach(() => { beforeEach(() => {
mocks.ensureConnected.mockReset().mockResolvedValue(undefined); mocks.ensureConnected.mockReset().mockResolvedValue(undefined);
mocks.listSchemas.mockReset().mockResolvedValue(["dbo", "sales", "analytics"]);
mocks.listTables.mockReset().mockResolvedValue([ mocks.listTables.mockReset().mockResolvedValue([
{ name: "existing_target", table_type: "TABLE" }, { name: "existing_target", table_type: "TABLE" },
{ name: "archived_target", table_type: "TABLE" }, { name: "archived_target", table_type: "TABLE" },
@@ -452,4 +468,114 @@ describe("TableImportDialog existing targets", () => {
expect(updateOption?.disabled).toBe(true); expect(updateOption?.disabled).toBe(true);
expect(document.body.textContent).toContain("requires primary-key metadata"); 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<HTMLSelectElement>("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<HTMLInputElement>("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");
});
}); });