Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { db, eq } from "@openstatus/db";
import { monitor } from "@openstatus/db/src/schema";
import { monitor, pageComponent } from "@openstatus/db/src/schema";
import { monitorStatusTable } from "@openstatus/db/src/schema/monitor_status/monitor_status";

import { app } from "@/index";
Expand Down Expand Up @@ -468,6 +468,61 @@ describe("MonitorService.DeleteMonitor", () => {
await db.delete(monitor).where(eq(monitor.id, otherWorkspaceMon.id));
}
});

test("deleting a monitor removes its page components", async () => {
// Create a monitor
const mon = await db
.insert(monitor)
.values({
workspaceId: 1,
name: `${TEST_PREFIX}-delete-with-component`,
url: "https://delete-component.example.com",
periodicity: "1m",
active: true,
regions: "ams",
jobType: "http",
})
.returning()
.get();

// Create a pageComponent referencing that monitor
const component = await db
.insert(pageComponent)
.values({
workspaceId: 1,
pageId: 1,
type: "monitor",
monitorId: mon.id,
name: `${TEST_PREFIX}-component`,
order: 0,
})
.returning()
.get();

// Verify the component exists
const beforeDelete = await db
.select()
.from(pageComponent)
.where(eq(pageComponent.id, component.id))
.get();
expect(beforeDelete).toBeDefined();

// Delete the monitor
const res = await connectRequest(
"DeleteMonitor",
{ id: String(mon.id) },
{ "x-openstatus-key": "1" },
);
expect(res.status).toBe(200);

// Verify the page component was removed
const afterDelete = await db
.select()
.from(pageComponent)
.where(eq(pageComponent.id, component.id))
.get();
expect(afterDelete).toBeUndefined();
});
});

describe("MonitorService.CreateHTTPMonitor", () => {
Expand Down
47 changes: 38 additions & 9 deletions apps/server/src/routes/rpc/services/monitor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ import { getCheckerPayload, getCheckerUrl } from "@/libs/checker";
import { tb } from "@/libs/clients";
import type { ServiceImpl } from "@connectrpc/connect";
import { and, db, eq, gte, inArray, isNull, sql } from "@openstatus/db";
import { monitor, monitorRun } from "@openstatus/db/src/schema";
import {
maintenancesToMonitors,
monitor,
monitorRun,
monitorTagsToMonitors,
monitorsToPages,
monitorsToStatusReport,
notificationsToMonitors,
pageComponent,
} from "@openstatus/db/src/schema";
import { monitorStatusTable } from "@openstatus/db/src/schema/monitor_status/monitor_status";
import { selectMonitorSchema } from "@openstatus/db/src/schema/monitors/validation";
import type {
Expand Down Expand Up @@ -563,14 +572,34 @@ export const monitorServiceImpl: ServiceImpl<typeof MonitorService> = {
throw monitorNotFoundError(req.id);
}

// Soft delete
await db
.update(monitor)
.set({
active: false,
deletedAt: new Date(),
})
.where(eq(monitor.id, dbMon.id));
// Soft delete and clean up related rows atomically
await db.transaction(async (tx) => {
await tx
.update(monitor)
.set({
active: false,
deletedAt: new Date(),
})
.where(eq(monitor.id, dbMon.id));
await tx
.delete(monitorsToPages)
.where(eq(monitorsToPages.monitorId, dbMon.id));
await tx
.delete(monitorTagsToMonitors)
.where(eq(monitorTagsToMonitors.monitorId, dbMon.id));
await tx
.delete(monitorsToStatusReport)
.where(eq(monitorsToStatusReport.monitorId, dbMon.id));
await tx
.delete(notificationsToMonitors)
.where(eq(notificationsToMonitors.monitorId, dbMon.id));
await tx
.delete(maintenancesToMonitors)
.where(eq(maintenancesToMonitors.monitorId, dbMon.id));
await tx
.delete(pageComponent)
.where(eq(pageComponent.monitorId, dbMon.id));
});

return { success: true };
},
Expand Down
27 changes: 15 additions & 12 deletions packages/api/src/router/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
monitorsToStatusReport,
notification,
notificationsToMonitors,
pageComponent,
privateLocation,
privateLocationToMonitors,
selectIncidentSchema,
Expand Down Expand Up @@ -63,13 +64,11 @@ export const monitorRouter = createTRPCRouter({
.get();
if (!monitorToDelete) return;

await opts.ctx.db
.update(monitor)
.set({ deletedAt: new Date(), active: false })
.where(eq(monitor.id, monitorToDelete.id))
.run();

await opts.ctx.db.transaction(async (tx) => {
await tx
.update(monitor)
.set({ deletedAt: new Date(), active: false })
.where(eq(monitor.id, monitorToDelete.id));
await tx
.delete(monitorsToPages)
.where(eq(monitorsToPages.monitorId, monitorToDelete.id));
Expand All @@ -85,6 +84,9 @@ export const monitorRouter = createTRPCRouter({
await tx
.delete(maintenancesToMonitors)
.where(eq(maintenancesToMonitors.monitorId, monitorToDelete.id));
await tx
.delete(pageComponent)
.where(eq(pageComponent.monitorId, monitorToDelete.id));
});
}),

Expand All @@ -109,13 +111,11 @@ export const monitorRouter = createTRPCRouter({
});
}

await opts.ctx.db
.update(monitor)
.set({ deletedAt: new Date(), active: false })
.where(inArray(monitor.id, opts.input.ids))
.run();

await opts.ctx.db.transaction(async (tx) => {
await tx
.update(monitor)
.set({ deletedAt: new Date(), active: false })
.where(inArray(monitor.id, opts.input.ids));
await tx
.delete(monitorsToPages)
.where(inArray(monitorsToPages.monitorId, opts.input.ids));
Expand All @@ -131,6 +131,9 @@ export const monitorRouter = createTRPCRouter({
await tx
.delete(maintenancesToMonitors)
.where(inArray(maintenancesToMonitors.monitorId, opts.input.ids));
await tx
.delete(pageComponent)
.where(inArray(pageComponent.monitorId, opts.input.ids));
});
Comment on lines 132 to 137
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same atomicity concern as the single-delete path: the soft-delete update is executed before the cleanup transaction. Wrapping both the monitor update and the related deletes in one transaction would avoid partially-applied deletions if the cleanup fails.

Copilot uses AI. Check for mistakes.
}),

Expand Down
Loading