feat(structure): show table constraints for postgres-compatible dbs

This commit is contained in:
amwps290 authored and GitHub committed 2026-08-28 12:14:04 +08:00
1 parent 7ad05b178f
commit 20a2994fa1
37 files changed
+2725 -156

No files matched your search

+118 -2
View File
@@ -16,6 +16,7 @@ import {
Search,
Inbox,
SearchX,
ShieldCheck,
Code2,
Copy,
Loader2,
@@ -78,7 +79,7 @@ import TemporalCellEditor from "@/components/grid/TemporalCellEditor.vue";
import EnumCellEditor from "@/components/grid/EnumCellEditor.vue";
import DataGridReadonlyTextSelection from "@/components/grid/DataGridReadonlyTextSelection.vue";
import GridSnapshotDialog from "@/components/grid/GridSnapshotDialog.vue";
import type { QueryResult, ColumnInfo, DatabaseType, ForeignKeyInfo, IndexInfo, ReferenceKeyInfo, TriggerInfo, TableInfoTab, QueryResultSourceColumnRef } from "@/types/database";
import type { QueryResult, ColumnInfo, ConstraintInfo, DatabaseType, ForeignKeyInfo, IndexInfo, ReferenceKeyInfo, TriggerInfo, TableInfoTab, QueryResultSourceColumnRef } from "@/types/database";
import { isQueryExecutionErrorResult } from "@/lib/query/queryResultError";
import { tableObjectSourceKind } from "@/lib/table/tableObjectSourceKind";
import { tableColumnDefaultDisplayValue } from "@/lib/table/tableColumnDefaultPresentation";
@@ -345,6 +346,7 @@ import {
} from "@/lib/dataGrid/dataGridToolbar";
import { getTableMetadataCapabilities } from "@/lib/table/tableMetadataCapabilities";
import { getTableStructureCapabilities } from "@/lib/table/tableStructureCapabilities";
import { constraintsForConstraintsTab } from "@/lib/table/constraintPresentation";
import { filterObjectBrowserTableColumns } from "@/lib/table/objectBrowserTableInfo";
import { gaussdbMTypeDisplayName } from "@/lib/table/postgresDataTypeHelp";
import { reserveDataGridHeaderLine } from "@/lib/dataGrid/dataGridHeaderLayout";
@@ -10638,6 +10640,23 @@ const triggers = ref<TriggerInfo[]>([]);
const triggersLoaded = ref(false);
const triggersLoading = ref(false);
const triggersError = ref("");
const constraints = ref<ConstraintInfo[]>([]);
const constraintsLoaded = ref(false);
const constraintsLoading = ref(false);
const constraintsError = ref("");
const currentConstraintTableIdentity = computed(() =>
columnIndexTableIdentity({
connectionId: props.connectionId,
database: props.database,
catalog: props.tableMeta?.catalog,
schema: props.tableMeta?.schema,
tableName: props.tableMeta?.tableName,
}),
);
let constraintsRequestGeneration = 0;
// The Constraints tab hides foreign keys when a dedicated Foreign Keys tab is
// also shown (each constraint appears once; FK navigation stays in that tab).
const constraintsForTab = computed(() => constraintsForConstraintsTab(constraints.value, tableMetadataCapabilities.value.foreignKeys));
const searchQuery = ref("");
const cellDetailPanelLayout = computed(() => settingsStore.editorSettings.cellDetailPanelLayout);
const cellDetailPanelIsBottom = computed(() => cellDetailPanelLayout.value === "bottom");
@@ -10723,7 +10742,7 @@ function toggleCellDetailPanelLayout() {
});
}
const tableMetadataCapabilities = computed(() => getTableMetadataCapabilities(props.databaseType));
const tableMetadataCapabilities = computed(() => getTableMetadataCapabilities(resolvedDatabaseType.value));
const canOpenTableStructureEditor = computed(() => !!props.connectionId && !!props.database && !!props.tableMeta?.tableName && supportsTableStructureEditing(resolvedDatabaseType.value));
const mongoConnectionConfig = resolvedConnectionConfig;
const canManageMongoIndexes = computed(() => resolvedDatabaseType.value === "mongodb" && !!props.connectionId && !!props.database && !!props.tableMeta?.tableName && supportsMongoIndexMutations(mongoConnectionConfig.value, props.tableMeta?.tableType));
@@ -10757,6 +10776,14 @@ const tableInfoTabs = computed(() => {
count: foreignKeys.value.length,
});
}
if (tableMetadataCapabilities.value.constraints) {
tabs.push({
id: "constraints",
label: t("grid.tableInfoConstraints"),
icon: ShieldCheck,
count: constraintsForTab.value.length,
});
}
if (tableMetadataCapabilities.value.triggers) {
tabs.push({
id: "triggers",
@@ -10792,6 +10819,7 @@ async function selectTableInfoTab(tab: TableInfoTab) {
if (nextTab === "ddl") await fetchDdl();
else if (nextTab === "indexes") await fetchIndexes();
else if (nextTab === "foreignKeys") await fetchForeignKeys();
else if (nextTab === "constraints") await fetchConstraints();
else if (nextTab === "triggers") await fetchTriggers();
}
@@ -10954,6 +10982,54 @@ async function fetchTriggers() {
}
}
async function fetchConstraints() {
const requestIdentity = currentConstraintTableIdentity.value;
if (!props.connectionId || !props.tableMeta || !requestIdentity || constraintsLoaded.value || constraintsLoading.value) return;
const connectionId = props.connectionId;
const database = props.database || "";
const schema = props.tableMeta.schema || props.database || "";
const tableName = props.tableMeta.tableName;
const catalog = props.tableMeta.catalog;
const requestGeneration = ++constraintsRequestGeneration;
constraintsLoading.value = true;
constraintsError.value = "";
try {
const nextConstraints = await api.listConstraints(connectionId, database, schema, tableName, catalog);
if (
!columnIndexMetadataRequestCurrent({
requestGeneration,
currentGeneration: constraintsRequestGeneration,
requestIdentity,
currentIdentity: currentConstraintTableIdentity.value,
})
)
return;
constraints.value = nextConstraints;
constraintsLoaded.value = true;
} catch (e: any) {
if (
!columnIndexMetadataRequestCurrent({
requestGeneration,
currentGeneration: constraintsRequestGeneration,
requestIdentity,
currentIdentity: currentConstraintTableIdentity.value,
})
)
return;
constraintsError.value = String(e?.message || e);
} finally {
if (
columnIndexMetadataRequestCurrent({
requestGeneration,
currentGeneration: constraintsRequestGeneration,
requestIdentity,
currentIdentity: currentConstraintTableIdentity.value,
})
)
constraintsLoading.value = false;
}
}
watch(
() => [props.connectionId, props.database, props.tableMeta?.catalog, props.tableMeta?.schema, props.tableMeta?.tableName],
() => {
@@ -10975,6 +11051,11 @@ watch(
triggers.value = [];
triggersLoaded.value = false;
triggersError.value = "";
constraints.value = [];
constraintsLoaded.value = false;
constraintsLoading.value = false;
constraintsError.value = "";
constraintsRequestGeneration += 1;
// 表身份变更后,主动触发索引加载,确保索引指示器在切换表后立即可见
if (showIndexIndicatorsInHeader.value && canShowTableIndexes.value && currentIndexTableIdentity.value) {
void fetchIndexes();
@@ -11573,6 +11654,13 @@ const filteredTriggers = computed(() => {
return triggers.value.filter((t) => t.name.toLowerCase().includes(q));
});
const filteredConstraints = computed(() => {
const base = constraintsForTab.value;
if (!searchQuery.value) return base;
const q = searchQuery.value.toLowerCase();
return base.filter((c) => c.name.toLowerCase().includes(q) || c.constraint_type.toLowerCase().includes(q) || c.columns.some((col) => col.toLowerCase().includes(q)) || c.definition.toLowerCase().includes(q));
});
const filteredDdlContent = computed(() => {
if (!ddlContent.value) return "";
const html = highlight(ddlContent.value);
@@ -13754,6 +13842,34 @@ function openGridSnapshot() {
</div>
</div>
<div v-else-if="activeTableInfoTab === 'constraints'" class="flex-1 min-h-0 overflow-auto">
<div v-if="constraintsLoading" class="h-full flex items-center justify-center">
<Loader2 class="w-4 h-4 animate-spin text-muted-foreground" />
</div>
<div v-else-if="constraintsError" class="p-3 text-xs text-destructive">
{{ constraintsError }}
</div>
<div v-else-if="searchQuery && filteredConstraints.length === 0" class="p-6 text-center text-xs text-muted-foreground">
{{ t("grid.tableInfoNoResults") }}
</div>
<div v-else-if="constraintsForTab.length === 0" class="p-6 text-center text-xs text-muted-foreground">
{{ t("grid.tableInfoEmpty") }}
</div>
<div v-else class="divide-y">
<div v-for="constraint in filteredConstraints" :key="constraint.name" class="p-3 text-xs" :class="constraint.enabled ? '' : 'opacity-60'">
<div class="flex flex-wrap items-center gap-1.5">
<span class="font-medium truncate">{{ constraint.name }}</span>
<span class="rounded border px-1 py-px text-[10px] text-muted-foreground">{{ constraint.constraint_type }}</span>
<span v-if="!constraint.enabled" class="rounded border px-1 py-px text-[10px] text-muted-foreground">{{ t("grid.tableInfoConstraintDisabled") }}</span>
<span v-else-if="!constraint.valid" class="rounded border px-1 py-px text-[10px] text-muted-foreground">{{ t("grid.tableInfoConstraintNotValidated") }}</span>
</div>
<div v-if="constraint.columns.length" class="mt-1 font-mono text-[11px] text-muted-foreground break-all">{{ constraint.columns.join(", ") }}</div>
<div v-if="constraint.ref_table" class="mt-1 font-mono text-[11px] text-muted-foreground break-all">-> {{ constraint.ref_schema ? `${constraint.ref_schema}.` : "" }}{{ constraint.ref_table }}{{ constraint.ref_columns.length ? `(${constraint.ref_columns.join(", ")})` : "" }}</div>
<div v-if="constraint.definition" class="mt-1 font-mono text-[11px] text-muted-foreground break-all whitespace-pre-wrap">{{ constraint.definition }}</div>
</div>
</div>
</div>
<pre
v-else-if="activeTableInfoTab === 'ddl' && !ddlLoading"
ref="ddlPreRef"