Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

chore(cohorts): drop the superseded profile-keyed summary MVs (#468)

Ritik Jain232cbd10db0b
diff --git a/packages/db/code-migrations/23-drop-old-cohort-summary-mvs.ts b/packages/db/code-migrations/23-drop-old-cohort-summary-mvs.ts
new file mode 100644
index 000000000..ea60ec014
--- /dev/null
+++ b/packages/db/code-migrations/23-drop-old-cohort-summary-mvs.ts
@@ -0,0 +1,48 @@
+import { TABLE_NAMES } from '../src/clickhouse/client';
+import {
+ dropTable,
+ runClickhouseMigrationCommands,
+} from '../src/clickhouse/migration';
+import { getIsCluster } from './helpers';
+
+/**
+ * Drop the profile-keyed cohort summary MVs superseded by migration 20.
+ *
+ * - profile_event_summary_mv (created in migration 13)
+ * - profile_event_property_summary_mv (created in migration 14)
+ *
+ * Migration 20 created replacements keyed for the queries that read them,
+ * migration 21 filled them with history, and cohort.service has read the new
+ * tables since. The old pair kept receiving inserts so the change stayed
+ * revertible by pointer while the new tables were verified. Nothing reads
+ * them now, so they are two MV triggers firing on every event insert for no
+ * consumer.
+ *
+ * Clustered installs have two objects per MV: `<name>` is the Distributed
+ * table and `<name>_replicated` is the materialized view itself. The
+ * Distributed table goes first so nothing can route a read at a view that is
+ * mid-drop. Dropping the view takes its implicit `.inner_id.<uuid>` storage
+ * with it, so there is no third name to clean up.
+ *
+ * Deliberately one-way. The aggregated history goes with the tables, and
+ * rerunning migrations 13 and 14 would only bring back empty structure, so a
+ * down() would be a rollback in name only. If you need them back, those two
+ * migrations hold the definitions and migration 15 the backfill.
+ */
+
+const SUPERSEDED_MVS = [
+ TABLE_NAMES.profile_event_summary_mv,
+ TABLE_NAMES.profile_event_property_summary_mv,
+];
+
+export async function up() {
+ const isClustered = getIsCluster();
+
+ const sqls = SUPERSEDED_MVS.flatMap((name) =>
+ isClustered
+ ? [dropTable(name, true), dropTable(`${name}_replicated`, true)]
+ : [dropTable(name, false)],
+ );
+
+ await runClickhouseMigrationCommands(sqls);
+}
diff --git a/packages/db/src/clickhouse/client.ts b/packages/db/src/clickhouse/client.ts
index d78a5220c..783830de7 100644
--- a/packages/db/src/clickhouse/client.ts
+++ b/packages/db/src/clickhouse/client.ts
@@ -66,6 +66,8 @@ export const TABLE_NAMES = {
groups: 'groups',
cohort_members: 'cohort_members',
cohort_metadata: 'cohort_metadata',
+ // Superseded by the two event_*_summary_mv entries above and dropped in
+ // migration 23. Kept because migrations 13, 14 and 15 still name them.
profile_event_summary_mv: 'profile_event_summary_mv',
// Same content as the two MVs above, keyed for the cohort criteria that
// read them (event + window first, profile last) rather than by profile.
diff --git a/packages/db/src/services/delete.service.ts b/packages/db/src/services/delete.service.ts
index 7dace2640..680898a8f 100644
--- a/packages/db/src/services/delete.service.ts
+++ b/packages/db/src/services/delete.service.ts
@@ -49,8 +49,6 @@ export async function deleteFromClickhouse(projectIds: string[]) {
TABLE_NAMES.event_property_values_mv,
TABLE_NAMES.cohort_members,
TABLE_NAMES.cohort_metadata,
- TABLE_NAMES.profile_event_summary_mv,
- TABLE_NAMES.profile_event_property_summary_mv,
TABLE_NAMES.event_profile_summary_mv,
TABLE_NAMES.event_property_profile_summary_mv,
];