fix(objects): add safe object browser batch empty action
This commit is contained in:
3 files changed
+118
-7
No files matched your search
@@ -1,7 +1,26 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { batchTableEmptyFeedback, runBatchTableEmpty } from "@/lib/sidebar/batchTableEmpty";
|
||||
import { batchTableEmptyFeedback, buildBatchTableEmptyPlan, runBatchTableEmpty } from "@/lib/sidebar/batchTableEmpty";
|
||||
|
||||
describe("batch table empty", () => {
|
||||
it("executes the exact SQL captured for the confirmation preview", async () => {
|
||||
const targets = [{ name: "orders" }, { name: "customers" }];
|
||||
let schema = "public";
|
||||
const plan = await buildBatchTableEmptyPlan(targets, async (target) => `TRUNCATE TABLE "${schema}"."${target.name}";`);
|
||||
schema = "archive";
|
||||
|
||||
const executed: string[] = [];
|
||||
await runBatchTableEmpty(plan, async ({ sql }) => {
|
||||
executed.push(sql);
|
||||
});
|
||||
|
||||
expect(plan.map(({ sql }) => sql).join("\n")).toBe('TRUNCATE TABLE "public"."orders";\nTRUNCATE TABLE "public"."customers";');
|
||||
expect(executed).toEqual(['TRUNCATE TABLE "public"."orders";', 'TRUNCATE TABLE "public"."customers";']);
|
||||
});
|
||||
|
||||
it("rejects the entire destructive plan when any preview SQL is unavailable", async () => {
|
||||
await expect(buildBatchTableEmptyPlan(["orders", "customers"], async (target) => (target === "customers" ? "" : `TRUNCATE TABLE ${target};`))).rejects.toThrow("Empty table SQL preview is unavailable");
|
||||
});
|
||||
|
||||
it("continues after a table fails and collects each result", async () => {
|
||||
const executed: string[] = [];
|
||||
const result = await runBatchTableEmpty(["orders", "locked", "customers"], async (table) => {
|
||||
|
||||
@@ -3,8 +3,23 @@ export interface BatchTableEmptyResult<T> {
|
||||
failed: Array<{ target: T; error: unknown }>;
|
||||
}
|
||||
|
||||
export interface BatchTableEmptyPlanItem<T> {
|
||||
target: T;
|
||||
sql: string;
|
||||
}
|
||||
|
||||
export type BatchTableEmptyFeedback = "success" | "partial" | "submitted" | "submitted-partial";
|
||||
|
||||
export async function buildBatchTableEmptyPlan<T>(targets: readonly T[], buildSql: (target: T) => Promise<string>): Promise<BatchTableEmptyPlanItem<T>[]> {
|
||||
const plan: BatchTableEmptyPlanItem<T>[] = [];
|
||||
for (const target of targets) {
|
||||
const sql = await buildSql(target);
|
||||
if (!sql.trim()) throw new Error("Empty table SQL preview is unavailable");
|
||||
plan.push({ target, sql });
|
||||
}
|
||||
return plan;
|
||||
}
|
||||
|
||||
export async function runBatchTableEmpty<T>(targets: readonly T[], execute: (target: T) => Promise<void>): Promise<BatchTableEmptyResult<T>> {
|
||||
const result: BatchTableEmptyResult<T> = { succeeded: [], failed: [] };
|
||||
for (const target of targets) {
|
||||
|
||||
Reference in new issue
Block a user