feat(mongodb): rename collections from sidebar

This commit is contained in:
onenewcode authored and GitHub committed 2026-07-21 17:08:27 +08:00
1 parent 809500fd52
commit 9a83b46f00
26 files changed
+1105 -93

No files matched your search

@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createPinia, setActivePinia } from "pinia";
import { executeWithProductionSqlGuard } from "../productionExecutionGuard";
import { executeWithProductionContextGuard, executeWithProductionSqlGuard } from "../productionExecutionGuard";
import { useProductionSafetyStore } from "@/stores/productionSafetyStore";
import type { ConnectionConfig } from "@/types/database";
@@ -117,3 +117,67 @@ describe("production SQL execution guard", () => {
expect(execute).toHaveBeenCalledTimes(1);
});
});
describe("production context execution guard (non-SQL)", () => {
beforeEach(() => {
setActivePinia(createPinia());
});
it("waits for confirmation before executing production non-SQL mutations", async () => {
const execute = vi.fn().mockResolvedValue("renamed");
const reviewText = 'db.getSiblingDB("app").getCollection("users").renameCollection("accounts")';
const pendingExecution = executeWithProductionContextGuard({
connection: connection({ db_type: "mongodb", is_production: true }),
database: "app",
reviewText,
source: "Object tree",
execute,
});
await Promise.resolve();
const store = useProductionSafetyStore();
expect(store.pending).toMatchObject({
sql: reviewText,
database: "app",
source: "Object tree",
});
expect(execute).not.toHaveBeenCalled();
store.confirm();
await expect(pendingExecution).resolves.toBe("renamed");
expect(execute).toHaveBeenCalledTimes(1);
});
it("cancels production non-SQL mutations without executing the callback", async () => {
const execute = vi.fn().mockResolvedValue("renamed");
const pendingExecution = executeWithProductionContextGuard({
connection: connection({ db_type: "mongodb", production_databases: ["app"] }),
database: "app",
reviewText: 'db.getCollection("users").renameCollection("accounts")',
source: "Object tree",
execute,
});
await Promise.resolve();
useProductionSafetyStore().cancel();
await expect(pendingExecution).resolves.toBeUndefined();
expect(execute).not.toHaveBeenCalled();
});
it("executes non-production non-SQL mutations immediately", async () => {
const execute = vi.fn().mockResolvedValue("renamed");
await expect(
executeWithProductionContextGuard({
connection: connection({ db_type: "mongodb" }),
database: "scratch",
reviewText: 'db.getCollection("users").renameCollection("accounts")',
source: "Object tree",
execute,
}),
).resolves.toBe("renamed");
expect(useProductionSafetyStore().pending).toBeUndefined();
expect(execute).toHaveBeenCalledTimes(1);
});
});
@@ -7,8 +7,12 @@ export function shouldIncludeDefaultDatabaseNode(connection: Pick<ConnectionConf
return connection?.db_type === "mysql" && databases.some((database) => !database.name.trim());
}
export function compareSidebarNames(left: string, right: string): number {
return sidebarNameCollator.compare(left, right);
}
export function sortSidebarNames(names: readonly string[]): string[] {
return [...names].sort((left, right) => sidebarNameCollator.compare(left, right));
return [...names].sort(compareSidebarNames);
}
export function sortSidebarDatabases(databases: readonly DatabaseInfo[]): DatabaseInfo[] {
@@ -1,5 +1,5 @@
import { useProductionSafetyStore } from "@/stores/productionSafetyStore";
import { assessProductionSql } from "@/lib/database/productionSafety";
import { assessProductionSql, productionContextForDatabase } from "@/lib/database/productionSafety";
import type { ConnectionConfig } from "@/types/database";
export interface ProductionSqlExecutionGuardOptions<T> {
@@ -26,3 +26,31 @@ export async function executeWithProductionSqlGuard<T>(options: ProductionSqlExe
}
return options.execute();
}
export interface ProductionContextExecutionGuardOptions<T> {
connection?: ConnectionConfig;
database?: string | null;
/** Review text shown in the production confirmation dialog (SQL or shell preview). */
reviewText: string;
source?: string;
execute: () => Promise<T>;
}
/**
* Gate non-SQL mutations (Mongo shell commands, structured editors, etc.) using
* connection/database production scope rather than SQL risk classification.
*/
export async function executeWithProductionContextGuard<T>(options: ProductionContextExecutionGuardOptions<T>): Promise<T | undefined> {
const productionContext = productionContextForDatabase(options.connection, options.database);
if (productionContext.active) {
const confirmed = await useProductionSafetyStore().requestConfirmation({
sql: options.reviewText,
connectionName: options.connection?.name,
database: options.database ?? undefined,
productionDatabases: productionContext.databases,
source: options.source,
});
if (!confirmed) return undefined;
}
return options.execute();
}