From 54b2bc4b8aefc508313e88012ba02e1528de6e4b Mon Sep 17 00:00:00 2001 From: James Greenhill Date: Tue, 27 Aug 2024 11:59:37 -0700 Subject: [PATCH] feat: Add soft delete column to events (#24581) --- .../0078_add_soft_delete_column_on_events.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 posthog/clickhouse/migrations/0078_add_soft_delete_column_on_events.py diff --git a/posthog/clickhouse/migrations/0078_add_soft_delete_column_on_events.py b/posthog/clickhouse/migrations/0078_add_soft_delete_column_on_events.py new file mode 100644 index 0000000000000..c64810443c9b6 --- /dev/null +++ b/posthog/clickhouse/migrations/0078_add_soft_delete_column_on_events.py @@ -0,0 +1,34 @@ +from infi.clickhouse_orm import migrations + +from posthog.clickhouse.client.connection import ch_pool +from posthog.settings import CLICKHOUSE_CLUSTER + + +DROP_COLUMNS_EVENTS = """ +ALTER TABLE {table} ON CLUSTER {cluster} +DROP COLUMN IF EXISTS is_deleted +""" + +ADD_COLUMNS_EVENTS = """ +ALTER TABLE {table} ON CLUSTER {cluster} +ADD COLUMN IF NOT EXISTS is_deleted Boolean +""" + +ADD_COLUMNS_INDEX_EVENTS = """ +ALTER TABLE {table} ON CLUSTER {cluster} +ADD INDEX IF NOT EXISTS is_deleted_idx (is_deleted) TYPE minmax GRANULARITY 1 +""" + + +def add_columns_to_required_tables(_): + with ch_pool.get_client() as client: + client.execute(DROP_COLUMNS_EVENTS.format(table="sharded_events", cluster=CLICKHOUSE_CLUSTER)) + client.execute(DROP_COLUMNS_EVENTS.format(table="events", cluster=CLICKHOUSE_CLUSTER)) + client.execute(ADD_COLUMNS_EVENTS.format(table="sharded_events", cluster=CLICKHOUSE_CLUSTER)) + client.execute(ADD_COLUMNS_EVENTS.format(table="events", cluster=CLICKHOUSE_CLUSTER)) + client.execute(ADD_COLUMNS_INDEX_EVENTS.format(table="sharded_events", cluster=CLICKHOUSE_CLUSTER)) + + +operations = [ + migrations.RunPython(add_columns_to_required_tables), +]