1 parent
e9998e2806
commit
0a6ab8d360
28 files changed
+923
-85
No files matched your search
@@ -1,19 +1,29 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from "vue";
|
||||
import { computed, ref } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import type { SqlExportOptions, SqlInsertMode } from "@/lib/export/sqlInsertMode";
|
||||
import type { SqlExportColumnSelection, SqlExportOptions, SqlInsertMode } from "@/lib/export/sqlInsertMode";
|
||||
|
||||
const { t } = useI18n();
|
||||
const open = defineModel<boolean>("open", { default: false });
|
||||
const props = defineProps<{ allowSplit?: boolean }>();
|
||||
const props = defineProps<{ allowSplit?: boolean; columns?: SqlExportColumnSelection[] }>();
|
||||
const selected = ref<SqlInsertMode>("batch");
|
||||
const selectedColumnIndexes = ref(props.columns?.map((column) => column.sourceIndex) ?? []);
|
||||
const splitSqlOutput = ref(false);
|
||||
const splitSqlPartMaxMb = ref(100);
|
||||
const MIN_SPLIT_SQL_PART_MB = 1;
|
||||
const MAX_SPLIT_SQL_PART_MB = 4096;
|
||||
let outcomeEmitted = false;
|
||||
const columnSelectionEnabled = computed(() => props.columns !== undefined);
|
||||
const selectedColumnSet = computed(() => new Set(selectedColumnIndexes.value));
|
||||
const selectedColumns = computed(() => props.columns?.filter((column) => selectedColumnSet.value.has(column.sourceIndex)) ?? []);
|
||||
const canConfirm = computed(() => !columnSelectionEnabled.value || selectedColumns.value.length > 0);
|
||||
const duplicateColumnNames = computed(() => {
|
||||
const counts = new Map<string, number>();
|
||||
for (const column of props.columns ?? []) counts.set(column.name, (counts.get(column.name) ?? 0) + 1);
|
||||
return new Set([...counts].flatMap(([name, count]) => (count > 1 ? [name] : [])));
|
||||
});
|
||||
|
||||
const emit = defineEmits<{
|
||||
confirm: [options: SqlExportOptions];
|
||||
@@ -27,14 +37,24 @@ function normalizedSplitSqlPartMaxMb(): number {
|
||||
}
|
||||
|
||||
function onConfirm() {
|
||||
if (!canConfirm.value) return;
|
||||
outcomeEmitted = true;
|
||||
open.value = false;
|
||||
emit("confirm", {
|
||||
insertMode: selected.value,
|
||||
splitMaxMb: props.allowSplit && splitSqlOutput.value ? normalizedSplitSqlPartMaxMb() : undefined,
|
||||
...(columnSelectionEnabled.value ? { selectedColumns: selectedColumns.value } : {}),
|
||||
});
|
||||
}
|
||||
|
||||
function selectAllColumns() {
|
||||
selectedColumnIndexes.value = props.columns?.map((column) => column.sourceIndex) ?? [];
|
||||
}
|
||||
|
||||
function clearColumns() {
|
||||
selectedColumnIndexes.value = [];
|
||||
}
|
||||
|
||||
function onCancel() {
|
||||
if (outcomeEmitted) return;
|
||||
outcomeEmitted = true;
|
||||
@@ -69,6 +89,27 @@ function onOpenChange(value: boolean) {
|
||||
<span class="mt-1 block text-xs text-muted-foreground">{{ t("grid.sqlInsertModeSingleDescription") }}</span>
|
||||
</span>
|
||||
</label>
|
||||
<div v-if="columnSelectionEnabled" class="space-y-2 rounded-md border p-3" data-sql-export-columns>
|
||||
<div class="flex items-center justify-between gap-3">
|
||||
<div>
|
||||
<p class="text-sm font-medium">{{ t("databaseExport.sqlColumnSelection") }}</p>
|
||||
<p class="text-xs text-muted-foreground">{{ t("databaseExport.selectedColumns", { selected: selectedColumns.length, total: props.columns?.length ?? 0 }) }}</p>
|
||||
</div>
|
||||
<div class="flex gap-2">
|
||||
<Button type="button" variant="ghost" size="sm" data-sql-export-select-all @click="selectAllColumns">{{ t("databaseExport.selectAllColumns") }}</Button>
|
||||
<Button type="button" variant="ghost" size="sm" data-sql-export-clear @click="clearColumns">{{ t("databaseExport.clearColumns") }}</Button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="max-h-48 space-y-1 overflow-y-auto rounded border p-2">
|
||||
<label v-for="column in props.columns" :key="column.sourceIndex" class="flex cursor-pointer items-center gap-2 rounded px-2 py-1 text-sm hover:bg-accent/50">
|
||||
<input v-model="selectedColumnIndexes" type="checkbox" :value="column.sourceIndex" class="h-4 w-4" :data-sql-export-column="column.sourceIndex" />
|
||||
<span class="truncate"
|
||||
>{{ column.name }}<span v-if="duplicateColumnNames.has(column.name)" class="text-muted-foreground"> #{{ column.nameOccurrence + 1 }}</span></span
|
||||
>
|
||||
</label>
|
||||
</div>
|
||||
<p v-if="!canConfirm" role="alert" class="text-xs text-destructive" data-sql-export-column-error>{{ t("databaseExport.selectAtLeastOneColumn") }}</p>
|
||||
</div>
|
||||
<div v-if="props.allowSplit" class="space-y-2 rounded-md border p-3">
|
||||
<label class="flex cursor-pointer items-center gap-2 text-sm">
|
||||
<input v-model="splitSqlOutput" type="checkbox" class="h-4 w-4" data-sql-split-output />
|
||||
@@ -85,7 +126,7 @@ function onOpenChange(value: boolean) {
|
||||
</div>
|
||||
<DialogFooter>
|
||||
<Button variant="outline" @click="onCancel">{{ t("common.cancel") }}</Button>
|
||||
<Button data-sql-insert-mode-confirm @click="onConfirm">{{ t("common.confirm") }}</Button>
|
||||
<Button :disabled="!canConfirm" data-sql-insert-mode-confirm @click="onConfirm">{{ t("common.confirm") }}</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
import { createApp, h, nextTick, type App, defineComponent } from "vue";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import i18n from "@/i18n";
|
||||
import { sqlExportColumnChoices } from "@/lib/export/sqlExportColumns";
|
||||
import type { SqlExportColumnSelection } from "@/lib/export/sqlInsertMode";
|
||||
|
||||
vi.mock("@/components/ui/dialog", async () => {
|
||||
const { defineComponent, h } = await import("vue");
|
||||
@@ -37,13 +39,13 @@ afterEach(() => {
|
||||
i18n.global.locale.value = "en";
|
||||
});
|
||||
|
||||
async function mountDialog(onConfirm = () => {}, onCancel = () => {}, allowSplit = false) {
|
||||
async function mountDialog(onConfirm = () => {}, onCancel = () => {}, allowSplit = false, columns?: SqlExportColumnSelection[]) {
|
||||
const container = document.createElement("div");
|
||||
document.body.appendChild(container);
|
||||
const app = createApp(
|
||||
defineComponent({
|
||||
setup() {
|
||||
return () => h(SqlInsertModeDialog, { open: true, allowSplit, onConfirm, onCancel });
|
||||
return () => h(SqlInsertModeDialog, { open: true, allowSplit, columns, onConfirm, onCancel });
|
||||
},
|
||||
}),
|
||||
);
|
||||
@@ -54,6 +56,37 @@ async function mountDialog(onConfirm = () => {}, onCancel = () => {}, allowSplit
|
||||
}
|
||||
|
||||
describe("SqlInsertModeDialog", () => {
|
||||
it("defaults to every column and allows deselecting one duplicate independently", async () => {
|
||||
const onConfirm = vi.fn();
|
||||
const choices = sqlExportColumnChoices(["id", "name", "id"]);
|
||||
await mountDialog(onConfirm, () => {}, false, choices);
|
||||
expect([...document.querySelectorAll<HTMLInputElement>("[data-sql-export-column]")].every((input) => input.checked)).toBe(true);
|
||||
expect(document.body.textContent).toContain("#1");
|
||||
expect(document.body.textContent).toContain("#2");
|
||||
document.querySelector<HTMLInputElement>('[data-sql-export-column="0"]')?.click();
|
||||
await nextTick();
|
||||
document.querySelector<HTMLButtonElement>("[data-sql-insert-mode-confirm]")?.click();
|
||||
expect(onConfirm).toHaveBeenCalledWith({ insertMode: "batch", splitMaxMb: undefined, selectedColumns: choices.slice(1) });
|
||||
});
|
||||
|
||||
it("blocks an empty selection and restores every column with select all", async () => {
|
||||
const onConfirm = vi.fn();
|
||||
const choices = sqlExportColumnChoices(["id", "name"]);
|
||||
await mountDialog(onConfirm, () => {}, true, choices);
|
||||
document.querySelector<HTMLButtonElement>("[data-sql-export-clear]")?.click();
|
||||
await nextTick();
|
||||
const confirm = document.querySelector<HTMLButtonElement>("[data-sql-insert-mode-confirm]");
|
||||
expect(confirm?.disabled).toBe(true);
|
||||
expect(document.querySelector("[data-sql-export-column-error]")).not.toBeNull();
|
||||
confirm?.click();
|
||||
expect(onConfirm).not.toHaveBeenCalled();
|
||||
document.querySelector<HTMLButtonElement>("[data-sql-export-select-all]")?.click();
|
||||
await nextTick();
|
||||
expect(confirm?.disabled).toBe(false);
|
||||
confirm?.click();
|
||||
expect(onConfirm).toHaveBeenCalledWith({ insertMode: "batch", splitMaxMb: undefined, selectedColumns: choices });
|
||||
});
|
||||
|
||||
it("defaults to batch mode and emits the selected single-row mode", async () => {
|
||||
const onConfirm = vi.fn();
|
||||
await mountDialog(onConfirm);
|
||||
|
||||
Reference in new issue
Block a user