fix(structure): load editor on open

This commit is contained in:
t8y2 2026-05-19 12:58:10 +08:00
parent 329664bfb8
commit 5b3b69f7fb
4 changed files with 52 additions and 12 deletions

View File

@ -39,7 +39,12 @@ import {
type EditableStructureIndex,
} from "@/lib/tableStructureEditorSql";
import { getTableStructureCapabilities } from "@/lib/tableStructureCapabilities";
import { createColumnDrafts, createIndexDrafts, toColumnNames } from "@/lib/tableStructureEditorState";
import {
buildStructureTargetLabel,
createColumnDrafts,
createIndexDrafts,
toColumnNames,
} from "@/lib/tableStructureEditorState";
import type { ForeignKeyInfo, TriggerInfo } from "@/types/database";
import * as api from "@/lib/api";
@ -116,11 +121,14 @@ const indexColLabels = computed(() => [
const targetSchema = computed(() => props.prefillSchema || props.prefillDatabase || "");
const isCreateMode = computed(() => !props.prefillTable);
const newTableName = ref("");
const targetLabel = computed(() => {
const parts = [connection.value?.name, props.prefillDatabase, props.prefillSchema];
if (!isCreateMode.value) parts.push(props.prefillTable);
return parts.filter(Boolean).join(" / ");
});
const targetLabel = computed(() =>
buildStructureTargetLabel(
connection.value?.name,
props.prefillDatabase,
props.prefillSchema,
isCreateMode.value ? undefined : props.prefillTable,
),
);
const changeSql = computed(() => {
if (isCreateMode.value) {
@ -331,12 +339,16 @@ async function applyChanges() {
}
}
watch(open, (value) => {
if (value) {
resetState();
void loadStructure();
}
});
watch(
open,
(value) => {
if (value) {
resetState();
void loadStructure();
}
},
{ immediate: true },
);
</script>
<template>

View File

@ -34,3 +34,15 @@ export function createIndexDrafts(indexes: IndexInfo[]): EditableStructureIndex[
export function toColumnNames(columns: string[]): string {
return columns.join(", ");
}
export function buildStructureTargetLabel(
connectionName: string | undefined,
database: string | undefined,
schema: string | undefined,
tableName: string | undefined,
): string {
const parts = [connectionName, database];
if (schema && schema !== database) parts.push(schema);
if (tableName) parts.push(tableName);
return parts.filter(Boolean).join(" / ");
}

View File

@ -18,6 +18,10 @@ test("structure editor keeps columns when optional metadata fails", () => {
assert.match(source, /api\s*\n\s*\.listTriggers[\s\S]*\.catch\(\(\) => \[\]\)/);
});
test("structure editor loads immediately when mounted open", () => {
assert.match(source, /watch\(\s*open,[\s\S]*\{\s*immediate:\s*true\s*\},?\s*\)/);
});
test("structure editor gates controls through table structure capabilities", () => {
assert.match(source, /getTableStructureCapabilities/);
assert.match(source, /const structureCapabilities = computed/);

View File

@ -1,6 +1,7 @@
import assert from "node:assert/strict";
import test from "node:test";
import {
buildStructureTargetLabel,
createColumnDrafts,
createIndexDrafts,
toColumnNames,
@ -102,3 +103,14 @@ test("creates editable index drafts and splits pasted column lists", () => {
]);
assert.equal(toColumnNames(["id", "name"]), "id, name");
});
test("structure editor target label omits duplicate database and schema", () => {
assert.equal(
buildStructureTargetLabel("online-clickhouse", "testdb", "testdb", "users"),
"online-clickhouse / testdb / users",
);
assert.equal(
buildStructureTargetLabel("online-postgres", "app", "public", "users"),
"online-postgres / app / public / users",
);
});