feat(import): update rows on primary-key conflicts

Closes #10014
This commit is contained in:
t8y2 committed 2026-09-27 05:54:32 +00:00
1 parent d5dcdabd22
commit 5f8bcf74a5
12 files changed
+758 -110

No files matched your search

@@ -140,7 +140,7 @@ const titleRow = ref(1);
const dataStartRow = ref(2);
const lastDataRow = ref(0);
const trimValues = ref(false);
const skipDuplicateRows = ref(false);
const conflictPolicy = ref<api.TableImportConflictPolicy>("error");
const emptyStringAsNull = ref(defaultTableImportEmptyStringAsNull(sourceFormat.value));
const selectedSheet = ref("");
const jsonShape = ref<api.TableImportJsonShape>("auto");
@@ -176,10 +176,10 @@ const wizardSteps: Array<{ value: TableImportWizardStep; labelKey: string }> = [
const selectedConnection = computed(() => (props.prefillConnectionId ? store.getConfig(props.prefillConnectionId) : undefined));
const structureDatabaseType = computed(() => tableStructureDatabaseTypeForConnection(selectedConnection.value));
// Mirrors the dialect list in skip-generator dispatch (transfer.rs): only these
// engines turn the skip-duplicates option into conflict-handling INSERTs.
const SKIP_DUPLICATE_ROWS_DATABASE_TYPES = new Set<DatabaseType>(["postgres", "kingbase", "opengauss", "sqlite", "cloudflare-d1", "duckdb", "mysql", "doris", "starrocks"]);
const supportsSkipDuplicateRows = computed(() => structureDatabaseType.value !== undefined && SKIP_DUPLICATE_ROWS_DATABASE_TYPES.has(structureDatabaseType.value));
// Mirrors the conflict SQL dispatch in transfer.rs. Other dialects must keep
// 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 supportsImportConflictPolicy = computed(() => structureDatabaseType.value !== undefined && IMPORT_CONFLICT_DATABASE_TYPES.has(structureDatabaseType.value));
const targetSchema = computed(() => 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);
@@ -202,6 +202,20 @@ const mappedColumns = computed<api.TableImportColumnMapping[]>(() => {
});
const mappedCount = computed(() => mappedColumns.value.length);
const mappingValidation = computed(() => validateImportMappings(mappedColumns.value));
const primaryKeyColumns = computed(() => targetColumns.value.filter((column) => column.is_primary_key));
const updateExistingUnavailableReason = computed(() => {
if (targetMode.value !== "existing") return t("tableImport.updateExistingRequiresExistingTable");
if (!supportsImportConflictPolicy.value) return t("tableImport.updateExistingUnsupportedDialect");
if (!existingTargetMetadataReady.value || primaryKeyColumns.value.length === 0) return t("tableImport.updateExistingRequiresPrimaryKey");
const mappedTargets = mappedColumns.value.map((mapping) => mapping.targetColumn.toLowerCase());
const missingKeys = primaryKeyColumns.value.filter((column) => !mappedTargets.includes(column.name.toLowerCase()));
if (missingKeys.length > 0) return t("tableImport.updateExistingRequiresMappedPrimaryKey", { columns: missingKeys.map((column) => column.name).join(", ") });
const primaryKeyNames = primaryKeyColumns.value.map((column) => column.name.toLowerCase());
if (!mappedTargets.some((column) => !primaryKeyNames.includes(column))) return t("tableImport.updateExistingRequiresMappedValue");
return "";
});
const canUpdateExistingRows = computed(() => updateExistingUnavailableReason.value === "");
const conflictPolicyError = computed(() => (conflictPolicy.value === "updateExisting" ? updateExistingUnavailableReason.value : ""));
const requiredUnmappedColumns = computed(() =>
requiredImportTargetColumns(
targetColumns.value,
@@ -215,7 +229,7 @@ const batchTargetNamesValid = computed(() => {
return tableNames.length > 0 && tableNames.every(Boolean) && new Set(tableNames).size === tableNames.length;
});
const canImport = computed(() => {
if (running.value || !props.prefillConnectionId || !existingTargetMetadataReady.value) return false;
if (running.value || !props.prefillConnectionId || !existingTargetMetadataReady.value || conflictPolicyError.value) return false;
if (!isBatchImport.value) return !!preview.value && !!targetTableName.value && mappingValidation.value.valid;
if (!batchTargetNamesValid.value) return false;
return selectedBatchTasks.value.every((task) => {
@@ -231,7 +245,7 @@ const canGoNext = computed(() => {
if (wizardStep.value === "mapping") return canImport.value;
}
if (wizardStep.value === "options") return !!preview.value && !!targetTableName.value && existingTargetMetadataReady.value;
if (wizardStep.value === "mapping") return existingTargetMetadataReady.value && mappingValidation.value.valid;
if (wizardStep.value === "mapping") return existingTargetMetadataReady.value && mappingValidation.value.valid && !conflictPolicyError.value;
return false;
});
const rawProgressPercent = computed(() => tableImportProgressPercent(progress.value));
@@ -384,7 +398,7 @@ function resetState() {
dataStartRow.value = 2;
lastDataRow.value = 0;
trimValues.value = false;
skipDuplicateRows.value = false;
conflictPolicy.value = "error";
emptyStringAsNull.value = defaultTableImportEmptyStringAsNull(sourceFormat.value);
selectedSheet.value = "";
jsonShape.value = "auto";
@@ -851,7 +865,7 @@ function canOpenStep(step: TableImportWizardStep) {
if (step === "review") return canImport.value;
}
if (step === "mapping") return !!preview.value && existingTargetMetadataReady.value;
if (step === "review") return !!preview.value && existingTargetMetadataReady.value && mappingValidation.value.valid;
if (step === "review") return !!preview.value && existingTargetMetadataReady.value && mappingValidation.value.valid && !conflictPolicyError.value;
return false;
}
@@ -941,7 +955,8 @@ async function startImport() {
mode: targetMode.value === "create" ? "append" : importMode.value,
createTable: targetMode.value === "create",
batchSize: Math.max(1, Number(batchSize.value) || 500),
skipDuplicateRows: supportsSkipDuplicateRows.value && skipDuplicateRows.value,
conflictPolicy: conflictPolicy.value,
skipDuplicateRows: conflictPolicy.value === "skip",
dateTimeFormat: settingsStore.editorSettings.globalDateTimeImportFormat || undefined,
preparedSource: preparedImportSource(currentPreview),
},
@@ -1031,7 +1046,8 @@ async function startBatchImport() {
mode: "append",
createTable: true,
batchSize: Math.max(1, Number(batchSize.value) || 500),
skipDuplicateRows: supportsSkipDuplicateRows.value && skipDuplicateRows.value,
conflictPolicy: conflictPolicy.value,
skipDuplicateRows: conflictPolicy.value === "skip",
dateTimeFormat: settingsStore.editorSettings.globalDateTimeImportFormat || undefined,
preparedSource: preparedImportSource(task.preview),
retainSource: true,
@@ -1198,13 +1214,17 @@ watch(targetMode, (mode) => {
loadedTargetTableName.value = "";
loadingTarget.value = false;
importMode.value = "append";
conflictPolicy.value = "error";
applyAutoMapping();
applySuggestedColumnDataTypes();
void loadDataTypeOptions();
}
});
watch(selectedExistingTable, () => {
if (targetMode.value === "existing") void loadTargetColumns();
if (targetMode.value === "existing") {
conflictPolicy.value = "error";
void loadTargetColumns();
}
});
watch(rawProgressPercent, (percent) => {
@@ -1422,10 +1442,6 @@ watch(rawProgressPercent, (percent) => {
<input v-model="emptyStringAsNull" type="checkbox" class="h-3.5 w-3.5 accent-primary" />
{{ t("tableImport.emptyStringAsNull") }}
</label>
<label v-if="supportsSkipDuplicateRows" class="flex items-center gap-2 text-xs">
<input v-model="skipDuplicateRows" type="checkbox" class="h-3.5 w-3.5 accent-primary" />
{{ t("tableImport.skipDuplicateRows") }}
</label>
</div>
<div v-else-if="sourceFormat === 'sql'" class="grid grid-cols-5 gap-3 rounded-md border p-3">
@@ -1490,6 +1506,20 @@ watch(rawProgressPercent, (percent) => {
</div>
</div>
<div v-if="supportsImportConflictPolicy" class="space-y-1.5 rounded-md border p-3">
<Label for="table-import-conflict-policy" class="text-xs">{{ t("tableImport.conflictPolicy") }}</Label>
<select id="table-import-conflict-policy" v-model="conflictPolicy" data-testid="table-import-conflict-policy" class="flex h-8 w-full rounded-md border border-input bg-background px-2 text-xs">
<option value="error">{{ t("tableImport.conflictError") }}</option>
<option value="skip">{{ t("tableImport.skipDuplicateRows") }}</option>
<option v-if="targetMode === 'existing'" value="updateExisting" :disabled="!canUpdateExistingRows">
{{ t("tableImport.updateExistingRows") }}
</option>
</select>
<p v-if="targetMode === 'existing' && !canUpdateExistingRows" class="text-[11px] text-muted-foreground">
{{ updateExistingUnavailableReason }}
</p>
</div>
<div class="flex items-center gap-2">
<Button size="sm" :disabled="!selectedSource || loadingPreview" @click="loadPreview()">
<Loader2 v-if="loadingPreview" class="mr-1.5 h-3.5 w-3.5 animate-spin" />
@@ -1597,6 +1627,9 @@ watch(rawProgressPercent, (percent) => {
<div v-if="mappingValidation.errors.length" class="rounded-md border border-destructive/30 bg-destructive/10 px-3 py-2 text-xs text-destructive">
{{ mappingValidation.errors.join("; ") }}
</div>
<div v-else-if="conflictPolicyError" class="rounded-md border border-destructive/30 bg-destructive/10 px-3 py-2 text-xs text-destructive">
{{ conflictPolicyError }}
</div>
<div v-else-if="requiredUnmappedColumns.length" class="flex items-start gap-2 rounded-md border border-amber-300 bg-amber-50 px-3 py-2 text-xs text-amber-800 dark:bg-amber-950/20 dark:text-amber-300">
<AlertTriangle class="mt-0.5 h-3.5 w-3.5 shrink-0" />
<span>{{ t("tableImport.requiredUnmapped", { columns: requiredUnmappedColumns.join(", ") }) }}</span>
@@ -294,6 +294,7 @@ describe("TableImportDialog batch selection", () => {
await flushAsyncUpdates();
expect(document.body.textContent).toContain("Trim values");
expect(document.body.textContent).not.toContain("Skip duplicate rows");
expect(document.body.textContent).not.toContain("Update the existing row");
});
it("offers the skip-duplicates option for dialects with conflict handling", async () => {
@@ -304,6 +305,25 @@ describe("TableImportDialog batch selection", () => {
expect(document.body.textContent).toContain("Skip duplicate rows");
});
it("serializes the explicit skip policy with the legacy compatibility flag", async () => {
mocks.previewTableImportFile.mockImplementation(() => Promise.resolve(delimitedPreview()));
await mountDialog([new File(["id,name\n1,a"], "rows.csv")]);
const policySelect = document.body.querySelector<HTMLSelectElement>('[data-testid="table-import-conflict-policy"]');
expect(policySelect).toBeTruthy();
if (policySelect) {
policySelect.value = "skip";
policySelect.dispatchEvent(new Event("change", { bubbles: true }));
}
await flushAsyncUpdates();
await startImport();
expect(mocks.importTableFile).toHaveBeenCalledTimes(1);
expect(mocks.importTableFile.mock.calls[0]![0]).toMatchObject({
conflictPolicy: "skip",
skipDuplicateRows: true,
});
});
it("keeps every worksheet selected by default and imports them in workbook order", async () => {
await mountDialog();
for (const name of ["rows_First", "rows_Second", "rows_Third"]) expect(taskCheckbox(name).checked).toBe(true);
@@ -3,6 +3,7 @@
import { createApp, defineComponent, h, nextTick, type App } from "vue";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import i18n from "@/i18n";
import type { TableImportProgress, TableImportRequest, TableImportSummary } from "@/lib/backend/api";
const mocks = vi.hoisted(() => ({
ensureConnected: vi.fn().mockResolvedValue(undefined),
@@ -11,8 +12,8 @@ const mocks = vi.hoisted(() => ({
{ name: "archived_target", table_type: "TABLE" },
]),
getColumns: vi.fn().mockResolvedValue([
{ name: "id", data_type: "INTEGER", nullable: false },
{ name: "name", data_type: "TEXT", nullable: true },
{ name: "id", data_type: "INTEGER", nullable: false, is_primary_key: true },
{ name: "name", data_type: "TEXT", nullable: true, is_primary_key: false },
]),
listDataTypes: vi.fn().mockResolvedValue(["INTEGER", "TEXT"]),
previewTableImportFile: vi.fn().mockResolvedValue({
@@ -26,6 +27,7 @@ const mocks = vi.hoisted(() => ({
sourceFingerprint: "rows-xlsx",
sheets: ["Data"],
}),
importTableFile: vi.fn<(request: TableImportRequest, onProgress: (progress: TableImportProgress) => void) => Promise<TableImportSummary>>(),
releaseTableImportSource: vi.fn().mockResolvedValue(undefined),
}));
@@ -52,7 +54,7 @@ vi.mock("@/lib/backend/api", () => ({
listDataTypes: mocks.listDataTypes,
previewTableImportFile: mocks.previewTableImportFile,
releaseTableImportSource: mocks.releaseTableImportSource,
importTableFile: vi.fn(),
importTableFile: mocks.importTableFile,
cancelTableImport: vi.fn(),
}));
@@ -208,7 +210,12 @@ async function selectWorkbook() {
value: [new File(["test workbook"], "rows.xlsx", { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" })],
});
fileInput?.dispatchEvent(new Event("change", { bubbles: true }));
await vi.waitFor(() => expect(mocks.previewTableImportFile).toHaveBeenCalled());
await flushAsyncUpdates();
if (!mocks.previewTableImportFile.mock.calls.length) {
buttonContaining("Load Preview")?.click();
}
await vi.waitFor(() => expect(mocks.previewTableImportFile).toHaveBeenCalled(), { timeout: 3000 });
await flushAsyncUpdates();
}
async function selectExistingTarget(tableName: string) {
@@ -232,11 +239,15 @@ beforeEach(() => {
{ name: "archived_target", table_type: "TABLE" },
]);
mocks.getColumns.mockReset().mockResolvedValue([
{ name: "id", data_type: "INTEGER", nullable: false },
{ name: "name", data_type: "TEXT", nullable: true },
{ name: "id", data_type: "INTEGER", nullable: false, is_primary_key: true },
{ name: "name", data_type: "TEXT", nullable: true, is_primary_key: false },
]);
mocks.listDataTypes.mockClear();
mocks.previewTableImportFile.mockClear();
mocks.importTableFile.mockReset().mockImplementation(async (request, onProgress) => {
onProgress({ importId: request.importId, status: "done", rowsImported: 1, totalRows: 1, elapsedMs: 1 });
return { importId: request.importId, rowsImported: 1, totalRows: 1, elapsedMs: 1 };
});
mocks.releaseTableImportSource.mockClear();
});
@@ -346,4 +357,60 @@ describe("TableImportDialog existing targets", () => {
expect(mocks.listTables).not.toHaveBeenCalled();
expect(mocks.getColumns).toHaveBeenCalledWith("connection-1", "main", "main", "existing_target");
});
it("offers update only with primary-key metadata and serializes the explicit policy", async () => {
i18n.global.locale.value = "en";
await mountDialog({ schema: "main" });
await selectWorkbook();
buttonContaining("Existing table")?.click();
await flushAsyncUpdates();
await selectExistingTarget("existing_target");
await vi.waitFor(() => expect(buttonContaining("Next")?.disabled).toBe(false));
const policySelect = document.body.querySelector<HTMLSelectElement>('[data-testid="table-import-conflict-policy"]');
expect(policySelect).toBeTruthy();
const updateOption = policySelect?.querySelector<HTMLOptionElement>('option[value="updateExisting"]');
await vi.waitFor(() => expect(updateOption?.disabled, document.body.textContent || "").toBe(false));
if (policySelect) {
policySelect.value = "updateExisting";
policySelect.dispatchEvent(new Event("change", { bubbles: true }));
}
await flushAsyncUpdates();
buttonContaining("Next")?.click();
await flushAsyncUpdates();
buttonContaining("Next")?.click();
await flushAsyncUpdates();
buttonContaining("Start Import")?.click();
await flushAsyncUpdates();
expect(mocks.importTableFile).toHaveBeenCalledTimes(1);
expect(mocks.importTableFile.mock.calls[0]![0]).toMatchObject({
conflictPolicy: "updateExisting",
skipDuplicateRows: false,
mappings: [
{ sourceColumn: "id", targetColumn: "id" },
{ sourceColumn: "name", targetColumn: "name" },
],
});
});
it("disables update when target primary-key metadata is absent", async () => {
mocks.getColumns.mockResolvedValueOnce([
{ name: "id", data_type: "INTEGER", nullable: false, is_primary_key: false },
{ name: "name", data_type: "TEXT", nullable: true, is_primary_key: false },
]);
i18n.global.locale.value = "en";
await mountDialog({ schema: "main" });
await selectWorkbook();
buttonContaining("Existing table")?.click();
await flushAsyncUpdates();
await selectExistingTarget("existing_target");
await vi.waitFor(() => expect(mocks.getColumns).toHaveBeenCalled());
await flushAsyncUpdates();
const updateOption = document.body.querySelector<HTMLOptionElement>('option[value="updateExisting"]');
expect(updateOption?.disabled).toBe(true);
expect(document.body.textContent).toContain("requires primary-key metadata");
});
});