From 7feccebd9fd3d8cd7da4cd2f62077095976ce0e1 Mon Sep 17 00:00:00 2001
From: James Greenhill <fuziontech@gmail.com>
Date: Mon, 25 Nov 2024 15:57:31 -0800
Subject: [PATCH] fix: make the ALTER TABLE MODIFY QUERY run ON CLUSTER

---
 posthog/clickhouse/migrations/0088_noop.py                  | 1 +
 .../0090_nullable_uuid_type_session_on_cluster.py           | 6 ++++++
 posthog/models/raw_sessions/sql.py                          | 4 +++-
 3 files changed, 10 insertions(+), 1 deletion(-)
 create mode 100644 posthog/clickhouse/migrations/0088_noop.py
 create mode 100644 posthog/clickhouse/migrations/0090_nullable_uuid_type_session_on_cluster.py

diff --git a/posthog/clickhouse/migrations/0088_noop.py b/posthog/clickhouse/migrations/0088_noop.py
new file mode 100644
index 0000000000000..d26d574c55224
--- /dev/null
+++ b/posthog/clickhouse/migrations/0088_noop.py
@@ -0,0 +1 @@
+operations = []
diff --git a/posthog/clickhouse/migrations/0090_nullable_uuid_type_session_on_cluster.py b/posthog/clickhouse/migrations/0090_nullable_uuid_type_session_on_cluster.py
new file mode 100644
index 0000000000000..628803f4beba9
--- /dev/null
+++ b/posthog/clickhouse/migrations/0090_nullable_uuid_type_session_on_cluster.py
@@ -0,0 +1,6 @@
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+from posthog.models.raw_sessions.sql import RAW_SESSION_TABLE_UPDATE_SQL
+
+operations = [
+    run_sql_with_exceptions(RAW_SESSION_TABLE_UPDATE_SQL),
+]
diff --git a/posthog/models/raw_sessions/sql.py b/posthog/models/raw_sessions/sql.py
index 4c060513dc742..dfd3b6c668709 100644
--- a/posthog/models/raw_sessions/sql.py
+++ b/posthog/models/raw_sessions/sql.py
@@ -409,10 +409,12 @@ def source_nullable_float_column(column_name: str) -> str:
 
 RAW_SESSION_TABLE_UPDATE_SQL = (
     lambda: """
-ALTER TABLE {table_name} MODIFY QUERY
+ALTER TABLE {table_name} ON CLUSTER '{cluster}'
+MODIFY QUERY
 {select_sql}
 """.format(
         table_name=f"{TABLE_BASE_NAME}_mv",
+        cluster=settings.CLICKHOUSE_CLUSTER,
         select_sql=RAW_SESSION_TABLE_MV_SELECT_SQL(),
     )
 )