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), +]