feat(export): support column selection in SQL exports

Closes #9475
This commit is contained in:
t8y2 committed 2026-09-27 22:37:50 +00:00
1 parent e9998e2806
commit 0a6ab8d360
28 files changed
+923 -85

No files matched your search

+3 -1
View File
@@ -51,7 +51,7 @@ import { decodeMeilisearchDocumentPage, decodeMeilisearchSearchResult, type Meil
import type { XuguTablespaceInfo } from "@/types/database";
import type { CreatedKey, EnqueuedTaskSummary, KeyCreateInput, KeyListItem, KeyPage, KeyUpdateInput, MeilisearchCreateIndexInput, MeilisearchSystemOverview, MeilisearchTask, TaskListInput, TaskPage, TaskSelector } from "@/types/meilisearchManagement";
import type { CsvQuoteMode } from "@/lib/export/csvQuoteMode";
import type { SqlInsertDialect, SqlInsertMode } from "@/lib/export/sqlInsertMode";
import type { SqlExportColumnSelection, SqlInsertDialect, SqlInsertMode } from "@/lib/export/sqlInsertMode";
/** Normalize Tauri rejections once at the public backend boundary. */
async function invokeBackend<T>(command: string, args?: Record<string, unknown>): Promise<T> {
@@ -5792,6 +5792,7 @@ export interface TableExportRequest {
insertDialect?: SqlInsertDialect;
csvQuoteMode?: CsvQuoteMode;
columns?: string[];
selectedColumns?: SqlExportColumnSelection[];
columnTypes?: Array<string | null | undefined>;
/** 与 `columns` 对齐的列 EXTRA 元数据(identity 等),用于 SQL INSERT 导出的 `SET IDENTITY_INSERT`。 */
columnExtras?: Array<string | null | undefined>;
@@ -5857,6 +5858,7 @@ export interface QueryResultExportRequest {
dateTimeFormat?: string;
exportTableName?: string;
exportColumnTypes?: Array<string | null | undefined>;
selectedColumns?: SqlExportColumnSelection[];
/**
* 结果列对应的原表 EXTRA 元数据(identity 等)。后端据此为 SQL INSERT 导出
* 补上 `SET IDENTITY_INSERT` 包裹,缺省表示未知。
@@ -0,0 +1,31 @@
import { describe, expect, it } from "vitest";
import { resolveSqlExportColumnIndexes, sqlExportColumnChoices } from "@/lib/export/sqlExportColumns";
describe("SQL export column identities", () => {
it("retains source positions and distinguishes duplicate labels", () => {
expect(sqlExportColumnChoices(["id", undefined, "name", "id"])).toEqual([
{ sourceIndex: 0, name: "id", nameOccurrence: 0 },
{ sourceIndex: 2, name: "name", nameOccurrence: 0 },
{ sourceIndex: 3, name: "id", nameOccurrence: 1 },
]);
});
it("resolves duplicate occurrences after columns are reordered or internal keys removed", () => {
const choices = sqlExportColumnChoices(["__DBX_ROWID", "id", "name", "id"]);
expect(resolveSqlExportColumnIndexes(["id", "id", "name"], [choices[3]!, choices[2]!])).toEqual([1, 2]);
});
it("preserves omitted selection and rejects empty, missing and repeated selections", () => {
const choices = sqlExportColumnChoices(["id", "id"]);
expect(resolveSqlExportColumnIndexes(["id", "name"])).toEqual([0, 1]);
expect(() => resolveSqlExportColumnIndexes(["id"], [])).toThrow("at least one");
expect(() => resolveSqlExportColumnIndexes(["id"], [choices[1]!])).toThrow("not found");
expect(() => resolveSqlExportColumnIndexes(["id"], [choices[0]!, choices[0]!])).toThrow("more than once");
});
it("handles wide projections without collapsing equal labels", () => {
const columns = Array.from({ length: 10000 }, () => "value");
const choices = sqlExportColumnChoices(columns);
expect(resolveSqlExportColumnIndexes(columns, choices)).toEqual(columns.map((_, index) => index));
});
});
@@ -0,0 +1,31 @@
import type { SqlExportColumnSelection } from "@/lib/export/sqlInsertMode";
export function sqlExportColumnChoices(columns: readonly (string | undefined)[]): SqlExportColumnSelection[] {
const occurrences = new Map<string, number>();
return columns.flatMap((name, sourceIndex) => {
if (!name) return [];
const nameOccurrence = occurrences.get(name) ?? 0;
occurrences.set(name, nameOccurrence + 1);
return [{ sourceIndex, name, nameOccurrence }];
});
}
export function resolveSqlExportColumnIndexes(columns: readonly (string | undefined)[], selectedColumns?: readonly SqlExportColumnSelection[]): number[] {
if (selectedColumns === undefined) return columns.map((_, index) => index);
if (!selectedColumns.length) throw new Error("Select at least one column for SQL export.");
const indexesByName = new Map<string, number[]>();
columns.forEach((name, index) => {
if (!name) return;
const indexes = indexesByName.get(name) ?? [];
indexes.push(index);
indexesByName.set(name, indexes);
});
const used = new Set<number>();
return selectedColumns.map((column) => {
const index = indexesByName.get(column.name)?.[column.nameOccurrence];
if (index === undefined) throw new Error(`Selected SQL export column was not found: ${column.name}`);
if (used.has(index)) throw new Error(`SQL export column was selected more than once: ${column.name}`);
used.add(index);
return index;
});
}
+15 -2
View File
@@ -5,16 +5,28 @@ import i18n from "@/i18n";
export type SqlInsertMode = "batch" | "single";
export type SqlInsertDialect = "source" | "standard";
export interface SqlExportColumnSelection {
sourceIndex: number;
name: string;
nameOccurrence: number;
}
export interface SqlExportOptions {
insertMode: SqlInsertMode;
splitMaxMb?: number;
selectedColumns?: SqlExportColumnSelection[];
}
export const DEFAULT_SQL_INSERT_MODE: SqlInsertMode = "batch";
export const DEFAULT_SQL_INSERT_DIALECT: SqlInsertDialect = "source";
export function showSqlInsertModeDialog(options: { allowSplit?: boolean } = {}): Promise<SqlExportOptions | null> {
if (typeof document === "undefined") return Promise.resolve({ insertMode: DEFAULT_SQL_INSERT_MODE });
export function showSqlInsertModeDialog(options: { allowSplit?: boolean; columns?: SqlExportColumnSelection[] } = {}): Promise<SqlExportOptions | null> {
if (typeof document === "undefined") {
return Promise.resolve({
insertMode: DEFAULT_SQL_INSERT_MODE,
...(options.columns ? { selectedColumns: [...options.columns] } : {}),
});
}
return new Promise((resolve) => {
const container = document.createElement("div");
@@ -31,6 +43,7 @@ export function showSqlInsertModeDialog(options: { allowSplit?: boolean } = {}):
app = createApp(SqlInsertModeDialog, {
open: true,
allowSplit: options.allowSplit === true,
columns: options.columns,
onConfirm: (options: SqlExportOptions) => finish(options),
onCancel: () => finish(null),
});