feat: add metadata catalog cleanup commands
This commit is contained in:
@@ -3,6 +3,7 @@ import { setTimeout as delay } from "node:timers/promises";
|
||||
import { z } from "zod";
|
||||
import { isPrincipalContext, requirePermission } from "../auth/authorization.js";
|
||||
import type { CatalogSyncWorker } from "../catalog/sync-worker.js";
|
||||
import type { CatalogOperationCoordinator } from "../catalog/operation-coordinator.js";
|
||||
import {
|
||||
CatalogConflictError,
|
||||
CatalogConnectorError,
|
||||
@@ -23,6 +24,14 @@ const createRunSchema = z.object({
|
||||
scope: z.enum(["tables", "columns", "relationships", "all"]),
|
||||
tableIds: z.array(idSchema).max(10_000).default([]),
|
||||
}).strict();
|
||||
const databaseCleanupSchema = z.object({
|
||||
databaseIds: z.array(idSchema).min(1).max(1_000),
|
||||
target: z.enum(["tables", "relationships"]),
|
||||
}).strict();
|
||||
const tableCleanupSchema = z.object({
|
||||
tableIds: z.array(idSchema).min(1).max(10_000),
|
||||
target: z.enum(["columns", "relationships"]),
|
||||
}).strict();
|
||||
const confirmationSchema = z.object({ confirmationToken: z.string().uuid() }).strict();
|
||||
const eventQuerySchema = z.object({ after: z.coerce.number().int().nonnegative().default(0) });
|
||||
|
||||
@@ -62,7 +71,11 @@ function publicRun(run: CatalogSyncRun) {
|
||||
|
||||
export function catalogSchemaRoutes(
|
||||
app: FastifyInstance,
|
||||
deps: { repository: CatalogRepository; worker: CatalogSyncWorker },
|
||||
deps: {
|
||||
repository: CatalogRepository;
|
||||
worker: CatalogSyncWorker;
|
||||
operations?: CatalogOperationCoordinator;
|
||||
},
|
||||
): void {
|
||||
app.get("/catalog/databases/:databaseId/tables/:tableId/columns", async (request, reply) => {
|
||||
if (!manage(request, reply)) return reply;
|
||||
@@ -114,6 +127,45 @@ export function catalogSchemaRoutes(
|
||||
} catch (error) { return safeError(reply, error); }
|
||||
});
|
||||
|
||||
app.post("/catalog/databases/metadata-cleanup", async (request, reply) => {
|
||||
if (!manage(request, reply)) return reply;
|
||||
try {
|
||||
const input = databaseCleanupSchema.parse(request.body);
|
||||
const databaseIds = [...new Set(input.databaseIds)];
|
||||
const operation = async () => await deps.repository.deleteDatabaseMetadata(databaseIds, input.target);
|
||||
const deleted = deps.operations
|
||||
? await deps.operations.runMany(databaseIds, operation)
|
||||
: await operation();
|
||||
if (!deleted) {
|
||||
return reply.code(404).send({
|
||||
code: "database_not_found",
|
||||
message: "One or more database configurations were not found.",
|
||||
});
|
||||
}
|
||||
return deleted;
|
||||
} catch (error) { return safeError(reply, error); }
|
||||
});
|
||||
|
||||
app.post("/catalog/databases/:databaseId/tables/metadata-cleanup", async (request, reply) => {
|
||||
if (!manage(request, reply)) return reply;
|
||||
try {
|
||||
const databaseId = idSchema.parse((request.params as { databaseId?: unknown }).databaseId);
|
||||
const input = tableCleanupSchema.parse(request.body);
|
||||
const tableIds = [...new Set(input.tableIds)];
|
||||
const operation = async () => await deps.repository.deleteTableMetadata(databaseId, tableIds, input.target);
|
||||
const deleted = deps.operations
|
||||
? await deps.operations.run(databaseId, operation)
|
||||
: await operation();
|
||||
if (!deleted) {
|
||||
return reply.code(404).send({
|
||||
code: "table_not_found",
|
||||
message: "One or more catalog tables were not found.",
|
||||
});
|
||||
}
|
||||
return deleted;
|
||||
} catch (error) { return safeError(reply, error); }
|
||||
});
|
||||
|
||||
app.post("/catalog/databases/:databaseId/sync-runs", async (request, reply) => {
|
||||
if (!manage(request, reply)) return reply;
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user