OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
fix(worker): better deletion of project
Carl-Gerhard Lindesvärdd38ccb4717dd
diff --git a/apps/worker/src/jobs/cron.delete-projects.ts b/apps/worker/src/jobs/cron.delete-projects.tsindex be1d84999..0f4a3af5c 100644--- a/apps/worker/src/jobs/cron.delete-projects.ts+++ b/apps/worker/src/jobs/cron.delete-projects.ts@@ -1,9 +1,10 @@import { logger } from '@/utils/logger';-import { generateSalt } from '@openpanel/common/server';-import { TABLE_NAMES, ch, chQuery, db } from '@openpanel/db';+import { TABLE_NAMES, ch, db } from '@openpanel/db';+import type { CronQueuePayload } from '@openpanel/queue';+import type { Job } from 'bullmq';import { escape } from 'sqlstring';-export async function deleteProjects() {+export async function deleteProjects(job: Job<CronQueuePayload>) {const projects = await db.project.findMany({where: {deleteAt: {@@ -24,16 +25,33 @@ export async function deleteProjects() {});}- if (process.env.SELF_HOSTED) {- await ch.command({- query: `DELETE FROM ${TABLE_NAMES.events} WHERE project_id IN (${projects.map((project) => escape(project.id)).join(',')});`,- clickhouse_settings: {- lightweight_deletes_sync: 0,- },- });- } else {+ logger.info('Deleting projects', {+ projects,+ });++ projects.forEach((project) => {+ job.log(`Delete project: "${project.id}"`);+ });++ const where = `project_id IN (${projects.map((project) => escape(project.id)).join(',')})`;+ const tables = [+ TABLE_NAMES.events,+ TABLE_NAMES.profiles,+ TABLE_NAMES.events_bots,+ TABLE_NAMES.sessions,+ TABLE_NAMES.cohort_events_mv,+ TABLE_NAMES.dau_mv,+ TABLE_NAMES.event_names_mv,+ TABLE_NAMES.event_property_values_mv,+ ];++ for (const table of tables) {+ const query = process.env.SELF_HOSTED+ ? `ALTER TABLE ${table} DELETE WHERE ${where};`+ : `ALTER TABLE ${table}_replicated ON CLUSTER '{cluster}' DELETE WHERE ${where};`;+await ch.command({- query: `DELETE FROM ${TABLE_NAMES.events}_replicated ON CLUSTER '{cluster}' WHERE project_id IN (${projects.map((project) => escape(project.id)).join(',')});`,+ query,clickhouse_settings: {lightweight_deletes_sync: 0,},diff --git a/apps/worker/src/jobs/cron.ts b/apps/worker/src/jobs/cron.tsindex 289080767..f4ccf9bd5 100644--- a/apps/worker/src/jobs/cron.ts+++ b/apps/worker/src/jobs/cron.ts@@ -25,7 +25,7 @@ export async function cronJob(job: Job<CronQueuePayload>) {return await ping();}case 'deleteProjects': {- return await deleteProjects();+ return await deleteProjects(job);}}}diff --git a/packages/db/prisma/migrations/20250331190933_more_cascade/migration.sql b/packages/db/prisma/migrations/20250331190933_more_cascade/migration.sqlnew file mode 100644index 000000000..a2f02a93f--- /dev/null+++ b/packages/db/prisma/migrations/20250331190933_more_cascade/migration.sql@@ -0,0 +1,47 @@+-- DropForeignKey+ALTER TABLE "dashboards" DROP CONSTRAINT "dashboards_organizationId_fkey";++-- DropForeignKey+ALTER TABLE "integrations" DROP CONSTRAINT "integrations_organizationId_fkey";++-- DropForeignKey+ALTER TABLE "invites" DROP CONSTRAINT "invites_createdById_fkey";++-- DropForeignKey+ALTER TABLE "notifications" DROP CONSTRAINT "notifications_integrationId_fkey";++-- DropForeignKey+ALTER TABLE "notifications" DROP CONSTRAINT "notifications_notificationRuleId_fkey";++-- DropForeignKey+ALTER TABLE "reports" DROP CONSTRAINT "reports_dashboardId_fkey";++-- DropForeignKey+ALTER TABLE "reset_password" DROP CONSTRAINT "reset_password_accountId_fkey";++-- DropForeignKey+ALTER TABLE "shares" DROP CONSTRAINT "shares_organizationId_fkey";++-- AddForeignKey+ALTER TABLE "invites" ADD CONSTRAINT "invites_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;++-- AddForeignKey+ALTER TABLE "dashboards" ADD CONSTRAINT "dashboards_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE;++-- AddForeignKey+ALTER TABLE "reports" ADD CONSTRAINT "reports_dashboardId_fkey" FOREIGN KEY ("dashboardId") REFERENCES "dashboards"("id") ON DELETE CASCADE ON UPDATE CASCADE;++-- AddForeignKey+ALTER TABLE "shares" ADD CONSTRAINT "shares_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE;++-- AddForeignKey+ALTER TABLE "notifications" ADD CONSTRAINT "notifications_integrationId_fkey" FOREIGN KEY ("integrationId") REFERENCES "integrations"("id") ON DELETE CASCADE ON UPDATE CASCADE;++-- AddForeignKey+ALTER TABLE "notifications" ADD CONSTRAINT "notifications_notificationRuleId_fkey" FOREIGN KEY ("notificationRuleId") REFERENCES "notification_rules"("id") ON DELETE CASCADE ON UPDATE CASCADE;++-- AddForeignKey+ALTER TABLE "integrations" ADD CONSTRAINT "integrations_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE;++-- AddForeignKey+ALTER TABLE "reset_password" ADD CONSTRAINT "reset_password_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "accounts"("id") ON DELETE CASCADE ON UPDATE CASCADE;diff --git a/packages/db/prisma/schema.prisma b/packages/db/prisma/schema.prismaindex a5b393ee7..76789b5b0 100644--- a/packages/db/prisma/schema.prisma+++ b/packages/db/prisma/schema.prisma@@ -36,7 +36,7 @@ model Organization {projects Project[]members Member[]createdByUserId String?- createdBy User? @relation(name: "organizationCreatedBy", fields: [createdByUserId], references: [id])+ createdBy User? @relation(name: "organizationCreatedBy", fields: [createdByUserId], references: [id], onDelete: SetNull)ProjectAccess ProjectAccess[]Client Client[]Dashboard Dashboard[]@@ -129,7 +129,7 @@ model Member {userId String?user User? @relation(fields: [userId], references: [id], onDelete: Cascade)invitedById String?- invitedBy User? @relation("invitedBy", fields: [invitedById], references: [id])+ invitedBy User? @relation("invitedBy", fields: [invitedById], references: [id], onDelete: SetNull)organizationId Stringorganization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)meta Json?@@ -142,7 +142,7 @@ model Member {model Invite {id String @idemail String- createdBy User @relation(fields: [createdById], references: [id])+ createdBy User @relation(fields: [createdById], references: [id], onDelete: Cascade)createdById StringorganizationId Stringorganization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)@@ -263,7 +263,7 @@ enum ChartType {model Dashboard {id String @id @default(dbgenerated("gen_random_uuid()"))name String- organization Organization @relation(fields: [organizationId], references: [id])+ organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)organizationId StringprojectId Stringproject Project @relation(fields: [projectId], references: [id], onDelete: Cascade)@@ -302,7 +302,7 @@ model Report {funnelWindow Float?dashboardId String- dashboard Dashboard @relation(fields: [dashboardId], references: [id])+ dashboard Dashboard @relation(fields: [dashboardId], references: [id], onDelete: Cascade)createdAt DateTime @default(now())updatedAt DateTime @default(now()) @updatedAt@@ -314,7 +314,7 @@ model ShareOverview {id String @uniqueprojectId String @uniqueproject Project @relation(fields: [projectId], references: [id], onDelete: Cascade)- organization Organization @relation(fields: [organizationId], references: [id])+ organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)organizationId Stringpublic Boolean @default(false)password String?@@ -389,10 +389,10 @@ model Notification {updatedAt DateTime @default(now()) @updatedAtsendToApp Boolean @default(false)sendToEmail Boolean @default(false)- integration Integration? @relation(fields: [integrationId], references: [id])+ integration Integration? @relation(fields: [integrationId], references: [id], onDelete: Cascade)integrationId String? @db.UuidnotificationRuleId String? @db.Uuid- notificationRule NotificationRule? @relation(fields: [notificationRuleId], references: [id])+ notificationRule NotificationRule? @relation(fields: [notificationRuleId], references: [id], onDelete: Cascade)/// [IPrismaNotificationPayload]payload Json?@@ -404,7 +404,7 @@ model Integration {name String/// [IPrismaIntegrationConfig]config Json- organization Organization @relation(fields: [organizationId], references: [id])+ organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)organizationId StringnotificationRules NotificationRule[]notifications Notification[]@@ -417,7 +417,7 @@ model Integration {model ResetPassword {id String @idaccountId String- account Account @relation(fields: [accountId], references: [id])+ account Account @relation(fields: [accountId], references: [id], onDelete: Cascade)expiresAt DateTimecreatedAt DateTime @default(now())updatedAt DateTime @default(now()) @updatedAt