fix(sidebar): store full selection as unfiltered visible databases
This commit is contained in:
5 files changed
+170
-5
No files matched your search
@@ -7,6 +7,7 @@ import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { useConnectionStore } from "@/stores/connectionStore";
|
||||
import { canSaveVisibleDatabaseSelection, connectionUsesVisibleSchemaFilter, databaseNameMatchesVisiblePatterns, filterDatabaseNamesForVisiblePicker, filterSchemaNamesForVisiblePicker, normalizeVisibleDatabaseSelection, parseVisibleDatabasePatternsInput } from "@/lib/database/visibleDatabases";
|
||||
import { resolveVisibleDatabaseSaveAction } from "./visibleDatabasesDialogState";
|
||||
import * as api from "@/lib/backend/api";
|
||||
|
||||
const props = defineProps<{
|
||||
@@ -159,8 +160,27 @@ async function saveSelection() {
|
||||
if (!canSaveSelection.value) return;
|
||||
if (isSchemaFilterMode.value) {
|
||||
await connectionStore.setVisibleSchemas(props.connectionId, databaseKey.value, normalizeVisibleDatabaseSelection([...selectedNames.value], objectNames.value));
|
||||
emit("update:open", false);
|
||||
return;
|
||||
}
|
||||
// 勾选等于"默认可见库"且没有通配符时等价于不筛选:不能存成当时的库名快照,
|
||||
// 否则之后新建的库永远不会出现在侧边栏(用户视角就是"库丢了")。
|
||||
const action = resolveVisibleDatabaseSaveAction({
|
||||
selection: selectedNames.value,
|
||||
allNames: objectNames.value,
|
||||
defaultVisibleNames: defaultObjectNames.value,
|
||||
configured: connection.value?.visible_databases,
|
||||
configuredPatterns: connection.value?.visible_database_patterns,
|
||||
patterns: parsedPatterns.value,
|
||||
});
|
||||
if (action.type === "none") {
|
||||
emit("update:open", false);
|
||||
return;
|
||||
}
|
||||
if (action.type === "clear") {
|
||||
await connectionStore.clearVisibleDatabases(props.connectionId);
|
||||
} else {
|
||||
await connectionStore.setVisibleDatabaseFilter(props.connectionId, [...selectedNames.value], parsedPatterns.value);
|
||||
await connectionStore.setVisibleDatabaseFilter(props.connectionId, action.databaseNames, action.patterns);
|
||||
}
|
||||
emit("update:open", false);
|
||||
}
|
||||
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { resolveVisibleDatabaseSaveAction } from "../visibleDatabasesDialogState";
|
||||
|
||||
const allNames = ["data_upload_mid", "ops", "mysql", "performance_schema", "sys"];
|
||||
const defaultVisibleNames = ["data_upload_mid", "ops"];
|
||||
|
||||
function resolve(selection: string[], overrides: Partial<{ configured: string[] | undefined; configuredPatterns: string[]; patterns: string[] }> = {}) {
|
||||
return resolveVisibleDatabaseSaveAction({
|
||||
selection: new Set(selection),
|
||||
allNames,
|
||||
defaultVisibleNames,
|
||||
configured: "configured" in overrides ? overrides.configured : undefined,
|
||||
configuredPatterns: overrides.configuredPatterns,
|
||||
patterns: overrides.patterns ?? [],
|
||||
});
|
||||
}
|
||||
|
||||
describe("visibleDatabasesDialogState", () => {
|
||||
it("treats a full selection as show-all instead of persisting a snapshot list", () => {
|
||||
expect(resolve([...defaultVisibleNames])).toEqual({ type: "none" });
|
||||
expect(resolve([...defaultVisibleNames], { configured: [...defaultVisibleNames] })).toEqual({ type: "clear" });
|
||||
});
|
||||
|
||||
it("persists a real subset as an explicit list", () => {
|
||||
expect(resolve(["ops"])).toEqual({ type: "set", databaseNames: ["ops"], patterns: [] });
|
||||
expect(resolve(["ops"], { configured: ["ops", "data_upload_mid"] })).toEqual({ type: "set", databaseNames: ["ops"], patterns: [] });
|
||||
});
|
||||
|
||||
it("clears an existing subset once the selection grows back to the full default set", () => {
|
||||
expect(resolve([...defaultVisibleNames], { configured: ["ops"] })).toEqual({ type: "clear" });
|
||||
expect(resolve([...defaultVisibleNames], { configured: ["ops", "mysql"] })).toEqual({ type: "clear" });
|
||||
});
|
||||
|
||||
it("keeps an explicit list when system databases are selected on purpose", () => {
|
||||
expect(resolve(["data_upload_mid", "ops", "mysql"])).toEqual({ type: "set", databaseNames: ["data_upload_mid", "ops", "mysql"], patterns: [] });
|
||||
});
|
||||
|
||||
it("keeps the list while wildcard patterns are active because the two are unioned", () => {
|
||||
expect(resolve([...defaultVisibleNames], { patterns: ["log_%"] })).toEqual({
|
||||
type: "set",
|
||||
databaseNames: [...defaultVisibleNames],
|
||||
patterns: ["log_%"],
|
||||
});
|
||||
});
|
||||
|
||||
it("skips persistence when nothing changed", () => {
|
||||
expect(resolve(["ops"], { configured: ["ops"] })).toEqual({ type: "none" });
|
||||
expect(resolve(["ops"], { configured: ["ops"], configuredPatterns: ["log_%"], patterns: ["log_%"] })).toEqual({ type: "none" });
|
||||
});
|
||||
|
||||
it("writes a changed wildcard filter back", () => {
|
||||
expect(resolve(["ops"], { configured: ["ops"], configuredPatterns: ["log_%"], patterns: ["tmp_%"] })).toEqual({
|
||||
type: "set",
|
||||
databaseNames: ["ops"],
|
||||
patterns: ["tmp_%"],
|
||||
});
|
||||
expect(resolve([...defaultVisibleNames], { configuredPatterns: ["log_%"], patterns: [] })).toEqual({ type: "clear" });
|
||||
});
|
||||
|
||||
it("drops names that no longer exist and ignores blank wildcards", () => {
|
||||
expect(resolve(["ops", "dropped_db"])).toEqual({ type: "set", databaseNames: ["ops"], patterns: [] });
|
||||
expect(resolve([...defaultVisibleNames], { patterns: ["", " "] })).toEqual({ type: "none" });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,66 @@
|
||||
import { normalizeVisibleDatabaseSelection } from "@/lib/database/visibleDatabases";
|
||||
|
||||
export type VisibleDatabaseSaveAction = { type: "none" } | { type: "clear" } | { type: "set"; databaseNames: string[]; patterns: string[] };
|
||||
|
||||
export interface VisibleDatabaseSaveInput {
|
||||
/** 弹窗里当前勾选的库名。 */
|
||||
selection: ReadonlySet<string>;
|
||||
/** 连接上实际存在的全部库名(含系统库),用于规范化与落库。 */
|
||||
allNames: readonly string[];
|
||||
/** 未筛选时默认显示的库名(非系统库)。 */
|
||||
defaultVisibleNames: readonly string[];
|
||||
/** 已保存的 visible_databases。 */
|
||||
configured: readonly string[] | undefined;
|
||||
/** 已保存的 visible_database_patterns。 */
|
||||
configuredPatterns: readonly string[] | undefined;
|
||||
/** 本次要保存的通配符;连接弹窗不改通配符时与 configuredPatterns 相同。 */
|
||||
patterns: readonly string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* 显式名单是白名单语义:名单一旦落库,之后新建的库既不在名单内也不会自动出现。
|
||||
* 如果用户在弹窗里点的其实就是"全选",把它存成"当时那份库名"的快照会让后来新增的库
|
||||
* 永久消失(用户会觉得"我明明没设置过筛选")。因此勾选结果与默认可见集合完全一致、
|
||||
* 且没有通配符时,等价于不筛选,直接按"未筛选"保存。
|
||||
*/
|
||||
export function resolveVisibleDatabaseSaveAction(input: VisibleDatabaseSaveInput): VisibleDatabaseSaveAction {
|
||||
const allNames = [...input.allNames];
|
||||
const selectedNames = normalizeVisibleDatabaseSelection([...input.selection], allNames);
|
||||
const nextPatterns = normalizeVisibleDatabasePatterns(input.patterns);
|
||||
const configuredNames = input.configured ? normalizeVisibleDatabaseSelection([...input.configured], allNames) : undefined;
|
||||
const configuredPatterns = normalizeVisibleDatabasePatterns(input.configuredPatterns ?? []);
|
||||
|
||||
const coversDefaultVisible = nextPatterns.length === 0 && sameSelection(selectedNames, input.defaultVisibleNames);
|
||||
const nextNames = coversDefaultVisible ? undefined : selectedNames;
|
||||
// 通配符存在时不能丢掉名单:两者取并集,名单缺失会让未命中通配符的库一起消失。
|
||||
const nextPatternValues = nextNames === undefined || nextPatterns.length === 0 ? undefined : nextPatterns;
|
||||
|
||||
if (sameOptionalSelection(nextNames, configuredNames) && sameOptionalSelection(nextPatternValues, configuredPatterns.length > 0 ? configuredPatterns : undefined)) {
|
||||
return { type: "none" };
|
||||
}
|
||||
if (nextNames === undefined) return { type: "clear" };
|
||||
return { type: "set", databaseNames: nextNames, patterns: nextPatterns };
|
||||
}
|
||||
|
||||
function normalizeVisibleDatabasePatterns(patterns: readonly string[]): string[] {
|
||||
const seen = new Set<string>();
|
||||
const normalized: string[] = [];
|
||||
for (const pattern of patterns) {
|
||||
const trimmed = pattern.trim();
|
||||
if (!trimmed || seen.has(trimmed)) continue;
|
||||
seen.add(trimmed);
|
||||
normalized.push(trimmed);
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function sameOptionalSelection(left: readonly string[] | undefined, right: readonly string[] | undefined): boolean {
|
||||
if (left === undefined || right === undefined) return left === right;
|
||||
return sameSelection(left, right);
|
||||
}
|
||||
|
||||
function sameSelection(left: readonly string[], right: readonly string[]): boolean {
|
||||
if (left.length !== right.length) return false;
|
||||
const rightSet = new Set(right);
|
||||
return left.every((name) => rightSet.has(name));
|
||||
}
|
||||
Reference in new issue
Block a user