feat(transfer): add tree-based object selection

This commit is contained in:
GIWTO authored and GitHub committed 2026-08-02 16:34:50 +08:00
1 parent fb7b2878fd
commit 473ef910b6
28 files changed
+5395 -205

No files matched your search

@@ -4,6 +4,7 @@ import { uuid } from "@/lib/common/utils";
import { useI18n } from "vue-i18n"; import { useI18n } from "vue-i18n";
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { buildTransferObjectSelections } from "./transferSelections";
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label"; import { Label } from "@/components/ui/label";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
@@ -13,7 +14,9 @@ import { useConnectionStore } from "@/stores/connectionStore";
import DatabaseIcon from "@/components/icons/DatabaseIcon.vue"; import DatabaseIcon from "@/components/icons/DatabaseIcon.vue";
import { connectionIconType } from "@/lib/connection/connectionPresentation"; import { connectionIconType } from "@/lib/connection/connectionPresentation";
import * as api from "@/lib/backend/api"; import * as api from "@/lib/backend/api";
import type { TransferMode, TransferTableNameCase } from "@/lib/backend/api"; import type { TransferContent, TransferMode, TransferObjectKind, TransferTableNameCase } from "@/lib/backend/api";
import { crossFamilyTransferableKinds, isSameTransferFamily, transferObjectKindsForDatabase } from "@/lib/database/transferObjectKinds";
import ObjectSelectionTree from "@/components/transfer/ObjectSelectionTree.vue";
import type { DatabaseType } from "@/types/database"; import type { DatabaseType } from "@/types/database";
import { isSchemaAware, supportsTransfer } from "@/lib/database/databaseCapabilities"; import { isSchemaAware, supportsTransfer } from "@/lib/database/databaseCapabilities";
import { isDorisFamilyCatalogCapable } from "@/lib/database/databaseFeatureSupport"; import { isDorisFamilyCatalogCapable } from "@/lib/database/databaseFeatureSupport";
@@ -21,7 +24,7 @@ import { isSameTransferDatabase, normalizeTransferCatalog } from "@/lib/database
import { databaseOptionsForConnection, fetchCatalogNamespaceOptions, fetchNamespaceOptionsForConnection, namespaceOptionsAreSchemas } from "@/composables/useDatabaseOptions"; import { databaseOptionsForConnection, fetchCatalogNamespaceOptions, fetchNamespaceOptionsForConnection, namespaceOptionsAreSchemas } from "@/composables/useDatabaseOptions";
import { useExportTracker } from "@/composables/useExportTracker"; import { useExportTracker } from "@/composables/useExportTracker";
import type { CatalogInfo } from "@/types/database"; import type { CatalogInfo } from "@/types/database";
import { ArrowRightLeft, ArrowLeftRight, Loader2, Square, CheckSquare } from "@lucide/vue"; import { ArrowRightLeft, ArrowLeftRight, Loader2 } from "@lucide/vue";
const { t } = useI18n(); const { t } = useI18n();
const { startDataTransferTask } = useExportTracker(); const { startDataTransferTask } = useExportTracker();
@@ -50,10 +53,73 @@ const sourceDatabase = ref("");
const sourceDatabases = ref<string[]>([]); const sourceDatabases = ref<string[]>([]);
const sourceSchemas = ref<string[]>([]); const sourceSchemas = ref<string[]>([]);
const sourceSchema = ref(""); const sourceSchema = ref("");
const sourceTables = ref<string[]>([]); const objectGroups = ref<Partial<Record<TransferObjectKind, string[]>>>({});
const selectedTables = ref<Set<string>>(new Set()); const selectedObjects = ref<Partial<Record<TransferObjectKind, Set<string>>>>({});
const tableSearch = ref(""); const objectSearch = ref("");
const loadingTables = ref(false); const loadingObjects = ref(false);
const transferContent = ref<TransferContent>("structureAndData");
const selectedTables = computed(() => new Set(selectedObjects.value.TABLE ?? []));
const OBJECT_KIND_LABEL_KEY: Record<TransferObjectKind, string> = {
TABLE: "objectTypeTable",
VIEW: "objectTypeView",
MATERIALIZED_VIEW: "objectTypeMaterializedView",
PROCEDURE: "objectTypeProcedure",
FUNCTION: "objectTypeFunction",
TRIGGER: "objectTypeTrigger",
SEQUENCE: "objectTypeSequence",
EVENT: "objectTypeEvent",
};
const treeSelection = computed<Record<string, string[]>>({
get: () => Object.fromEntries(Object.entries(selectedObjects.value).map(([k, v]) => [k, [...(v ?? [])]])),
set: (value) => {
const next: Partial<Record<TransferObjectKind, Set<string>>> = {};
for (const [k, names] of Object.entries(value)) {
if (names.length > 0) next[k as TransferObjectKind] = new Set(names);
}
selectedObjects.value = next;
},
});
const treeGroups = computed(() =>
(Object.keys(objectGroups.value) as TransferObjectKind[]).map((kind) => ({
kind,
label: t(`transfer.${OBJECT_KIND_LABEL_KEY[kind]}`),
items: objectGroups.value[kind] ?? [],
})),
);
const treeDisabledGroups = computed<TransferObjectKind[]>(() => {
const presentKinds = Object.keys(objectGroups.value) as TransferObjectKind[];
if (transferContent.value === "dataOnly") {
return presentKinds.filter((k) => k !== "TABLE");
}
const sourceConfig = store.getConfig(sourceConnectionId.value);
const targetConfig = store.getConfig(targetConnectionId.value);
const allowed = crossFamilyTransferableKinds(sourceConfig?.db_type, targetConfig?.db_type);
return presentKinds.filter((k) => k !== "TABLE" && !allowed.includes(k));
});
const treeDisabledHints = computed<Record<string, string>>(() => {
const hints: Record<string, string> = {};
const dataOnly = transferContent.value === "dataOnly";
for (const kind of treeDisabledGroups.value) {
hints[kind] = dataOnly ? t("transfer.objectDataOnlyDisabled") : t("transfer.objectCrossFamilyDisabled");
}
return hints;
});
const showCrossFamilyViewHint = computed(() => {
const sourceConfig = store.getConfig(sourceConnectionId.value);
const targetConfig = store.getConfig(targetConnectionId.value);
if (transferContent.value === "dataOnly") return false;
if (!sourceConfig || !targetConfig) return false;
const allowed = crossFamilyTransferableKinds(sourceConfig.db_type, targetConfig.db_type);
if (!allowed.includes("VIEW")) return false;
return !isSameTransferFamily(sourceConfig.db_type, targetConfig.db_type) && (selectedObjects.value.VIEW?.size ?? 0) > 0;
});
const pendingSourceSchemaPrefill = ref(""); const pendingSourceSchemaPrefill = ref("");
const pendingSelectedTablesPrefill = ref<string[] | null>(null); const pendingSelectedTablesPrefill = ref<string[] | null>(null);
@@ -68,7 +134,6 @@ const targetSchema = ref("");
const pendingTargetSchemaPrefill = ref(""); const pendingTargetSchemaPrefill = ref("");
// Options // Options
const createTable = ref(true);
const transferMode = ref<TransferMode>("append"); const transferMode = ref<TransferMode>("append");
const targetTableNameCase = ref<TransferTableNameCase>("preserve"); const targetTableNameCase = ref<TransferTableNameCase>("preserve");
const batchSize = ref(1000); const batchSize = ref(1000);
@@ -79,13 +144,6 @@ const ownershipTargetOwner = ref("");
const pendingOwnershipRequest = ref<api.TransferRequest | null>(null); const pendingOwnershipRequest = ref<api.TransferRequest | null>(null);
const pendingOwnershipRefresh = ref<{ shouldRefreshTargetTree: boolean } | null>(null); const pendingOwnershipRefresh = ref<{ shouldRefreshTargetTree: boolean } | null>(null);
const filteredTables = computed(() => {
const q = tableSearch.value.toLowerCase();
return q ? sourceTables.value.filter((t) => t.toLowerCase().includes(q)) : sourceTables.value;
});
const allSelected = computed(() => filteredTables.value.length > 0 && filteredTables.value.every((t) => selectedTables.value.has(t)));
function connectionType(id: string): DatabaseType | undefined { function connectionType(id: string): DatabaseType | undefined {
return store.connections.find((c) => c.id === id)?.db_type; return store.connections.find((c) => c.id === id)?.db_type;
} }
@@ -108,26 +166,17 @@ const canStart = computed(() => {
); );
const sameSourceAndTarget = sameCatalogAndDatabase && effectiveSourceSchema === effectiveTargetSchema; const sameSourceAndTarget = sameCatalogAndDatabase && effectiveSourceSchema === effectiveTargetSchema;
return ( return (
!!sourceConnectionId.value && !!sourceDatabase.value && !!targetConnectionId.value && !!targetDatabase.value && (sourceCatalogs.value.length <= 1 || !!sourceCatalog.value) && (targetCatalogs.value.length <= 1 || !!targetCatalog.value) && selectedTables.value.size > 0 && !sameSourceAndTarget !!sourceConnectionId.value &&
!!sourceDatabase.value &&
!!targetConnectionId.value &&
!!targetDatabase.value &&
(sourceCatalogs.value.length <= 1 || !!sourceCatalog.value) &&
(targetCatalogs.value.length <= 1 || !!targetCatalog.value) &&
(selectedTables.value.size > 0 || Object.values(selectedObjects.value).some((names) => names.size > 0)) &&
!sameSourceAndTarget
); );
}); });
function toggleSelectAll() {
if (allSelected.value) {
filteredTables.value.forEach((t) => selectedTables.value.delete(t));
} else {
filteredTables.value.forEach((t) => selectedTables.value.add(t));
}
}
function toggleTable(table: string) {
if (selectedTables.value.has(table)) {
selectedTables.value.delete(table);
} else {
selectedTables.value.add(table);
}
}
async function loadCatalogs(connectionId: string, side: "source" | "target") { async function loadCatalogs(connectionId: string, side: "source" | "target") {
if (!connectionId || !isCatalogCapable(connectionId)) { if (!connectionId || !isCatalogCapable(connectionId)) {
if (side === "source") { if (side === "source") {
@@ -234,19 +283,26 @@ async function loadSchemas(connectionId: string, database: string, side: "source
function applyPendingTableSelection() { function applyPendingTableSelection() {
const pending = pendingSelectedTablesPrefill.value; const pending = pendingSelectedTablesPrefill.value;
selectedTables.value = pending ? new Set(sourceTables.value.filter((table) => pending.includes(table))) : new Set(sourceTables.value); const tables = objectGroups.value.TABLE ?? [];
if (pending) {
const chosen = new Set(tables.filter((table) => pending.includes(table)));
if (chosen.size > 0) {
selectedObjects.value = { ...selectedObjects.value, TABLE: chosen };
}
}
pendingSelectedTablesPrefill.value = null; pendingSelectedTablesPrefill.value = null;
} }
async function loadTables() { async function loadObjects() {
if (!sourceConnectionId.value || !sourceDatabase.value) { if (!sourceConnectionId.value || !sourceDatabase.value) {
sourceTables.value = []; objectGroups.value = {};
return; return;
} }
loadingTables.value = true; loadingObjects.value = true;
try { try {
if (isMongoConnection(sourceConnectionId.value)) { if (isMongoConnection(sourceConnectionId.value)) {
sourceTables.value = (await api.mongoListCollections(sourceConnectionId.value, sourceDatabase.value)).map((c) => c.name); const collections = await api.mongoListCollections(sourceConnectionId.value, sourceDatabase.value);
objectGroups.value = { TABLE: collections.map((c) => c.name) };
applyPendingTableSelection(); applyPendingTableSelection();
return; return;
} }
@@ -254,13 +310,27 @@ async function loadTables() {
const needsSchema = isSchemaAware(config?.db_type); const needsSchema = isSchemaAware(config?.db_type);
const schema = needsSchema && sourceSchema.value ? sourceSchema.value : sourceDatabase.value; const schema = needsSchema && sourceSchema.value ? sourceSchema.value : sourceDatabase.value;
const catalog = sourceCatalog.value || undefined; const catalog = sourceCatalog.value || undefined;
const tables = await api.listTables(sourceConnectionId.value, sourceDatabase.value, schema, undefined, undefined, undefined, undefined, catalog); const kinds = transferObjectKindsForDatabase(config?.db_type);
sourceTables.value = tables.filter((t) => t.table_type === "TABLE" || t.table_type === "BASE TABLE").map((t) => t.name); const groups: Partial<Record<TransferObjectKind, string[]>> = {};
for (const kind of kinds) {
try {
if (kind === "TABLE") {
const tables = await api.listTables(sourceConnectionId.value, sourceDatabase.value, schema, undefined, undefined, undefined, undefined, catalog);
groups.TABLE = tables.filter((t) => t.table_type === "TABLE" || t.table_type === "BASE TABLE").map((t) => t.name);
} else {
const objects = await api.listObjects(sourceConnectionId.value, sourceDatabase.value, schema, [kind], undefined, undefined, undefined, catalog);
groups[kind] = objects.map((o) => o.name);
}
} catch {
groups[kind] = [];
}
}
objectGroups.value = groups;
applyPendingTableSelection(); applyPendingTableSelection();
} catch { } catch {
sourceTables.value = []; objectGroups.value = {};
} finally { } finally {
loadingTables.value = false; loadingObjects.value = false;
} }
} }
@@ -275,8 +345,8 @@ watch(sourceConnectionId, async (id) => {
sourceCatalog.value = ""; sourceCatalog.value = "";
sourceCatalogs.value = []; sourceCatalogs.value = [];
sourceDatabase.value = ""; sourceDatabase.value = "";
sourceTables.value = []; objectGroups.value = {};
selectedTables.value.clear(); selectedObjects.value = {};
pendingSourceSchemaPrefill.value = ""; pendingSourceSchemaPrefill.value = "";
pendingSelectedTablesPrefill.value = null; pendingSelectedTablesPrefill.value = null;
if (isCatalogCapable(id)) { if (isCatalogCapable(id)) {
@@ -292,8 +362,8 @@ watch(sourceConnectionId, async (id) => {
watch(sourceCatalog, async (catalog) => { watch(sourceCatalog, async (catalog) => {
if (!sourceConnectionId.value) return; if (!sourceConnectionId.value) return;
sourceDatabase.value = ""; sourceDatabase.value = "";
sourceTables.value = []; objectGroups.value = {};
selectedTables.value.clear(); selectedObjects.value = {};
if (catalog) { if (catalog) {
await loadDatabasesForCatalog(sourceConnectionId.value, catalog, "source"); await loadDatabasesForCatalog(sourceConnectionId.value, catalog, "source");
} }
@@ -316,7 +386,7 @@ watch(sourceDatabase, async (db) => {
} }
}); });
watch(sourceSchema, () => loadTables()); watch(sourceSchema, () => loadObjects());
watch(targetConnectionId, async (id) => { watch(targetConnectionId, async (id) => {
if (skipTargetWatch.value) { if (skipTargetWatch.value) {
@@ -414,11 +484,11 @@ function resetState() {
sourceDatabases.value = []; sourceDatabases.value = [];
sourceSchemas.value = []; sourceSchemas.value = [];
sourceSchema.value = ""; sourceSchema.value = "";
sourceTables.value = []; objectGroups.value = {};
selectedTables.value.clear(); selectedObjects.value = {};
pendingSourceSchemaPrefill.value = ""; pendingSourceSchemaPrefill.value = "";
pendingSelectedTablesPrefill.value = null; pendingSelectedTablesPrefill.value = null;
tableSearch.value = ""; objectSearch.value = "";
targetConnectionId.value = ""; targetConnectionId.value = "";
targetCatalog.value = ""; targetCatalog.value = "";
targetCatalogs.value = []; targetCatalogs.value = [];
@@ -427,7 +497,7 @@ function resetState() {
targetSchemas.value = []; targetSchemas.value = [];
targetSchema.value = ""; targetSchema.value = "";
pendingTargetSchemaPrefill.value = ""; pendingTargetSchemaPrefill.value = "";
createTable.value = true; transferContent.value = "structureAndData";
transferMode.value = "append"; transferMode.value = "append";
targetTableNameCase.value = "preserve"; targetTableNameCase.value = "preserve";
batchSize.value = 1000; batchSize.value = 1000;
@@ -448,7 +518,7 @@ async function startTransfer() {
const sourceDatabaseName = sourceDatabase.value; const sourceDatabaseName = sourceDatabase.value;
const targetConnection = targetConnectionId.value; const targetConnection = targetConnectionId.value;
const targetDatabaseName = targetDatabase.value; const targetDatabaseName = targetDatabase.value;
const shouldRefreshTargetTree = createTable.value; const shouldRefreshTargetTree = transferContent.value !== "dataOnly";
const request: api.TransferRequest = { const request: api.TransferRequest = {
transferId: uuid(), transferId: uuid(),
@@ -461,14 +531,16 @@ async function startTransfer() {
targetSchema: effectiveTargetSchema, targetSchema: effectiveTargetSchema,
targetCatalog: normalizeTransferCatalog(targetCatalog.value, targetCatalogs.value) || undefined, targetCatalog: normalizeTransferCatalog(targetCatalog.value, targetCatalogs.value) || undefined,
tables: [...selectedTables.value], tables: [...selectedTables.value],
createTable: createTable.value, createTable: transferContent.value !== "dataOnly",
content: transferContent.value,
objects: buildTransferObjectSelections(selectedObjects.value, treeDisabledGroups.value),
mode: transferMode.value, mode: transferMode.value,
targetTableNameCase: targetTableNameCase.value, targetTableNameCase: targetTableNameCase.value,
ownershipPolicy: "preserve", ownershipPolicy: "preserve",
batchSize: batchSize.value, batchSize: batchSize.value,
}; };
if (createTable.value) { if (transferContent.value !== "dataOnly") {
try { try {
const preview = await api.previewTransferOwnership(request); const preview = await api.previewTransferOwnership(request);
if (preview.missingOwners.length > 0) { if (preview.missingOwners.length > 0) {
@@ -535,8 +607,8 @@ function getConnectionName(id: string) {
</DialogTitle> </DialogTitle>
</DialogHeader> </DialogHeader>
<div class="min-h-0 flex-1 overflow-hidden"> <div class="min-h-0 flex-1 overflow-y-auto pr-1 scrollbar-thin">
<div class="grid h-full min-h-0 grid-rows-[auto_minmax(0,1fr)_auto] gap-4 py-3"> <div class="flex flex-col gap-5 py-3">
<!-- Source / Target Side by Side --> <!-- Source / Target Side by Side -->
<div class="grid grid-cols-[1fr_auto_1fr] gap-4 items-start"> <div class="grid grid-cols-[1fr_auto_1fr] gap-4 items-start">
<!-- Source Section --> <!-- Source Section -->
@@ -693,48 +765,47 @@ function getConnectionName(id: string) {
</div> </div>
</div> </div>
<!-- Tables Section --> <!-- Objects Section -->
<div class="flex min-h-0 flex-col gap-2"> <div class="flex min-h-0 flex-col gap-2">
<div class="flex items-center justify-between"> <div class="flex items-center justify-between">
<div class="text-xs font-medium text-muted-foreground uppercase tracking-wider"> <div class="text-xs font-medium text-muted-foreground uppercase tracking-wider">
{{ t("transfer.tables") }} {{ t("transfer.objects") }}
<span v-if="sourceTables.length" class="text-muted-foreground/60">({{ selectedTables.size }}/{{ sourceTables.length }})</span> <span v-if="objectGroups.TABLE?.length" class="text-muted-foreground/60">({{ selectedTables.size }}/{{ objectGroups.TABLE.length }})</span>
</div> </div>
<Button v-if="sourceTables.length" variant="ghost" size="sm" class="h-6 text-xs px-2" @click="toggleSelectAll">
{{ allSelected ? t("transfer.deselectAll") : t("transfer.selectAll") }}
</Button>
</div> </div>
<Input v-if="sourceTables.length > 5" v-model="tableSearch" :placeholder="t('transfer.searchTables')" class="h-7 text-xs" /> <div v-if="(!loadingObjects && !sourceConnectionId) || !sourceDatabase" class="text-xs text-muted-foreground py-4 text-center">
<div v-if="loadingTables" class="flex items-center gap-2 text-xs text-muted-foreground py-4 justify-center">
<Loader2 class="w-3.5 h-3.5 animate-spin" />
{{ t("common.loading") }}
</div>
<div v-else-if="!sourceConnectionId || !sourceDatabase" class="text-xs text-muted-foreground py-4 text-center">
{{ t("transfer.selectSourceFirst") }} {{ t("transfer.selectSourceFirst") }}
</div> </div>
<div v-else-if="sourceTables.length === 0" class="text-xs text-muted-foreground py-4 text-center"> <ObjectSelectionTree v-model="treeSelection" :groups="treeGroups" :disabled-groups="treeDisabledGroups" :disabled-hints="treeDisabledHints" v-model:search="objectSearch" :loading="loadingObjects" class="min-h-0 flex-1" />
{{ t("transfer.noTables") }} <div v-if="showCrossFamilyViewHint" class="mt-1.5 rounded-md border border-amber-300/40 bg-amber-50 px-2 py-1.5 text-xs text-amber-700">
</div> {{ t("transfer.crossFamilyViewHint") }}
<div v-else class="min-h-0 max-h-[200px] overflow-y-auto rounded-md border">
<div v-for="table in filteredTables" :key="table" class="flex items-center gap-2 px-2.5 py-1.5 hover:bg-muted/50 cursor-pointer text-xs" @click="toggleTable(table)">
<CheckSquare v-if="selectedTables.has(table)" class="w-3.5 h-3.5 text-primary shrink-0" />
<Square v-else class="w-3.5 h-3.5 text-muted-foreground/40 shrink-0" />
<span class="truncate">{{ table }}</span>
</div>
</div> </div>
</div> </div>
<!-- Options --> <!-- Options -->
<div class="space-y-2.5"> <div class="space-y-2.5">
<div class="flex items-center gap-2 cursor-pointer text-xs" @click="createTable = !createTable"> <div class="space-y-1">
<CheckSquare v-if="createTable" class="w-3.5 h-3.5 text-primary shrink-0" /> <Label class="text-xs">{{ t("transfer.content") }}</Label>
<Square v-else class="w-3.5 h-3.5 text-muted-foreground/40 shrink-0" /> <div class="flex flex-col gap-1">
{{ t("transfer.createTable") }} <label class="flex cursor-pointer items-center gap-2 text-xs">
<input type="radio" value="structureAndData" v-model="transferContent" class="h-3.5 w-3.5" />
{{ t("transfer.contentStructureAndData") }}
</label>
<label class="flex cursor-pointer items-center gap-2 text-xs">
<input type="radio" value="structureOnly" v-model="transferContent" class="h-3.5 w-3.5" />
{{ t("transfer.contentStructureOnly") }}
<span class="text-muted-foreground/70">{{ t("transfer.contentStructureOnlyHint") }}</span>
</label>
<label class="flex cursor-pointer items-center gap-2 text-xs">
<input type="radio" value="dataOnly" v-model="transferContent" class="h-3.5 w-3.5" />
{{ t("transfer.contentDataOnly") }}
<span class="text-muted-foreground/70">{{ t("transfer.contentDataOnlyHint") }}</span>
</label>
</div>
</div> </div>
<div class="flex items-center gap-3"> <div v-if="transferContent !== 'structureOnly'" class="flex items-center gap-3">
<Label class="text-xs shrink-0">{{ t("transfer.transferMode") }}</Label> <Label class="text-xs shrink-0">{{ t("transfer.dataWriteMode") }}</Label>
<Select v-model="transferMode"> <Select v-model="transferMode">
<SelectTrigger class="h-7 text-xs"> <SelectTrigger class="h-7 text-xs">
<SelectValue /> <SelectValue />
@@ -0,0 +1,236 @@
<script setup lang="ts">
import { computed, ref, watch } from "vue";
import { useI18n } from "vue-i18n";
import type { TransferObjectKind } from "@/lib/backend/api";
import { Square, CheckSquare, MinusSquare, Search, ChevronRight } from "@lucide/vue";
const { t } = useI18n();
export interface ObjectTreeGroup {
kind: TransferObjectKind;
label: string;
items: string[];
}
const props = defineProps<{
groups: ObjectTreeGroup[];
disabledGroups: TransferObjectKind[];
disabledHints: Record<string, string>;
modelValue: Record<string, string[]>;
search?: string;
loading?: boolean;
}>();
const emit = defineEmits<{
"update:modelValue": [value: Record<string, string[]>];
"update:search": [value: string];
}>();
const expanded = ref<Set<string>>(new Set(props.groups.map((g) => g.kind)));
// Groups may arrive after the tree mounts (async object loading); keep newly
// appearing groups expanded so search results are visible without a manual tap.
watch(
() => props.groups.map((g) => g.kind),
(kinds) => {
let changed = false;
const next = new Set(expanded.value);
for (const kind of kinds) {
if (!next.has(kind)) {
next.add(kind);
changed = true;
}
}
if (changed) expanded.value = next;
},
);
// Local search state: the input edits this ref directly and changes are
// forwarded up via update:search (v-model:search on the parent).
const localSearch = ref(props.search ?? "");
watch(
() => props.search,
(v) => {
if (v !== localSearch.value) localSearch.value = v ?? "";
},
);
watch(localSearch, (v) => {
if (v !== (props.search ?? "")) emit("update:search", v);
});
const searchQuery = computed(() => localSearch.value.trim().toLowerCase());
function toggleGroup(kind: string) {
const next = new Set(expanded.value);
if (next.has(kind)) {
next.delete(kind);
} else {
next.add(kind);
}
expanded.value = next;
}
function isGroupDisabled(kind: TransferObjectKind): boolean {
return props.disabledGroups.includes(kind);
}
function selectedNames(kind: string): string[] {
return props.modelValue[kind] ?? [];
}
function updateSelection(kind: string, names: string[]) {
const next: Record<string, string[]> = { ...props.modelValue };
if (names.length === 0) {
delete next[kind];
} else {
next[kind] = names;
}
emit("update:modelValue", next);
}
function toggleItem(kind: string, item: string) {
const current = selectedNames(kind);
const next = current.includes(item) ? current.filter((n) => n !== item) : [...current, item];
updateSelection(kind, next);
}
function toggleGroupAll(kind: TransferObjectKind, items: string[]) {
const current = selectedNames(kind);
const allVisibleSelected = items.length > 0 && items.every((n) => current.includes(n));
if (allVisibleSelected) {
// uncheck only the visible items, keep selections hidden by the search
updateSelection(
kind,
current.filter((n) => !items.includes(n)),
);
} else {
// check the visible items, merging with existing (possibly hidden) ones
updateSelection(kind, [...new Set([...current, ...items])]);
}
}
const filteredGroups = computed<ObjectTreeGroup[]>(() => {
const query = searchQuery.value;
if (!query) {
return props.groups;
}
return props.groups.map((g) => ({
...g,
items: g.items.filter((name) => name.toLowerCase().includes(query)),
}));
});
function selectAllEnabled() {
const next: Record<string, string[]> = { ...props.modelValue };
for (const group of filteredGroups.value) {
if (isGroupDisabled(group.kind)) continue;
next[group.kind] = [...new Set([...(next[group.kind] ?? []), ...group.items])];
}
emit("update:modelValue", next);
}
function deselectAll() {
// Clear only the visible, enabled selections; selections hidden by the
// current search and selections inside disabled groups are preserved so
// bulk actions never silently drop what the user cannot see.
const next: Record<string, string[]> = { ...props.modelValue };
for (const group of filteredGroups.value) {
if (isGroupDisabled(group.kind)) continue;
const kept = (next[group.kind] ?? []).filter((n) => !group.items.includes(n));
if (kept.length === 0) {
delete next[group.kind];
} else {
next[group.kind] = kept;
}
}
emit("update:modelValue", next);
}
// The bulk button flips between “select all” and “deselect all”. It shows
// “deselect all” only when every visible, enabled item is already selected
// (selecting more is then a no-op); otherwise “select all” is shown so the
// user can fill the remaining visible items. Groups with no visible items
// (fully filtered out by the search) and disabled groups are ignored.
const allVisibleEnabledSelected = computed(() => {
const visibleEnabled = filteredGroups.value.filter((g) => !isGroupDisabled(g.kind) && g.items.length > 0);
return visibleEnabled.length > 0 && visibleEnabled.every((g) => g.items.every((item) => (props.modelValue[g.kind] ?? []).includes(item)));
});
// Tri-state header checkbox: "all" when every visible item of the group is
// selected, "partial" when only some are, "none" otherwise. Mirrors
// toggleGroupAll, which only ever touches the visible items.
function groupSelectionState(kind: TransferObjectKind, items: string[]): "none" | "partial" | "all" {
if (items.length === 0) return "none";
const selected = props.modelValue[kind] ?? [];
const visibleSelected = items.filter((item) => selected.includes(item)).length;
if (visibleSelected === 0) return "none";
if (visibleSelected === items.length) return "all";
return "partial";
}
</script>
<template>
<div class="flex flex-col flex-1 min-h-0 gap-2.5">
<!-- Sticky top bar: search input and action buttons -->
<div class="flex items-center gap-2 shrink-0">
<div class="relative flex-1 min-w-0">
<Search class="absolute left-2.5 top-1/2 h-3.5 w-3.5 -translate-y-1/2 text-muted-foreground/60 pointer-events-none" />
<input
data-test="search"
:placeholder="t('transfer.searchObjects')"
v-model="localSearch"
autocapitalize="off"
autocomplete="off"
autocorrect="off"
spellcheck="false"
class="pl-8 pr-2.5 h-8 w-full rounded-md border border-input bg-transparent text-xs outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:border-ring transition-colors placeholder:text-muted-foreground/70"
/>
</div>
<Button v-if="allVisibleEnabledSelected" variant="outline" size="sm" @click="deselectAll">
{{ t("transfer.deselectAll") }}
</Button>
<Button v-else variant="outline" size="sm" @click="selectAllEnabled">
{{ t("transfer.selectAll") }}
</Button>
</div>
<div v-if="loading" class="py-4 text-center text-sm text-muted-foreground">
{{ t("common.loading") }}
</div>
<div v-else-if="groups.length === 0" class="py-4 text-center text-sm text-muted-foreground">
{{ t("transfer.noObjects") }}
</div>
<!-- Independent scroll area for tree groups -->
<div v-else class="flex-1 min-h-0 max-h-[240px] flex flex-col gap-1.5 overflow-y-auto rounded-md border border-border/80 bg-muted/10 p-1.5 scrollbar-thin scrollbar-thumb-muted-foreground/20">
<div v-for="group in filteredGroups" :key="group.kind" :data-test="`group-${group.kind}`" class="rounded-lg border border-border/60 bg-card/60 transition-all hover:bg-card flex flex-col p-1.5" :class="{ 'opacity-50 bg-muted/5 border-dashed border-muted': isGroupDisabled(group.kind) }">
<div class="flex items-center gap-2 px-1 py-0.5 select-none">
<button :data-test="'group-toggle'" :data-state="groupSelectionState(group.kind, group.items)" type="button" class="flex items-center gap-2 text-left shrink-0" :disabled="isGroupDisabled(group.kind)" @click="!isGroupDisabled(group.kind) && toggleGroupAll(group.kind, group.items)">
<CheckSquare v-if="groupSelectionState(group.kind, group.items) === 'all'" class="h-4 w-4 text-primary" />
<MinusSquare v-else-if="groupSelectionState(group.kind, group.items) === 'partial'" class="h-4 w-4 text-primary" />
<Square v-else class="h-4 w-4 text-muted-foreground" />
</button>
<span class="flex-1 text-xs font-semibold text-foreground/90 cursor-pointer" @click="toggleGroup(group.kind)">
{{ group.label }}
</span>
<span class="text-[10px] px-1 py-0.5 rounded-full bg-muted/60 text-muted-foreground font-mono shrink-0"> {{ (modelValue[group.kind] ?? []).length }}/{{ group.items.length }} </span>
<span v-if="disabledHints[group.kind]" class="rounded bg-amber-500/10 border border-amber-500/20 px-1.5 py-0.5 text-[10px] text-amber-700 font-medium shrink-0">
{{ disabledHints[group.kind] }}
</span>
<button type="button" class="text-muted-foreground hover:text-foreground shrink-0 p-0.5" @click="toggleGroup(group.kind)">
<ChevronRight class="h-3.5 w-3.5 text-muted-foreground/80 transition-transform duration-200" :class="{ 'rotate-90': expanded.has(group.kind) }" />
</button>
</div>
<div v-if="expanded.has(group.kind)" class="flex flex-col gap-0.5 pl-6 pr-1 mt-1">
<label v-for="item in group.items" :key="item" class="flex cursor-pointer items-center gap-2 rounded px-1.5 py-1 text-xs hover:bg-muted/70 transition-colors" :class="{ 'pointer-events-none opacity-50': isGroupDisabled(group.kind) }" :data-test="`item-${group.kind}-${item}`">
<input type="checkbox" class="h-3.5 w-3.5" :checked="(modelValue[group.kind] ?? []).includes(item)" :disabled="isGroupDisabled(group.kind)" @change="toggleItem(group.kind, item)" />
<span class="truncate text-foreground/80">{{ item }}</span>
</label>
<div v-if="group.items.length === 0" class="px-1 py-1 text-xs text-muted-foreground">无匹配</div>
</div>
</div>
</div>
</div>
</template>
@@ -7,17 +7,16 @@ describe("DataTransferDialog layout", () => {
it("keeps the header and footer outside the shrinking content region", () => { it("keeps the header and footer outside the shrinking content region", () => {
expect(dialogSource).toContain('<DialogHeader class="shrink-0">'); expect(dialogSource).toContain('<DialogHeader class="shrink-0">');
expect(dialogSource).toContain('<DialogFooter class="shrink-0">'); expect(dialogSource).toContain('<DialogFooter class="shrink-0">');
expect(dialogSource).toContain('class="min-h-0 flex-1 overflow-hidden"'); expect(dialogSource).toContain('class="min-h-0 flex-1 overflow-y-auto pr-1 scrollbar-thin"');
}); });
it("shrinks the table row on short viewports without adding dialog scrolling", () => { it("allows layout scrolling on short viewports to prevent content clipping", () => {
expect(dialogSource).toContain('class="grid h-full min-h-0 grid-rows-[auto_minmax(0,1fr)_auto] gap-4 py-3"'); expect(dialogSource).toContain('class="flex flex-col gap-5 py-3"');
expect(dialogSource).toContain('class="flex min-h-0 flex-col gap-2"'); expect(dialogSource).toContain('class="flex min-h-0 flex-col gap-2"');
expect(dialogSource).not.toContain('class="flex-1 min-h-0 overflow-auto"');
}); });
it("keeps long table lists independently scrollable", () => { it("keeps long table lists independently scrollable", () => {
expect(dialogSource).toContain('class="min-h-0 max-h-[200px] overflow-y-auto rounded-md border"'); expect(dialogSource).toContain('class="min-h-0 flex-1"');
}); });
}); });
@@ -32,7 +31,27 @@ describe("DataTransferDialog transfer prefill", () => {
it("keeps only copied tables selected after loading the source list", () => { it("keeps only copied tables selected after loading the source list", () => {
expect(dialogSource).toContain("function applyPendingTableSelection()"); expect(dialogSource).toContain("function applyPendingTableSelection()");
expect(dialogSource).toContain("new Set(sourceTables.value.filter((table) => pending.includes(table)))"); expect(dialogSource).toContain("new Set(tables.filter((table) => pending.includes(table)))");
});
it("sends content and objects in the transfer request", () => {
expect(dialogSource).toContain("buildTransferObjectSelections(selectedObjects.value, treeDisabledGroups.value)");
expect(dialogSource).toContain('import { buildTransferObjectSelections } from "./transferSelections"');
expect(dialogSource).toContain('createTable: transferContent.value !== "dataOnly"');
expect(dialogSource).toContain('createTable: transferContent.value !== "dataOnly"');
});
it("loads non-table object groups per source database kind", () => {
expect(dialogSource).toContain("transferObjectKindsForDatabase");
expect(dialogSource).toContain("api.listObjects(sourceConnectionId.value, sourceDatabase.value, schema, [kind]");
expect(dialogSource).toContain("groups[kind] = objects.map((o) => o.name)");
});
it("disables non-table groups for data-only and cross-family transfers", () => {
expect(dialogSource).toContain("treeDisabledGroups");
expect(dialogSource).toContain('transferContent.value === "dataOnly"');
expect(dialogSource).toContain("crossFamilyTransferableKinds");
expect(dialogSource).toContain("objectDataOnlyDisabled");
}); });
it("allows a transfer between different schemas in the same database", () => { it("allows a transfer between different schemas in the same database", () => {
@@ -0,0 +1,272 @@
// @vitest-environment happy-dom
import { createApp, defineComponent, h, nextTick, reactive } from "vue";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { Component } from "vue";
function passthrough(tag: string): Component {
return defineComponent({
inheritAttrs: false,
setup(_, { attrs, slots }) {
return () => h(tag, attrs, slots.default?.());
},
});
}
vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key }) }));
vi.mock("@lucide/vue", () => {
const Icon = passthrough("span");
return {
Square: Icon,
CheckSquare: Icon,
MinusSquare: Icon,
Search: Icon,
ChevronRight: Icon,
};
});
vi.mock("@/components/ui/button", () => ({ Button: passthrough("button") }));
import ObjectSelectionTree from "../ObjectSelectionTree.vue";
type Kind = "TABLE" | "VIEW" | "FUNCTION";
interface TreeGroup {
kind: Kind;
label: string;
items: string[];
}
const VIEWS: TreeGroup = { kind: "VIEW", label: "Views", items: ["v1", "v2", "v3"] };
const FUNCTIONS: TreeGroup = { kind: "FUNCTION", label: "Fns", items: ["f1", "f2"] };
const TABLES: TreeGroup = { kind: "TABLE", label: "Tables", items: ["t1", "t2"] };
function mountTree(init: { groups?: TreeGroup[]; disabledGroups?: Kind[]; disabledHints?: Record<string, string>; selection?: Record<string, string[]>; search?: string }) {
const { groups = [VIEWS, FUNCTIONS], disabledGroups = [], disabledHints = {}, selection = {}, search = "" } = init;
const state = reactive({ selection, search });
const Wrapper = defineComponent({
setup() {
return () =>
h(ObjectSelectionTree, {
groups,
disabledGroups,
disabledHints,
modelValue: state.selection,
"onUpdate:modelValue": (v: Record<string, string[]>) => {
state.selection = v;
},
search: state.search,
"onUpdate:search": (v: string) => {
state.search = v;
},
});
},
});
const container = document.createElement("div");
document.body.appendChild(container);
const app = createApp(Wrapper);
app.mount(container);
return { state, container, app };
}
function groupToggle(container: HTMLElement, index = 0): HTMLButtonElement {
return container.querySelectorAll<HTMLButtonElement>('button[data-test="group-toggle"]')[index];
}
function itemCheckbox(container: HTMLElement, kind: string, item: string): HTMLInputElement {
return container.querySelector<HTMLInputElement>(`label[data-test="item-${kind}-${item}"] input`)!;
}
function toolbarButton(container: HTMLElement, label: string): HTMLButtonElement | undefined {
return Array.from(container.querySelectorAll<HTMLButtonElement>("button")).find((b) => b.textContent?.includes(label));
}
async function typeSearch(container: HTMLElement, value: string) {
const input = container.querySelector<HTMLInputElement>('input[data-test="search"]')!;
input.value = value;
input.dispatchEvent(new Event("input"));
await nextTick();
}
let cleanup: (() => void) | undefined;
afterEach(() => {
cleanup?.();
cleanup = undefined;
document.body.innerHTML = "";
vi.clearAllMocks();
});
describe("ObjectSelectionTree interaction", () => {
it("renders group headers, items and the search input", async () => {
const { container, app } = mountTree({});
cleanup = () => app.unmount();
expect(container.textContent).toContain("Views");
expect(container.querySelector('label[data-test="item-VIEW-v1"]')).not.toBeNull();
expect(container.querySelector('label[data-test="item-FUNCTION-f2"]')).not.toBeNull();
expect(container.querySelector('input[data-test="search"]')).not.toBeNull();
});
it("toggles all items of a group through the group header checkbox", async () => {
const { state, container, app } = mountTree({});
cleanup = () => app.unmount();
groupToggle(container).click();
await nextTick();
expect(state.selection.VIEW).toEqual(["v1", "v2", "v3"]);
// clicking again with every visible item selected unchecks them all
groupToggle(container).click();
await nextTick();
expect(state.selection.VIEW ?? []).toEqual([]);
});
it("group toggle under a search only touches the visible items", async () => {
const { state, container, app } = mountTree({ selection: { VIEW: ["v1", "v2", "v3"] } });
cleanup = () => app.unmount();
await typeSearch(container, "v2");
// all visible items (only v2) are selected -> toggle unchecks only v2
groupToggle(container).click();
await nextTick();
expect(state.selection.VIEW).toEqual(["v1", "v3"]);
});
it("deselect all only clears visible enabled selections, keeping hidden ones", async () => {
const { state, container, app } = mountTree({
selection: { VIEW: ["v1", "v2", "v3"], FUNCTION: ["f1", "f2"] },
});
cleanup = () => app.unmount();
await typeSearch(container, "v1");
toolbarButton(container, "transfer.deselectAll")!.click();
await nextTick();
expect(state.selection.VIEW).toEqual(["v2", "v3"]); // hidden selections survive
expect(state.selection.FUNCTION).toEqual(["f1", "f2"]);
});
it("deselect all skips disabled groups entirely", async () => {
const { state, container, app } = mountTree({
groups: [TABLES, VIEWS],
disabledGroups: ["VIEW"],
selection: { TABLE: ["t1", "t2"], VIEW: ["v1", "v2"] },
});
cleanup = () => app.unmount();
toolbarButton(container, "transfer.deselectAll")!.click();
await nextTick();
expect(state.selection.TABLE ?? []).toEqual([]);
expect(state.selection.VIEW).toEqual(["v1", "v2"]);
});
it("select all only selects visible enabled groups, merging hidden selections", async () => {
const { state, container, app } = mountTree({
selection: { VIEW: ["v1"], FUNCTION: [] },
});
cleanup = () => app.unmount();
await typeSearch(container, "v2");
// VIEW is the only group with visible items; FUNCTION items are filtered out
toolbarButton(container, "transfer.selectAll")!.click();
await nextTick();
expect(state.selection.VIEW).toEqual(["v1", "v2"]);
expect(state.selection.FUNCTION ?? []).toEqual([]);
});
it("select all skips disabled groups", async () => {
const { state, container, app } = mountTree({
groups: [TABLES, VIEWS],
disabledGroups: ["VIEW"],
});
cleanup = () => app.unmount();
toolbarButton(container, "transfer.selectAll")!.click();
await nextTick();
expect(state.selection.TABLE).toEqual(["t1", "t2"]);
expect(state.selection.VIEW ?? []).toEqual([]);
});
it("shows select-all while some visible enabled items are still unselected", async () => {
// user scenario: every TABLE selected but VIEW untouched -> select-all
const { container, app } = mountTree({
groups: [TABLES, VIEWS],
selection: { TABLE: ["t1", "t2"], VIEW: ["v1"] },
});
cleanup = () => app.unmount();
expect(toolbarButton(container, "transfer.selectAll")).toBeDefined();
expect(toolbarButton(container, "transfer.deselectAll")).toBeUndefined();
});
it("shows deselect-all only when every visible enabled item is selected", async () => {
const { state, container, app } = mountTree({
selection: { VIEW: ["v1", "v2", "v3"], FUNCTION: ["f1", "f2"] },
});
cleanup = () => app.unmount();
expect(toolbarButton(container, "transfer.deselectAll")).toBeDefined();
expect(toolbarButton(container, "transfer.selectAll")).toBeUndefined();
// deselecting everything flips the button back to select-all
toolbarButton(container, "transfer.deselectAll")!.click();
await nextTick();
expect(state.selection).toEqual({});
expect(toolbarButton(container, "transfer.selectAll")).toBeDefined();
});
it("search-filtered view: all visible items selected still shows deselect-all", async () => {
const { container, app } = mountTree({
selection: { VIEW: ["v1", "v2", "v3"], FUNCTION: ["f1"] },
});
cleanup = () => app.unmount();
// only v1 and f1 remain visible; both are selected -> deselect-all
await typeSearch(container, "1");
expect(toolbarButton(container, "transfer.deselectAll")).toBeDefined();
// filtering to nothing leaves only hidden selections -> select-all is shown again
await typeSearch(container, "zzz-no-match");
expect(toolbarButton(container, "transfer.selectAll")).toBeDefined();
expect(toolbarButton(container, "transfer.deselectAll")).toBeUndefined();
});
it("hidden selections in disabled groups do not surface the deselect button", async () => {
const { container, app } = mountTree({
groups: [TABLES, VIEWS],
disabledGroups: ["VIEW"],
selection: { VIEW: ["v1", "v2"] },
});
cleanup = () => app.unmount();
expect(toolbarButton(container, "transfer.selectAll")).toBeDefined();
expect(toolbarButton(container, "transfer.deselectAll")).toBeUndefined();
});
it("item checkbox toggles a single selection", async () => {
const { state, container, app } = mountTree({});
cleanup = () => app.unmount();
itemCheckbox(container, "VIEW", "v2").click();
await nextTick();
expect(state.selection.VIEW).toEqual(["v2"]);
itemCheckbox(container, "VIEW", "v2").click();
await nextTick();
expect(state.selection.VIEW ?? []).toEqual([]);
});
it("group header checkbox shows none / partial / all tri-state", async () => {
// none: no selection in the group
const none = mountTree({ groups: [VIEWS], selection: {} });
cleanup = () => none.app.unmount();
expect(groupToggle(none.container).dataset.state).toBe("none");
none.app.unmount();
// partial: only some visible items selected
const partial = mountTree({ groups: [VIEWS], selection: { VIEW: ["v1"] } });
cleanup = () => partial.app.unmount();
expect(groupToggle(partial.container).dataset.state).toBe("partial");
partial.app.unmount();
// all: every visible item selected
const all = mountTree({ groups: [VIEWS], selection: { VIEW: ["v1", "v2", "v3"] } });
cleanup = () => all.app.unmount();
expect(groupToggle(all.container).dataset.state).toBe("all");
all.app.unmount();
});
it("group header state is computed over visible items only", async () => {
// every item selected, but the search narrows the visible set to v1
const { container, app } = mountTree({ groups: [VIEWS], selection: { VIEW: ["v1", "v2", "v3"] } });
cleanup = () => app.unmount();
await typeSearch(container, "v1");
// v1 is the only visible item and it is selected -> all
expect(groupToggle(container).dataset.state).toBe("all");
// clearing the search shows every item selected again -> all
await typeSearch(container, "");
expect(groupToggle(container).dataset.state).toBe("all");
});
});
@@ -0,0 +1,44 @@
import { describe, expect, it } from "vitest";
import { buildTransferObjectSelections } from "../transferSelections";
function setOf(names: string[]): Set<string> {
return new Set(names);
}
describe("buildTransferObjectSelections", () => {
it("serializes non-table selections in request order", () => {
const result = buildTransferObjectSelections(
{
VIEW: setOf(["v1", "v2"]),
SEQUENCE: setOf(["s1"]),
TABLE: setOf(["t1"]),
},
[],
);
expect(result).toEqual([
{ objectType: "VIEW", names: ["v1", "v2"] },
{ objectType: "SEQUENCE", names: ["s1"] },
]);
});
it("drops TABLE selections (handled by the tables field)", () => {
const result = buildTransferObjectSelections({ TABLE: setOf(["t1", "t2"]) }, []);
expect(result).toEqual([]);
});
it("filters disabled object types even when stale selections remain", () => {
const result = buildTransferObjectSelections(
{
VIEW: setOf(["v1"]),
SEQUENCE: setOf(["s1"]),
PROCEDURE: setOf(["p1"]),
},
["VIEW", "SEQUENCE"],
);
expect(result).toEqual([{ objectType: "PROCEDURE", names: ["p1"] }]);
});
it("returns an empty payload when nothing is selected", () => {
expect(buildTransferObjectSelections({}, [])).toEqual([]);
});
});
@@ -0,0 +1,22 @@
import type { TransferObjectKind } from "@/lib/backend/api";
export interface TransferObjectSelectionPayload {
objectType: TransferObjectKind;
names: string[];
}
/**
* Builds the `objects` payload of a transfer request from the tree
* selection state. TABLE is handled by `tables`, and object types that
* are currently disabled for this transfer (data-only mode, or kinds the
* cross-family matrix does not allow) are dropped at the request boundary
* even if stale selections are still present in the tree.
*/
export function buildTransferObjectSelections(selectedObjects: Partial<Record<TransferObjectKind, Set<string>>>, disabledGroups: TransferObjectKind[]): TransferObjectSelectionPayload[] {
return (Object.keys(selectedObjects) as TransferObjectKind[])
.filter((kind) => kind !== "TABLE" && !disabledGroups.includes(kind))
.map((kind) => ({
objectType: kind,
names: [...(selectedObjects[kind] ?? [])],
}));
}
+23
View File
@@ -3715,6 +3715,29 @@ export default {
}, },
transfer: { transfer: {
title: "Data Transfer", title: "Data Transfer",
content: "Transfer content",
contentStructureAndData: "Structure and data",
contentStructureOnly: "Structure only",
contentDataOnly: "Data only",
contentStructureOnlyHint: "Create table and object structures only, no data",
contentDataOnlyHint: "Copy data only into existing target tables",
objects: "Objects",
searchObjects: "Search object names...",
noObjects: "No objects",
objectTypeTable: "Tables",
objectTypeView: "Views",
objectTypeMaterializedView: "Materialized views",
objectTypeProcedure: "Procedures",
objectTypeFunction: "Functions",
objectTypeTrigger: "Triggers",
objectTypeSequence: "Sequences",
objectTypeEvent: "Events",
objectDataOnlyDisabled: "Non-table objects are not transferred in data-only mode",
objectCrossFamilyDisabled: "Cross-database transfer supports sequences only (plain DDL; view query bodies cannot be translated)",
crossFamilyViewHint: "Cross-database view transfer: select the tables the view depends on as well, or make sure they already exist in the target database",
objectUnsupportedDisabled: "This database does not support transferring this object type",
skippedExistingObjects: "Skipped {count} existing object(s)",
dataWriteMode: "Data write mode",
source: "Source", source: "Source",
target: "Target", target: "Target",
sourceConnection: "Source Connection", sourceConnection: "Source Connection",
+23
View File
@@ -3529,6 +3529,29 @@ export default withEnglishFallback({
}, },
transfer: { transfer: {
title: "Transferencia de datos", title: "Transferencia de datos",
content: "Modo de transferencia",
contentStructureAndData: "Estructura y datos",
contentStructureOnly: "Solo estructura",
contentDataOnly: "Solo datos",
contentStructureOnlyHint: "Crear solo las estructuras de tablas y objetos, sin copiar datos",
contentDataOnlyHint: "Copiar solo datos en las tablas de destino existentes",
objects: "Objetos",
searchObjects: "Buscar nombre de objeto...",
noObjects: "Sin objetos",
objectTypeTable: "Tablas",
objectTypeView: "Vistas",
objectTypeMaterializedView: "Vistas materializadas",
objectTypeProcedure: "Procedimientos",
objectTypeFunction: "Funciones",
objectTypeTrigger: "Disparadores",
objectTypeSequence: "Secuencias",
objectTypeEvent: "Eventos",
objectDataOnlyDisabled: "Los objetos que no son tablas no se transfieren en el modo de solo datos",
objectCrossFamilyDisabled: "La transferencia entre bases de datos solo admite secuencias (DDL simple; los cuerpos de las vistas no se traducen)",
crossFamilyViewHint: "Transferencia de vistas entre bases de datos: seleccione también las tablas de las que depende la vista o asegúrese de que ya existen en la base de datos de destino",
objectUnsupportedDisabled: "La base de datos actual no admite la transferencia de este tipo de objeto",
skippedExistingObjects: "Se omitieron {count} objetos existentes",
dataWriteMode: "Modo de escritura de datos",
source: "Origen", source: "Origen",
target: "Destino", target: "Destino",
sourceConnection: "Conexión de origen", sourceConnection: "Conexión de origen",
+23
View File
@@ -3527,6 +3527,29 @@ export default withEnglishFallback({
}, },
transfer: { transfer: {
title: "Trasferimento Dati", title: "Trasferimento Dati",
content: "Contenuto del trasferimento",
contentStructureAndData: "Struttura e dati",
contentStructureOnly: "Solo struttura",
contentDataOnly: "Solo dati",
contentStructureOnlyHint: "Crea solo la struttura di tabelle e oggetti, senza copiare dati",
contentDataOnlyHint: "Copia solo i dati nelle tabelle di destinazione esistenti",
objects: "Oggetti",
searchObjects: "Cerca nome oggetto...",
noObjects: "Nessun oggetto",
objectTypeTable: "Tabelle",
objectTypeView: "Viste",
objectTypeMaterializedView: "Viste materializzate",
objectTypeProcedure: "Procedure",
objectTypeFunction: "Funzioni",
objectTypeTrigger: "Trigger",
objectTypeSequence: "Sequenze",
objectTypeEvent: "Eventi",
objectDataOnlyDisabled: "Gli oggetti non di tabella non vengono trasferiti nella modalità solo dati",
objectCrossFamilyDisabled: "Il trasferimento tra database supporta solo sequenze (DDL semplice; i corpi delle viste non vengono tradotti)",
crossFamilyViewHint: "Trasferimento di viste tra database: selezionare anche le tabelle da cui la vista dipende, o assicurarsi che esistano già nel database di destinazione",
objectUnsupportedDisabled: "Il database corrente non supporta il trasferimento di questo tipo di oggetto",
skippedExistingObjects: "Saltati {count} oggetti esistenti",
dataWriteMode: "Modalità di scrittura dati",
source: "Sorgente", source: "Sorgente",
target: "Destinazione", target: "Destinazione",
sourceConnection: "Connessione Sorgente", sourceConnection: "Connessione Sorgente",
+23
View File
@@ -3559,6 +3559,29 @@ export default withEnglishFallback({
}, },
transfer: { transfer: {
title: "データ転送", title: "データ転送",
content: "転送内容",
contentStructureAndData: "構造とデータ",
contentStructureOnly: "構造のみ",
contentDataOnly: "データのみ",
contentStructureOnlyHint: "テーブルとオブジェクトの構造のみを作成し、データはコピーしません",
contentDataOnlyHint: "既存のターゲットテーブルにデータのみをコピーします",
objects: "オブジェクト",
searchObjects: "オブジェクト名で検索...",
noObjects: "オブジェクトなし",
objectTypeTable: "テーブル",
objectTypeView: "ビュー",
objectTypeMaterializedView: "マテリアライズドビュー",
objectTypeProcedure: "プロシージャ",
objectTypeFunction: "関数",
objectTypeTrigger: "トリガー",
objectTypeSequence: "シーケンス",
objectTypeEvent: "イベント",
objectDataOnlyDisabled: "データのみモードではテーブル以外のオブジェクトは転送されません",
objectCrossFamilyDisabled: "データベース間転送はシーケンスのみサポートします(単純なDDL。ビューの本文は翻訳されません)",
crossFamilyViewHint: "データベース間ビュー転送:ビューが依存するテーブルも選択するか、ターゲットDBに既に存在することを確認してください",
objectUnsupportedDisabled: "現在のデータベースは、このオブジェクトタイプの転送をサポートしていません",
skippedExistingObjects: "既存のオブジェクト {count} 件をスキップしました",
dataWriteMode: "データ書き込みモード",
source: "ソース", source: "ソース",
target: "対象", target: "対象",
sourceConnection: "ソース接続", sourceConnection: "ソース接続",
+23
View File
@@ -3461,6 +3461,29 @@ export default withEnglishFallback({
}, },
transfer: { transfer: {
title: "데이터 이전", title: "데이터 이전",
content: "전송 콘텐츠",
contentStructureAndData: "구조 및 데이터",
contentStructureOnly: "구조만",
contentDataOnly: "데이터만",
contentStructureOnlyHint: "테이블 및 개체 구조만 생성하고 데이터는 복사하지 않습니다",
contentDataOnlyHint: "기존 대상 테이블에 데이터만 복사합니다",
objects: "개체",
searchObjects: "개체 이름 검색...",
noObjects: "개체 없음",
objectTypeTable: "테이블",
objectTypeView: "뷰",
objectTypeMaterializedView: "구체화된 뷰",
objectTypeProcedure: "프로시저",
objectTypeFunction: "함수",
objectTypeTrigger: "트리거",
objectTypeSequence: "시퀀스",
objectTypeEvent: "이벤트",
objectDataOnlyDisabled: "데이터 전용 모드에서는 테이블이 아닌 개체는 전송되지 않습니다",
objectCrossFamilyDisabled: "데이터베이스 간 전송은 시퀀스만 지원합니다(단순 DDL, 뷰 본문은 변환되지 않음)",
crossFamilyViewHint: "데이터베이스 간 뷰 전송: 뷰가 의존하는 테이블도 선택하거나 대상 DB에 이미 존재하는지 확인하세요",
objectUnsupportedDisabled: "현재 데이터베이스는 이 개체 유형의 전송을 지원하지 않습니다",
skippedExistingObjects: "기존 개체 {count}개 건너뜀",
dataWriteMode: "데이터 쓰기 모드",
source: "소스", source: "소스",
target: "대상", target: "대상",
sourceConnection: "소스 연결", sourceConnection: "소스 연결",
+23
View File
@@ -3529,6 +3529,29 @@ export default withEnglishFallback({
}, },
transfer: { transfer: {
title: "Transferência de Dados", title: "Transferência de Dados",
content: "Conteúdo de transferência",
contentStructureAndData: "Estrutura e dados",
contentStructureOnly: "Apenas estrutura",
contentDataOnly: "Apenas dados",
contentStructureOnlyHint: "Cria apenas a estrutura de tabelas e objetos, sem copiar dados",
contentDataOnlyHint: "Copia apenas os dados para tabelas de destino existentes",
objects: "Objetos",
searchObjects: "Buscar nome do objeto...",
noObjects: "Nenhum objeto",
objectTypeTable: "Tabelas",
objectTypeView: "Views",
objectTypeMaterializedView: "Views materializadas",
objectTypeProcedure: "Procedimentos",
objectTypeFunction: "Funções",
objectTypeTrigger: "Triggers",
objectTypeSequence: "Sequências",
objectTypeEvent: "Eventos",
objectDataOnlyDisabled: "Objetos que não são tabelas não são transferidos no modo apenas dados",
objectCrossFamilyDisabled: "A transferência entre bancos suporta apenas sequências (DDL simples; corpos de views não são traduzidos)",
crossFamilyViewHint: "Transferência de views entre bancos: selecione também as tabelas das quais a view depende ou garanta que elas já existam no banco de destino",
objectUnsupportedDisabled: "O banco de dados atual não suporta a transferência deste tipo de objeto",
skippedExistingObjects: "Ignorado {count} objeto(s) existente(s)",
dataWriteMode: "Modo de gravação de dados",
source: "Origem", source: "Origem",
target: "Destino", target: "Destino",
sourceConnection: "Conexão de origem", sourceConnection: "Conexão de origem",
+23
View File
@@ -3715,6 +3715,29 @@ export default withEnglishFallback({
}, },
transfer: { transfer: {
title: "数据传输", title: "数据传输",
content: "传输方式",
contentStructureAndData: "结构和数据",
contentStructureOnly: "仅结构",
contentDataOnly: "仅数据",
contentStructureOnlyHint: "仅创建表和对象结构,不复制数据",
contentDataOnlyHint: "仅复制数据到已存在的目标表",
objects: "传输对象",
searchObjects: "搜索对象名...",
noObjects: "暂无对象",
objectTypeTable: "表",
objectTypeView: "视图",
objectTypeMaterializedView: "物化视图",
objectTypeProcedure: "存储过程",
objectTypeFunction: "函数",
objectTypeTrigger: "触发器",
objectTypeSequence: "序列",
objectTypeEvent: "事件",
objectDataOnlyDisabled: "仅数据模式不传输非表对象",
objectCrossFamilyDisabled: "跨库仅支持序列传输(纯DDL;视图查询体无法翻译)",
crossFamilyViewHint: "跨库视图传输:请同时选择视图依赖的表,或确保目标库已存在这些表",
objectUnsupportedDisabled: "当前数据库不支持该对象类型传输",
skippedExistingObjects: "跳过 {count} 个已存在对象",
dataWriteMode: "数据写入方式",
source: "源", source: "源",
target: "目标", target: "目标",
sourceConnection: "源连接", sourceConnection: "源连接",
+23
View File
@@ -2997,6 +2997,29 @@ export default withEnglishFallback({
}, },
transfer: { transfer: {
title: "資料傳輸", title: "資料傳輸",
content: "傳輸方式",
contentStructureAndData: "結構和資料",
contentStructureOnly: "僅結構",
contentDataOnly: "僅資料",
contentStructureOnlyHint: "僅建立表和物件結構,不複製資料",
contentDataOnlyHint: "僅複製資料到已存在之目標表",
objects: "傳輸物件",
searchObjects: "搜尋物件名稱...",
noObjects: "暫無物件",
objectTypeTable: "表",
objectTypeView: "檢視",
objectTypeMaterializedView: "具體化檢視",
objectTypeProcedure: "預存程序",
objectTypeFunction: "函式",
objectTypeTrigger: "觸發程序",
objectTypeSequence: "序列",
objectTypeEvent: "事件",
objectDataOnlyDisabled: "僅資料模式不傳輸非表物件",
objectCrossFamilyDisabled: "跨庫僅支援序列傳輸(純DDL;檢視查詢體無法翻譯)",
crossFamilyViewHint: "跨庫檢視傳輸:請同時選擇檢視依賴的表,或確保目標庫已存在這些表",
objectUnsupportedDisabled: "目前資料庫不支援該物件類型傳輸",
skippedExistingObjects: "跳過 {count} 個已存在物件",
dataWriteMode: "資料寫入方式",
source: "來源", source: "來源",
target: "目標", target: "目標",
sourceConnection: "來源連線", sourceConnection: "來源連線",
+3
View File
@@ -719,6 +719,9 @@ export type {
TransferRequest, TransferRequest,
TransferProgress, TransferProgress,
TransferMode, TransferMode,
TransferContent,
TransferObjectKind,
TransferObjectSelection,
TransferTableNameCase, TransferTableNameCase,
TransferOwnershipPolicy, TransferOwnershipPolicy,
TransferOwnershipPreview, TransferOwnershipPreview,
+1 -1
View File
@@ -692,7 +692,7 @@ export async function getTableComment(_connectionId: string, _database: string,
throw new Error("Table comment lookup is not available in the web backend"); throw new Error("Table comment lookup is not available in the web backend");
} }
export async function listObjects(connectionId: string, database: string, schema: string, objectTypes?: SidebarObjectKind[], filter?: string, limit?: number, offset?: number, catalog?: string): Promise<ObjectInfo[]> { export async function listObjects(connectionId: string, database: string, schema: string, objectTypes?: (SidebarObjectKind | "EVENT")[], filter?: string, limit?: number, offset?: number, catalog?: string): Promise<ObjectInfo[]> {
return get( return get(
`/api/schema/objects?${qs({ `/api/schema/objects?${qs({
connection_id: connectionId, connection_id: connectionId,
+10 -1
View File
@@ -974,7 +974,7 @@ export async function getTableComment(connectionId: string, database: string, sc
}); });
} }
export async function listObjects(connectionId: string, database: string, schema: string, objectTypes?: SidebarObjectKind[], filter?: string, limit?: number, offset?: number, catalog?: string): Promise<ObjectInfo[]> { export async function listObjects(connectionId: string, database: string, schema: string, objectTypes?: (SidebarObjectKind | "EVENT")[], filter?: string, limit?: number, offset?: number, catalog?: string): Promise<ObjectInfo[]> {
return invoke("list_objects", { return invoke("list_objects", {
connectionId, connectionId,
database, database,
@@ -3301,6 +3301,13 @@ export async function listenSqlFileProgress(handler: (progress: SqlFileProgress)
export type TransferMode = "append" | "overwrite" | "upsert"; export type TransferMode = "append" | "overwrite" | "upsert";
export type TransferTableNameCase = "preserve" | "lower" | "upper"; export type TransferTableNameCase = "preserve" | "lower" | "upper";
export type TransferOwnershipPolicy = "preserve" | "skip" | "reassignMissing"; export type TransferOwnershipPolicy = "preserve" | "skip" | "reassignMissing";
export type TransferContent = "structureAndData" | "structureOnly" | "dataOnly";
export type TransferObjectKind = "TABLE" | "VIEW" | "MATERIALIZED_VIEW" | "PROCEDURE" | "FUNCTION" | "TRIGGER" | "SEQUENCE" | "EVENT";
export interface TransferObjectSelection {
objectType: TransferObjectKind;
names: string[];
}
export interface TransferRequest { export interface TransferRequest {
transferId: string; transferId: string;
@@ -3314,6 +3321,8 @@ export interface TransferRequest {
targetCatalog?: string; targetCatalog?: string;
tables: string[]; tables: string[];
createTable: boolean; createTable: boolean;
content: TransferContent;
objects: TransferObjectSelection[];
mode: TransferMode; mode: TransferMode;
targetTableNameCase: TransferTableNameCase; targetTableNameCase: TransferTableNameCase;
ownershipPolicy?: TransferOwnershipPolicy; ownershipPolicy?: TransferOwnershipPolicy;
@@ -0,0 +1,46 @@
import { describe, expect, it } from "vitest";
import { transferObjectFamily, transferObjectKindsForDatabase, isSameTransferFamily, crossFamilyTransferableKinds, TransferObjectFamily } from "@/lib/database/transferObjectKinds";
describe("transferObjectKinds", () => {
it("groups databases into transfer families", () => {
expect(transferObjectFamily("mysql")).toBe(TransferObjectFamily.Mysql);
expect(transferObjectFamily("kingbase")).toBe(TransferObjectFamily.Postgres);
expect(transferObjectFamily("dameng")).toBe(TransferObjectFamily.Oracle);
expect(transferObjectFamily("sqlserver")).toBe(TransferObjectFamily.SqlServer);
expect(transferObjectFamily("sqlite")).toBeUndefined();
});
it("returns per-family object kinds", () => {
expect(transferObjectKindsForDatabase("mysql")).toEqual(["TABLE", "VIEW", "PROCEDURE", "FUNCTION", "TRIGGER", "EVENT"]);
expect(transferObjectKindsForDatabase("postgres")).toContain("SEQUENCE");
expect(transferObjectKindsForDatabase("dameng")).toContain("TRIGGER");
expect(transferObjectKindsForDatabase("sqlserver")).toEqual(["TABLE", "VIEW", "PROCEDURE", "FUNCTION", "TRIGGER", "SEQUENCE"]);
expect(transferObjectKindsForDatabase("sqlite")).toEqual([]);
});
it("detects same-family transfers", () => {
expect(isSameTransferFamily("mysql", "mysql")).toBe(true);
expect(isSameTransferFamily("mysql", "postgres")).toBe(false);
expect(isSameTransferFamily("oracle", "dameng")).toBe(true);
expect(isSameTransferFamily("postgres", "sqlite")).toBe(false);
expect(isSameTransferFamily("sqlserver", "sqlserver")).toBe(true);
expect(isSameTransferFamily("sqlserver", "mysql")).toBe(false);
});
it("limits cross-family transferable kinds to sequences only", () => {
// mysql participates: VIEW is disabled (query body not translated) and
// mysql has no sequences on either side
expect(crossFamilyTransferableKinds("mysql", "dameng")).toEqual([]);
expect(crossFamilyTransferableKinds("dameng", "mysql")).toEqual([]);
expect(crossFamilyTransferableKinds("mysql", "sqlserver")).toEqual([]);
// sqlserver <-> dameng: sequences only (plain DDL)
expect(crossFamilyTransferableKinds("sqlserver", "dameng")).toEqual(["SEQUENCE"]);
expect(crossFamilyTransferableKinds("dameng", "sqlserver")).toEqual(["SEQUENCE"]);
// same family: all source kinds
const same = crossFamilyTransferableKinds("mysql", "mysql");
expect(same).toContain("TRIGGER");
expect(same).toContain("EVENT");
// unsupported db
expect(crossFamilyTransferableKinds("sqlite", "mysql")).toEqual([]);
});
});
@@ -0,0 +1,86 @@
import type { DatabaseType } from "@/types/database";
export enum TransferObjectFamily {
Mysql = "mysql",
Postgres = "postgres",
Oracle = "oracle",
SqlServer = "sqlserver",
}
export type TransferObjectKind = "TABLE" | "VIEW" | "MATERIALIZED_VIEW" | "PROCEDURE" | "FUNCTION" | "TRIGGER" | "SEQUENCE" | "EVENT";
const MYSQL_KINDS: TransferObjectKind[] = ["TABLE", "VIEW", "PROCEDURE", "FUNCTION", "TRIGGER", "EVENT"];
const POSTGRES_KINDS: TransferObjectKind[] = ["TABLE", "VIEW", "MATERIALIZED_VIEW", "PROCEDURE", "FUNCTION", "TRIGGER", "SEQUENCE"];
const ORACLE_KINDS: TransferObjectKind[] = ["TABLE", "VIEW", "MATERIALIZED_VIEW", "PROCEDURE", "FUNCTION", "TRIGGER", "SEQUENCE"];
const SQLSERVER_KINDS: TransferObjectKind[] = ["TABLE", "VIEW", "PROCEDURE", "FUNCTION", "TRIGGER", "SEQUENCE"];
const FAMILY_BY_DB = new Map<DatabaseType, TransferObjectFamily>([
["mysql", TransferObjectFamily.Mysql],
["postgres", TransferObjectFamily.Postgres],
["kingbase", TransferObjectFamily.Postgres],
["gaussdb", TransferObjectFamily.Postgres],
["kwdb", TransferObjectFamily.Postgres],
["opengauss", TransferObjectFamily.Postgres],
["oracle", TransferObjectFamily.Oracle],
["dameng", TransferObjectFamily.Oracle],
["oceanbase-oracle", TransferObjectFamily.Oracle],
["sqlserver", TransferObjectFamily.SqlServer],
]);
export function transferObjectFamily(dbType?: DatabaseType): TransferObjectFamily | undefined {
return dbType ? FAMILY_BY_DB.get(dbType) : undefined;
}
export function isSameTransferFamily(a?: DatabaseType, b?: DatabaseType): boolean {
const fa = transferObjectFamily(a);
const fb = transferObjectFamily(b);
return !!fa && fa === fb;
}
export function transferObjectKindsForDatabase(dbType?: DatabaseType): TransferObjectKind[] {
switch (transferObjectFamily(dbType)) {
case TransferObjectFamily.Mysql:
return [...MYSQL_KINDS];
case TransferObjectFamily.Postgres:
return [...POSTGRES_KINDS];
case TransferObjectFamily.Oracle:
return [...ORACLE_KINDS];
case TransferObjectFamily.SqlServer:
return [...SQLSERVER_KINDS];
default:
return [];
}
}
/**
* Kinds selectable for a transfer between the two databases. Within one
* family every kind of the source is allowed; across families only
* sequences are allowed (plain DDL without a query body), and only when
* both sides support the type. Views are excluded: the backend rewrites
* only the DDL wrapper, quoting and schema qualifiers, not the view
* query body, so cross-family view bodies could run unchanged on an
* incompatible target.
*/
export function crossFamilyTransferableKinds(a?: DatabaseType, b?: DatabaseType): TransferObjectKind[] {
if (isSameTransferFamily(a, b)) {
return transferObjectKindsForDatabase(a);
}
const aFam = transferObjectFamily(a);
const bFam = transferObjectFamily(b);
// Cross-family DDL conversion is only implemented and validated for
// MySQL / SQL Server / Oracle families. Postgres-family sources or
// targets (postgres/kingbase/gaussdb/kwdb/opengauss) are not supported
// by the cross-family executor, so nothing is selectable there.
const supported = new Set([TransferObjectFamily.Mysql, TransferObjectFamily.SqlServer, TransferObjectFamily.Oracle]);
if (!aFam || !bFam || !supported.has(aFam) || !supported.has(bFam)) return [];
const aKinds = transferObjectKindsForDatabase(a);
const bKinds = transferObjectKindsForDatabase(b);
// Cross-family VIEW transfer is disabled: the backend only rewrites the
// DDL wrapper/quoting/schema qualifiers and cannot translate the view
// query body (IFNULL, TOP, GETDATE, … would run unchanged on an
// incompatible target). Sequences are plain DDL and remain transferable
// when both sides support them.
const allowed: TransferObjectKind[] = [];
if (aKinds.includes("SEQUENCE") && bKinds.includes("SEQUENCE")) allowed.push("SEQUENCE");
return allowed;
}
+81
View File
@@ -2798,6 +2798,14 @@ fn wants_routine_objects(object_types: Option<&[String]>) -> bool {
requested_object_type(object_types, "PROCEDURE") || requested_object_type(object_types, "FUNCTION") requested_object_type(object_types, "PROCEDURE") || requested_object_type(object_types, "FUNCTION")
} }
fn wants_trigger_objects(object_types: Option<&[String]>) -> bool {
requested_object_type(object_types, "TRIGGER")
}
fn wants_event_objects(object_types: Option<&[String]>) -> bool {
requested_object_type(object_types, "EVENT")
}
fn sql_pagination(limit: Option<usize>, offset: Option<usize>) -> String { fn sql_pagination(limit: Option<usize>, offset: Option<usize>) -> String {
limit.map_or_else(String::new, |limit| format!(" LIMIT {limit} OFFSET {}", offset.unwrap_or(0))) limit.map_or_else(String::new, |limit| format!(" LIMIT {limit} OFFSET {}", offset.unwrap_or(0)))
} }
@@ -2888,6 +2896,32 @@ fn row_to_object(row: &mysql_async::Row, database: &str) -> ObjectInfo {
} }
} }
fn list_triggers_objects_sql(database: &str) -> String {
format!(
"SELECT TRIGGER_NAME AS object_name, 'TRIGGER' AS object_type, NULL AS object_comment, \
CREATED AS created_at, NULL AS updated_at, \
TRIGGER_SCHEMA AS parent_schema, EVENT_OBJECT_TABLE AS parent_name, \
5 AS sort_order \
FROM information_schema.TRIGGERS \
WHERE TRIGGER_SCHEMA = {} \
ORDER BY object_name",
quote_value(database)
)
}
fn list_events_objects_sql(database: &str) -> String {
format!(
"SELECT EVENT_NAME AS object_name, 'EVENT' AS object_type, NULL AS object_comment, \
CREATED AS created_at, LAST_ALTERED AS updated_at, \
EVENT_SCHEMA AS parent_schema, NULL AS parent_name, \
6 AS sort_order \
FROM information_schema.EVENTS \
WHERE EVENT_SCHEMA = {} \
ORDER BY object_name",
quote_value(database)
)
}
pub struct PagedObjectList { pub struct PagedObjectList {
pub objects: Vec<ObjectInfo>, pub objects: Vec<ObjectInfo>,
pub paging_applied: bool, pub paging_applied: bool,
@@ -2985,6 +3019,40 @@ pub async fn list_objects(
} }
} }
if wants_trigger_objects(object_types) {
let triggers_sql = list_triggers_objects_sql(database);
match conn.query_iter(&triggers_sql).await {
Ok(result) => match result.collect_and_drop::<mysql_async::Row>().await {
Ok(trigger_rows) => {
objects.extend(trigger_rows.iter().map(|row| row_to_object(row, database)));
}
Err(e) => {
log::warn!("Skipping triggers for database `{}` in object browser: {}", database, e);
}
},
Err(e) => {
log::warn!("Skipping triggers for database `{}` in object browser: {}", database, e);
}
}
}
if wants_event_objects(object_types) {
let events_sql = list_events_objects_sql(database);
match conn.query_iter(&events_sql).await {
Ok(result) => match result.collect_and_drop::<mysql_async::Row>().await {
Ok(event_rows) => {
objects.extend(event_rows.iter().map(|row| row_to_object(row, database)));
}
Err(e) => {
log::warn!("Skipping events for database `{}` in object browser: {}", database, e);
}
},
Err(e) => {
log::warn!("Skipping events for database `{}` in object browser: {}", database, e);
}
}
}
Ok(PagedObjectList { objects, paging_applied }) Ok(PagedObjectList { objects, paging_applied })
} }
@@ -5179,6 +5247,19 @@ mod tests {
assert!(sql.contains("TRIGGER_SCHEMA = 'app'")); assert!(sql.contains("TRIGGER_SCHEMA = 'app'"));
} }
#[test]
fn lists_triggers_and_events_via_information_schema() {
let sql = list_triggers_objects_sql("shop");
assert!(sql.contains("information_schema.TRIGGERS"));
assert!(sql.contains("TRIGGER_SCHEMA = 'shop'"));
let sql = list_events_objects_sql("shop");
assert!(sql.contains("information_schema.EVENTS"));
assert!(sql.contains("EVENT_SCHEMA = 'shop'"));
assert!(wants_trigger_objects(Some(&["TRIGGER".to_string()])));
assert!(!wants_trigger_objects(Some(&["TABLE".to_string()])));
assert!(wants_event_objects(Some(&["EVENT".to_string()])));
}
#[test] #[test]
fn mysql_completion_like_pattern_uses_prefix_by_default() { fn mysql_completion_like_pattern_uses_prefix_by_default() {
assert_eq!(mysql_completion_like_pattern("Temp", Some(&CompletionAssistantMatchMode::Prefix)), "Temp%"); assert_eq!(mysql_completion_like_pattern("Temp", Some(&CompletionAssistantMatchMode::Prefix)), "Temp%");
File diff suppressed because it is too large. Load diff
@@ -218,6 +218,8 @@ async fn live_postgres_transfer_preserves_data_and_schema_objects() {
target_catalog: None, target_catalog: None,
tables: vec!["users".to_string(), "audit_logs".to_string(), "files".to_string()], tables: vec!["users".to_string(), "audit_logs".to_string(), "files".to_string()],
create_table: true, create_table: true,
content: dbx_core::transfer::TransferContent::default(),
objects: Vec::new(),
mode: TransferMode::Append, mode: TransferMode::Append,
target_table_name_case: TransferTableNameCase::Preserve, target_table_name_case: TransferTableNameCase::Preserve,
ownership_policy: TransferOwnershipPolicy::Preserve, ownership_policy: TransferOwnershipPolicy::Preserve,
@@ -501,6 +503,8 @@ async fn live_postgres_transfer_skips_create_ddl_for_existing_target_table() {
target_catalog: None, target_catalog: None,
tables: vec!["items".to_string()], tables: vec!["items".to_string()],
create_table: true, create_table: true,
content: dbx_core::transfer::TransferContent::default(),
objects: Vec::new(),
mode: TransferMode::Append, mode: TransferMode::Append,
target_table_name_case: TransferTableNameCase::Preserve, target_table_name_case: TransferTableNameCase::Preserve,
ownership_policy: TransferOwnershipPolicy::Preserve, ownership_policy: TransferOwnershipPolicy::Preserve,
@@ -1361,6 +1361,8 @@ async fn live_sqlserver_transfer_table_skips_rowversion_insert_column() {
target_catalog: None, target_catalog: None,
tables: vec![source_table.clone()], tables: vec![source_table.clone()],
create_table: true, create_table: true,
content: dbx_core::transfer::TransferContent::default(),
objects: Vec::new(),
mode: dbx_core::transfer::TransferMode::Append, mode: dbx_core::transfer::TransferMode::Append,
target_table_name_case: dbx_core::transfer::TransferTableNameCase::Upper, target_table_name_case: dbx_core::transfer::TransferTableNameCase::Upper,
ownership_policy: dbx_core::transfer::TransferOwnershipPolicy::Preserve, ownership_policy: dbx_core::transfer::TransferOwnershipPolicy::Preserve,
+241 -51
View File
@@ -78,7 +78,7 @@ pub async fn start_transfer(
Json(body): Json<StartTransferRequest>, Json(body): Json<StartTransferRequest>,
) -> Result<Json<serde_json::Value>, AppError> { ) -> Result<Json<serde_json::Value>, AppError> {
let req = body.request; let req = body.request;
transfer::validate_transfer_target_table_names(&req).map_err(AppError::from)?; transfer::validate_transfer_request(&req).map_err(AppError::from)?;
// Reject transfer early if the target connection is read-only // Reject transfer early if the target connection is read-only
if let Some(name) = dbx_core::query::connection_readonly_name(&state.app, &req.target_connection_id).await { if let Some(name) = dbx_core::query::connection_readonly_name(&state.app, &req.target_connection_id).await {
@@ -116,6 +116,22 @@ pub async fn start_transfer(
} }
}; };
// Cross-family object transfers are validated inside transfer_schema_objects:
// only mechanically rewriteable kinds (views, sequences) are allowed; any
// other selection fails with a descriptive error. Structure-only data
// transfer is unsupported for MongoDB.
if matches!(req.content, transfer::TransferContent::StructureOnly)
&& (matches!(source_db_type, dbx_core::models::connection::DatabaseType::MongoDb)
|| matches!(target_db_type, dbx_core::models::connection::DatabaseType::MongoDb))
{
send_transfer_progress(
&progress_channel,
&terminal_transfer_error(&req, "MongoDB 暂不支持仅结构传输".to_string()),
);
finish_transfer_channel(&state_clone, &req.transfer_id, &progress_channel).await;
return;
}
// External Doris/StarRocks catalogs: pool is created with `catalog=` URL // External Doris/StarRocks catalogs: pool is created with `catalog=` URL
// setup (SET catalog) and without USE <external-db>. See ensure_transfer_pool. // setup (SET catalog) and without USE <external-db>. See ensure_transfer_pool.
let source_pool_key = match transfer::ensure_transfer_pool( let source_pool_key = match transfer::ensure_transfer_pool(
@@ -321,57 +337,65 @@ pub async fn start_transfer(
} }
} }
if matches!(source_db_type, dbx_core::models::connection::DatabaseType::Postgres) // Transfer selected non-table objects (views, procedures, functions,
&& matches!(target_db_type, dbx_core::models::connection::DatabaseType::Postgres) // triggers, sequences, events) after the per-table loop. The shared
// Core decision handles all content modes: DataOnly never
// transfers schema objects; PG→PG keeps the legacy empty-selection
// default only when structure participates in the transfer.
let mut object_outcome = transfer::TransferObjectOutcome::default();
let progress_channel_clone = progress_channel.clone();
match transfer::transfer_schema_objects(&app, &req, &source_pool_key, &target_pool_key, |progress| {
send_transfer_progress(&progress_channel_clone, &progress);
})
.await
{ {
let progress_channel_clone = progress_channel.clone(); Ok(outcome) => {
match transfer::transfer_postgres_schema_objects( object_outcome = outcome;
&app, }
&req, Err(e) if e == "Cancelled" => {
&source_pool_key, let progress = transfer::TransferProgress {
&target_pool_key, transfer_id: req.transfer_id.clone(),
|progress| { table: "schema objects".to_string(),
send_transfer_progress(&progress_channel_clone, &progress); table_index: tables.len(),
}, total_tables: tables.len(),
) rows_transferred: 0,
.await total_rows: None,
{ status: TransferStatus::Cancelled,
Ok(()) => {} error: None,
Err(e) if e == "Cancelled" => { terminal: true,
let progress = transfer::TransferProgress { };
transfer_id: req.transfer_id.clone(), send_transfer_progress(&progress_channel, &progress);
table: "schema objects".to_string(), finish_transfer_channel(&state_clone, &req.transfer_id, &progress_channel).await;
table_index: tables.len(), return;
total_tables: tables.len(), }
rows_transferred: 0, Err(e) => {
total_rows: None, failed_tables.push("schema objects".to_string());
status: TransferStatus::Cancelled, let progress = transfer::TransferProgress {
error: None, transfer_id: req.transfer_id.clone(),
terminal: true, table: "schema objects".to_string(),
}; table_index: tables.len(),
send_transfer_progress(&progress_channel, &progress); total_tables: tables.len(),
finish_transfer_channel(&state_clone, &req.transfer_id, &progress_channel).await; rows_transferred: 0,
return; total_rows: None,
} status: TransferStatus::Error,
Err(e) => { error: Some(e),
failed_tables.push("schema objects".to_string()); terminal: false,
let progress = transfer::TransferProgress { };
transfer_id: req.transfer_id.clone(), send_transfer_progress(&progress_channel, &progress);
table: "schema objects".to_string(),
table_index: tables.len(),
total_tables: tables.len(),
rows_transferred: 0,
total_rows: None,
status: TransferStatus::Error,
error: Some(e),
terminal: false,
};
send_transfer_progress(&progress_channel, &progress);
}
} }
} }
// Send done // Send done
if !object_outcome.failed.is_empty() {
failed_tables.push(format!("schema objects ({})", object_outcome.failed.len()));
}
let skip_suffix = if !object_outcome.skipped.is_empty() && failed_tables.is_empty() {
format!(",跳过 {} 个已存在对象", object_outcome.skipped.len())
} else if !object_outcome.skipped.is_empty() {
format!(";跳过 {} 个已存在对象", object_outcome.skipped.len())
} else {
String::new()
};
let done = transfer::TransferProgress { let done = transfer::TransferProgress {
transfer_id: req.transfer_id.clone(), transfer_id: req.transfer_id.clone(),
table: String::new(), table: String::new(),
@@ -381,12 +405,17 @@ pub async fn start_transfer(
total_rows: None, total_rows: None,
status: if failed_tables.is_empty() { TransferStatus::Done } else { TransferStatus::Error }, status: if failed_tables.is_empty() { TransferStatus::Done } else { TransferStatus::Error },
error: if failed_tables.is_empty() { error: if failed_tables.is_empty() {
None if skip_suffix.is_empty() {
None
} else {
Some(skip_suffix.clone())
}
} else { } else {
Some(format!( Some(format!(
"{} table(s) failed: {}", "{} table(s) failed: {}{}",
failed_tables.len(), failed_tables.len(),
failed_tables.iter().take(5).cloned().collect::<Vec<_>>().join(", ") failed_tables.iter().take(5).cloned().collect::<Vec<_>>().join(", "),
skip_suffix
)) ))
}, },
terminal: true, terminal: true,
@@ -403,7 +432,7 @@ pub async fn preview_transfer_ownership(
Json(body): Json<PreviewTransferOwnershipRequest>, Json(body): Json<PreviewTransferOwnershipRequest>,
) -> Result<Json<dbx_core::transfer::TransferOwnershipPreview>, AppError> { ) -> Result<Json<dbx_core::transfer::TransferOwnershipPreview>, AppError> {
let req = body.request; let req = body.request;
transfer::validate_transfer_target_table_names(&req).map_err(AppError::from)?; transfer::validate_transfer_request(&req).map_err(AppError::from)?;
let source_db_type = transfer::get_db_type(&state.app, &req.source_connection_id).await.map_err(AppError::from)?; let source_db_type = transfer::get_db_type(&state.app, &req.source_connection_id).await.map_err(AppError::from)?;
let target_db_type = transfer::get_db_type(&state.app, &req.target_connection_id).await.map_err(AppError::from)?; let target_db_type = transfer::get_db_type(&state.app, &req.target_connection_id).await.map_err(AppError::from)?;
let source_pool_key = transfer::ensure_transfer_pool( let source_pool_key = transfer::ensure_transfer_pool(
@@ -480,3 +509,164 @@ pub async fn sort_tables_by_fk_dependency(
.map(Json) .map(Json)
.map_err(AppError::from) .map_err(AppError::from)
} }
#[cfg(test)]
mod tests {
use super::*;
use axum::extract::State;
use axum::response::IntoResponse;
use dbx_core::connection::AppState;
use dbx_core::models::connection::{
default_connect_timeout_secs, default_idle_timeout_secs, default_keepalive_interval_secs,
default_query_timeout_secs, ConnectionConfig, DatabaseType,
};
use dbx_core::storage::Storage;
use dbx_core::transfer::{
TransferContent, TransferMode, TransferOwnershipPolicy, TransferRequest, TransferTableNameCase,
};
fn sqlite_config(id: &str, path: &str) -> ConnectionConfig {
ConnectionConfig {
id: id.to_string(),
name: "SQLite".to_string(),
note: String::new(),
db_type: DatabaseType::Sqlite,
driver_profile: None,
driver_label: None,
url_params: None,
agent_java_options: Vec::new(),
host: path.to_string(),
port: 0,
username: String::new(),
password: String::new(),
database: None,
visible_databases: None,
visible_schemas: None,
show_system_schemas: false,
attached_databases: Vec::new(),
init_script: None,
color: None,
transport_layers: Vec::new(),
connect_timeout_secs: default_connect_timeout_secs(),
query_timeout_secs: default_query_timeout_secs(),
idle_timeout_secs: default_idle_timeout_secs(),
keepalive_interval_secs: default_keepalive_interval_secs(),
ssl: false,
ca_cert_path: String::new(),
client_cert_path: String::new(),
client_key_path: String::new(),
sysdba: false,
oracle_connection_type: None,
connection_string: None,
redis_connection_mode: None,
redis_sentinel_master: String::new(),
redis_sentinel_nodes: String::new(),
redis_sentinel_username: String::new(),
redis_sentinel_password: String::new(),
redis_sentinel_tls: false,
redis_cluster_nodes: String::new(),
redis_key_separator: dbx_core::models::connection::default_redis_key_separator(),
redis_scan_page_size: None,
redis_database_aliases: Default::default(),
etcd_endpoints: String::new(),
gbase_server: String::new(),
informix_server: String::new(),
external_config: None,
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
read_only: false,
is_production: false,
production_databases: vec![],
database_info: None,
}
}
async fn test_web_state() -> (Arc<WebState>, std::path::PathBuf) {
let dir = std::env::temp_dir().join(format!("dbx-web-transfer-test-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&dir).unwrap();
let storage = Storage::open(&dir.join("storage.db")).await.unwrap();
let app = Arc::new(AppState::new_with_plugin_dir(storage, dir.join("plugins")));
let state = Arc::new(WebState::for_tests(app, dir.clone()));
(state, dir)
}
fn transfer_request(source: &str, target: &str, dir: &std::path::Path) -> TransferRequest {
let db = dir.join("main.db").to_string_lossy().to_string();
TransferRequest {
transfer_id: "transfer-entry-test".to_string(),
source_connection_id: source.to_string(),
source_database: db.clone(),
source_schema: "main".to_string(),
source_catalog: None,
target_connection_id: target.to_string(),
target_database: db,
target_schema: "main".to_string(),
target_catalog: None,
tables: Vec::new(),
create_table: false,
content: TransferContent::DataOnly,
objects: Vec::new(),
mode: TransferMode::Append,
target_table_name_case: TransferTableNameCase::Preserve,
ownership_policy: TransferOwnershipPolicy::Preserve,
batch_size: 1000,
}
}
#[tokio::test]
async fn data_only_empty_object_selection_completes_through_the_core_noop() {
let (state, dir) = test_web_state().await;
let src = sqlite_config("src", &dir.join("src.db").to_string_lossy());
let dst = sqlite_config("dst", &dir.join("dst.db").to_string_lossy());
// SQLite pools need the backing files to exist before connecting.
std::fs::write(dir.join("src.db"), b"").unwrap();
std::fs::write(dir.join("dst.db"), b"").unwrap();
std::fs::write(dir.join("main.db"), b"").unwrap();
state.app.configs.write().await.insert("src".to_string(), src);
state.app.configs.write().await.insert("dst".to_string(), dst);
let req = transfer_request("src", "dst", &dir);
let transfer_id = req.transfer_id.clone();
let response = start_transfer(State(state.clone()), Json(StartTransferRequest { request: req })).await.unwrap();
let _ = response.into_response();
// Both HTTP and Tauri call the same Core schema-object stage
// unconditionally. DataOnly must resolve there to a no-op without an
// object progress event or database-family fallback.
let channel = {
let channels = state.transfer_progress_channels.read().await;
channels.get(&transfer_id).cloned().expect("transfer channel registered")
};
let mut saw_terminal = false;
let mut saw_schema_objects = false;
let mut terminal_error: Option<String> = None;
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(15);
while !saw_terminal {
if let Some(data) = channel.latest() {
let value: serde_json::Value = serde_json::from_str(&data).unwrap();
if value["table"].as_str() == Some("schema objects") {
saw_schema_objects = true;
}
if value["terminal"].as_bool() == Some(true) {
saw_terminal = true;
terminal_error = value["error"].as_str().map(|s| s.to_string());
}
}
if saw_terminal {
break;
}
if std::time::Instant::now() > deadline {
panic!("transfer did not reach a terminal event within 15s");
}
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
}
assert!(saw_terminal, "transfer must reach a terminal event");
assert!(!saw_schema_objects, "DataOnly must not transfer schema objects");
assert!(
terminal_error.is_none(),
"DataOnly must complete without a schema-object error, got: {terminal_error:?}"
);
let _ = std::fs::remove_dir_all(&dir);
}
}
+7
View File
@@ -67,6 +67,13 @@ impl TransferProgressChannel {
let _ = self.tx.send(data); let _ = self.tx.send(data);
} }
/// The most recently sent progress payload, if any. Used by tests to wait
/// for a terminal event without subscribing to the live stream.
#[cfg(test)]
pub fn latest(&self) -> Option<String> {
self.history.lock().unwrap_or_else(|poisoned| poisoned.into_inner()).latest.clone()
}
fn subscribe(&self) -> (Vec<String>, usize, broadcast::Receiver<String>) { fn subscribe(&self) -> (Vec<String>, usize, broadcast::Receiver<String>) {
// Subscribe while holding the history lock so an event is either in the // Subscribe while holding the history lock so an event is either in the
// replay snapshot or in the live receiver, with no gap between them. // replay snapshot or in the live receiver, with no gap between them.
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,171 @@
# Data Transfer: Tree Object Selection and Transfer Modes Design
Date: 2026-08-01
## Summary
The data transfer dialog (`apps/desktop/src/components/transfer/DataTransferDialog.vue`) currently lets the user pick tables from a flat checkbox list and copy table **data** between connections. Non-table objects (views, stored procedures, functions, triggers, sequences, events) are only copied for PostgreSQL→PostgreSQL transfers, and then only as an all-or-nothing schema sweep performed after the table data copy.
This change turns the table picker into a tree that groups selectable objects by type (tables, views, stored procedures, functions, triggers, sequences, events), and adds an explicit transfer mode: **structure and data** (default), **structure only**, and **data only**. Non-table object DDL copying is implemented for PostgreSQL and PG-family databases (Kingbase, GaussDB, KWDB, OpenGauss), MySQL, Dameng, and Oracle. Cross-database-family object DDL copy is out of scope for the first version.
## Goals
- Select transfer content via a tree grouped by object type, with per-group select-all and object-level checkboxes.
- Support three transfer modes: structure and data, structure only, data only.
- Copy non-table object DDL for PostgreSQL/PG-family, MySQL, Dameng, and Oracle, within the same database family.
- Skip objects that already exist on the target side (by name), and report the skip count on completion.
- Preserve the existing table data transfer behavior (append/overwrite/upsert write modes, FK dependency ordering, ownership preview, target-table-name case handling).
## Non-goals
- Cross-family non-table object DDL transfer (e.g., MySQL view → PostgreSQL view). The tree disables non-table groups with a hint in that case.
- Object transfer for databases outside the supported family set (SQLite, ClickHouse, SQL Server, MongoDB, Hive, Spark, etc.).
- Editing object DDL inside the transfer dialog (users use the object source editor for that).
- Differential/sync behavior beyond "skip existing" (no replacement of changed objects).
## Existing Design to Reuse
- `crates/dbx-core/src/transfer.rs` — table data transfer core: `TransferRequest`, `transfer_table`, FK dependency sorting (`sort_tables_by_fk_dependency`), progress reporting (`TransferProgress`), DDL generation (`generate_create_table_ddl`), column type mapping (`map_column_type`).
- `crates/dbx-core/src/transfer.rs:5020` — `transfer_postgres_schema_objects`: existing PostgreSQL schema object DDL copy (views, materialized views, procedures, functions, sequences, triggers, types, packages, synonyms + ownership/grants/policies). Reused and extended with per-object selection filtering.
- `crates/dbx-core/src/object_source_sql.rs` — `build_executable_object_source_statements`, `ObjectSourceKind`, `EditableObjectSourceSqlInput`: per-database object source DDL machinery (already used by the PG object transfer).
- `crates/dbx-web/src/routes/transfer.rs` — transfer orchestration route, progress channel, per-object failure handling.
- `apps/desktop/src/lib/backend/api.ts` — `listObjects(connectionId, database, schema, objectTypes?, filter?, limit?, offset?, catalog?)` returning `ObjectInfo[]`; `TransferRequest` types.
- `apps/desktop/src/lib/database/databaseObjectCapabilities.ts` — per-database sidebar object kind lists.
## Design
### 1. Backend protocol (`crates/dbx-core/src/transfer.rs`, `crates/dbx-web/src/routes/transfer.rs`)
Extend `TransferRequest`:
```rust
#[serde(rename_all = "camelCase")]
pub enum TransferContent {
#[default]
StructureAndData,
StructureOnly,
DataOnly,
}
#[serde(rename_all = "SCREAMING_SNAKE_CASE")] // matches SidebarObjectKind conventions
pub enum TransferObjectKind {
Table, View, MaterializedView, Procedure, Function, Trigger, Sequence, Event,
}
pub struct TransferObjectSelection {
pub object_type: TransferObjectKind,
pub names: Vec<String>,
}
```
New fields on `TransferRequest`:
- `content: TransferContent` — defaults to `StructureAndData`. Replaces the `create_table` boolean (`create_table == (content != DataOnly)`); the field is kept for wire compatibility but the frontend stops sending it.
- `objects: Vec<TransferObjectSelection>` — non-table object selections. The table selection stays in the existing `tables: Vec<String>` field (also mirrored as a `TransferObjectKind::Table` entry when the tree sends it).
Validation (in the route, before transfer starts):
- `DataOnly` with non-empty `objects` → error "仅数据模式不传输非表对象".
- `StructureOnly` with empty `tables` and empty `objects` → error (nothing to do).
- Non-table objects are rejected when source and target database families differ (see family rules below).
- Non-table object names are validated like table names (non-empty, no NUL bytes).
`TransferMode` (append/overwrite/upsert) is unchanged; it only takes effect when the content includes data (`StructureAndData` or `DataOnly`).
### 2. Database family rules
| Family | Database types | Non-table object DDL support |
|---|---|---|
| mysql | mysql | yes (views, procedures, functions, triggers, events) |
| postgres | postgres, kingbase, gaussdb, kwdb, opengauss | yes |
| oracle | oracle, dameng, oceanbase-oracle | yes (views, procedures, functions, triggers, sequences) |
| other | everything else | no — tree groups disabled, backend rejects |
`is_same_transfer_family(source, target)` — a new pure function in `transfer.rs`, unit-tested. Cross-family transfers keep table data copy working exactly as today, with non-table groups disabled in the UI.
Object type availability per family (drives which tree groups render):
| Object kind | mysql | postgres family | oracle/dameng |
|---|---|---|---|
| TABLE | ✓ | ✓ | ✓ |
| VIEW | ✓ | ✓ | ✓ |
| MATERIALIZED_VIEW | ✗ | ✓ | ✓ |
| PROCEDURE | ✓ | ✓ | ✓ |
| FUNCTION | ✓ | ✓ | ✓ |
| TRIGGER | ✓ | ✓ | ✓ |
| SEQUENCE | ✗ (MariaDB only, first version: no) | ✓ | ✓ |
| EVENT | ✓ | ✗ | ✗ |
### 3. Object listing (backend)
Extend the existing `listObjects` path (per-database object listing in `crates/dbx-core/src/schema.rs` and the agent-side listing) so the transfer tree can fetch objects grouped by type:
- **PostgreSQL family**: already supported kinds (TABLE, VIEW, MATERIALIZED_VIEW, PROCEDURE, FUNCTION, SEQUENCE) via `pg_catalog`; add TRIGGER listing (`pg_trigger` join `pg_class`/`pg_namespace`, names of triggers in the schema).
- **MySQL**: add TRIGGER (`information_schema.TRIGGERS` filtered by `EVENT_OBJECT_SCHEMA`) and EVENT (`information_schema.EVENTS`); VIEW/PROCEDURE/FUNCTION listing already exists.
- **Oracle/Dameng**: add TRIGGER and SEQUENCE (`ALL_OBJECTS` filtered by `OBJECT_TYPE`); VIEW/PROCEDURE/FUNCTION listing already exists (Dameng already has `SYS.SYSOBJECTS`-based listing).
The `Event` kind is transfer-only: it is added to the backend `ObjectSourceKind`/listing mapping but not to the sidebar object capability lists (the sidebar does not gain new node types in this change).
### 4. Non-table object DDL copy (backend)
New `transfer_objects` flow in `crates/dbx-core/src/transfer.rs`, invoked from the route after table data transfer and after the existing PG schema-dependency step. Execution order:
1. tables (existing flow, FK sorted)
2. sequences
3. views, materialized views
4. functions
5. procedures
6. triggers
7. events
DDL fetching per family:
- **PostgreSQL family**: reuse `get_postgres_schema_object_sources_for_transfer` + `get_postgres_materialized_view_sources_for_transfer` + new `get_postgres_trigger_sources_for_transfer`-style queries, filtered by the selected names. Existing schema rewrite (`rewrite_postgres_routine_schema`) and `build_executable_object_source_statements` apply. The current "transfer everything in the schema" behavior becomes "transfer only selected objects"; when the request carries no object selection the step is skipped entirely (no behavior surprise for table-only transfers).
- **MySQL**: VIEW/PROCEDURE/FUNCTION DDL via `SHOW CREATE` (existing `mysql_object_source_sql`); TRIGGER DDL assembled from `information_schema.TRIGGERS` (`CREATE TRIGGER` with ACTION_TIMING/EVENT_MANIPULATION/EVENT_OBJECT_TABLE/ACTION_STATEMENT — the per-table trigger query at `crates/dbx-core/src/db/mysql.rs:4239` is the basis); EVENT DDL assembled from `information_schema.EVENTS`; strip `DEFINER=` clauses; rewrite the schema qualifier from source schema to target schema when they differ.
- **Oracle/Dameng**: fetch DDL via `DBMS_METADATA.GET_DDL('<TYPE>', '<name>')` (the existing object-source machinery already implements per-type metadata DDL for these databases; reuse those code paths where possible); rewrite schema qualifiers; execute per statement.
Target-side existence check, per object, immediately before executing its DDL:
- **PostgreSQL family**: `SELECT 1 FROM pg_class/pg_proc ...` by schema+name.
- **MySQL**: `information_schema` lookups (VIEWS, ROUTINES, TRIGGERS, EVENTS).
- **Oracle/Dameng**: `ALL_OBJECTS` (or `SYS.SYSOBJECTS` for Dameng) by schema+name.
Existing objects are skipped and counted; the count is included in the final summary progress message ("skipped N existing objects"). Failed objects follow the existing pattern: recorded in the failure list, transfer continues, final status reflects the failures.
Progress: reuse `TransferProgress` with `table = "schema object: <name>"` (already the convention used by the PG object transfer). The `DataOnly` mode skips the whole object-DDL step.
### 5. Frontend (`apps/desktop/src/components/transfer/DataTransferDialog.vue`)
- Replace the flat table list with a tree:
- Top-level groups rendered from the source database family capability table (tables, views, materialized views, procedures, functions, triggers, sequences, events as applicable).
- Group header checkbox = select all / clear group; per-object checkboxes; expand/collapse per group; a search filter box filters object names across groups.
- Groups that cannot be transferred (cross-family, unsupported kind, or `DataOnly` mode) are rendered disabled with a tooltip explaining why.
- Table selection stays in `tables`; non-table selections serialize into `objects`.
- Transfer mode selector: 结构和数据 / 仅结构 / 仅数据 (i18n keys in the `transfer.*` namespace).
- `DataOnly`: non-table groups disabled ("仅数据模式不传输非表对象"); table group enabled.
- `StructureOnly`: data write mode (append/overwrite/upsert) selector hidden/disabled; ownership preview and target-table-name case options only apply when data is copied (ownership preview only relevant with `StructureAndData`; keep the existing behavior for that mode).
- The `createTable` checkbox is removed.
- Load object lists per group from `listObjects` with the per-kind object type filter; loading states per group; error states surface inline.
### 6. Error handling
- Validation errors from the route are shown inline in the dialog (existing error display pattern).
- Per-object DDL failures: existing `failed_tables` mechanism extended to carry object entries; final status `Error` with the failure list, transfer continues for remaining objects.
- Cancellation: existing `transfer_id` cancellation checks are threaded through the object-DDL loop (mirroring the PG object transfer loop).
### 7. Testing
Backend unit tests (`crates/dbx-core/src/transfer.rs` test module + `crates/dbx-core/tests/live_postgres_transfer.rs`):
- `is_same_transfer_family` matrix.
- Request validation (DataOnly + objects, cross-family objects).
- Per-family object DDL statement generation (MySQL `SHOW CREATE` handling with DEFINER stripping; schema rewriting).
- Target existence check SQL per family.
- Live PG transfer: object filtering by selection, skip-existing behavior, `StructureOnly` (no data rows copied), `DataOnly` (no DDL executed).
Frontend tests (`apps/desktop/src/components/transfer/__tests__/`):
- Tree group rendering per database family.
- Group select-all, object toggle, search filtering.
- Disable rules: `DataOnly` mode, cross-family, unsupported kinds.
- Request serialization (`tables` + `objects` + `content`).
+81 -52
View File
@@ -4,6 +4,7 @@ use tauri::{AppHandle, Emitter, State};
use crate::commands::connection::{ensure_connection_writable, AppState}; use crate::commands::connection::{ensure_connection_writable, AppState};
// Re-export types and functions used by other modules // Re-export types and functions used by other modules
use dbx_core::models::connection::DatabaseType;
pub use dbx_core::transfer::{ pub use dbx_core::transfer::{
get_db_type, TransferOwnershipPreview, TransferProgress, TransferRequest, TransferStatus, get_db_type, TransferOwnershipPreview, TransferProgress, TransferRequest, TransferStatus,
}; };
@@ -27,7 +28,16 @@ pub async fn start_transfer(
// Validate connections exist // Validate connections exist
let source_db_type = get_db_type(&state, &request.source_connection_id).await?; let source_db_type = get_db_type(&state, &request.source_connection_id).await?;
let target_db_type = get_db_type(&state, &request.target_connection_id).await?; let target_db_type = get_db_type(&state, &request.target_connection_id).await?;
dbx_core::transfer::validate_transfer_target_table_names(&request)?; dbx_core::transfer::validate_transfer_request(&request)?;
// Cross-family object transfers are validated inside transfer_schema_objects:
// only mechanically rewriteable kinds (views, sequences) are allowed.
// Structure-only data transfer is unsupported for MongoDB.
if matches!(request.content, dbx_core::transfer::TransferContent::StructureOnly)
&& (matches!(source_db_type, DatabaseType::MongoDb) || matches!(target_db_type, DatabaseType::MongoDb))
{
return Err("MongoDB 暂不支持仅结构传输".to_string());
}
// External Doris/StarRocks catalogs: pool is created with `catalog=` URL // External Doris/StarRocks catalogs: pool is created with `catalog=` URL
// setup (SET catalog) and without USE <external-db>. See ensure_transfer_pool. // setup (SET catalog) and without USE <external-db>. See ensure_transfer_pool.
@@ -234,56 +244,70 @@ pub async fn start_transfer(
} }
} }
if matches!(source_db_type, dbx_core::models::connection::DatabaseType::Postgres) // Transfer selected non-table objects (views, procedures, functions,
&& matches!(target_db_type, dbx_core::models::connection::DatabaseType::Postgres) // triggers, sequences, events) after the per-table loop. The shared
// Core decision handles all content modes: DataOnly never
// transfers schema objects; PG→PG keeps the legacy empty-selection
// default only when structure participates in the transfer.
let mut object_outcome = dbx_core::transfer::TransferObjectOutcome::default();
match dbx_core::transfer::transfer_schema_objects(
&state,
&request,
&source_pool_key,
&target_pool_key,
|progress| emit_progress(&app, progress),
)
.await
{ {
match dbx_core::transfer::transfer_postgres_schema_objects( Ok(outcome) => {
&state, object_outcome = outcome;
&request, }
&source_pool_key, Err(e) if e == "Cancelled" => {
&target_pool_key, emit_progress(
|progress| emit_progress(&app, progress), &app,
) TransferProgress {
.await transfer_id: transfer_id.clone(),
{ table: "schema objects".to_string(),
Ok(()) => {} table_index: total_tables,
Err(e) if e == "Cancelled" => { total_tables,
emit_progress( rows_transferred: 0,
&app, total_rows: None,
TransferProgress { status: TransferStatus::Cancelled,
transfer_id: transfer_id.clone(), error: None,
table: "schema objects".to_string(), terminal: true,
table_index: total_tables, },
total_tables, );
rows_transferred: 0, dbx_core::transfer::clear_cancelled(&transfer_id).await;
total_rows: None, return;
status: TransferStatus::Cancelled, }
error: None, Err(e) => {
terminal: true, failed_tables.push("schema objects".to_string());
}, emit_progress(
); &app,
dbx_core::transfer::clear_cancelled(&transfer_id).await; TransferProgress {
return; transfer_id: transfer_id.clone(),
} table: "schema objects".to_string(),
Err(e) => { table_index: total_tables,
failed_tables.push("schema objects".to_string()); total_tables,
emit_progress( rows_transferred: 0,
&app, total_rows: None,
TransferProgress { status: TransferStatus::Error,
transfer_id: transfer_id.clone(), error: Some(e),
table: "schema objects".to_string(), terminal: false,
table_index: total_tables, },
total_tables, );
rows_transferred: 0,
total_rows: None,
status: TransferStatus::Error,
error: Some(e),
terminal: false,
},
);
}
} }
} }
if !object_outcome.failed.is_empty() {
failed_tables.push(format!("schema objects ({})", object_outcome.failed.len()));
}
let skip_suffix = if !object_outcome.skipped.is_empty() && failed_tables.is_empty() {
format!(",跳过 {} 个已存在对象", object_outcome.skipped.len())
} else if !object_outcome.skipped.is_empty() {
format!(";跳过 {} 个已存在对象", object_outcome.skipped.len())
} else {
String::new()
};
emit_progress( emit_progress(
&app, &app,
@@ -296,12 +320,17 @@ pub async fn start_transfer(
total_rows: None, total_rows: None,
status: if failed_tables.is_empty() { TransferStatus::Done } else { TransferStatus::Error }, status: if failed_tables.is_empty() { TransferStatus::Done } else { TransferStatus::Error },
error: if failed_tables.is_empty() { error: if failed_tables.is_empty() {
None if skip_suffix.is_empty() {
None
} else {
Some(skip_suffix.clone())
}
} else { } else {
Some(format!( Some(format!(
"{} table(s) failed: {}", "{} table(s) failed: {}{}",
failed_tables.len(), failed_tables.len(),
failed_tables.iter().take(5).cloned().collect::<Vec<_>>().join(", ") failed_tables.iter().take(5).cloned().collect::<Vec<_>>().join(", "),
skip_suffix
)) ))
}, },
terminal: true, terminal: true,
@@ -321,7 +350,7 @@ pub async fn preview_transfer_ownership(
let state = state.inner().clone(); let state = state.inner().clone();
let source_db_type = get_db_type(&state, &request.source_connection_id).await?; let source_db_type = get_db_type(&state, &request.source_connection_id).await?;
let target_db_type = get_db_type(&state, &request.target_connection_id).await?; let target_db_type = get_db_type(&state, &request.target_connection_id).await?;
dbx_core::transfer::validate_transfer_target_table_names(&request)?; dbx_core::transfer::validate_transfer_request(&request)?;
let source_pool_key = dbx_core::transfer::ensure_transfer_pool( let source_pool_key = dbx_core::transfer::ensure_transfer_pool(
&state, &state,
&request.source_connection_id, &request.source_connection_id,