Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(clickhouse): use CLICKHOUSE_PASSWORD_FILE in migrate_clickhouse (#97567)

Tommy Gilmore7d5dec1d6e01
diff --git a/posthog/clickhouse/client/connection.py b/posthog/clickhouse/client/connection.py
index 9c51d7944034..6fd136e10a57 100644
--- a/posthog/clickhouse/client/connection.py
+++ b/posthog/clickhouse/client/connection.py
@@ -372,10 +372,12 @@ def get_pool(
return make_ch_pool(**kwargs)
-def default_client(host=settings.CLICKHOUSE_HOST):
+def default_client(host=settings.CLICKHOUSE_HOST, password=None):
"""
Return a bare bones client for use in places where we are only interested in general ClickHouse state
DO NOT USE THIS FOR QUERYING DATA
+
+ password overrides the static CLICKHOUSE_PASSWORD, for example with a resolved file-backed token.
"""
return SyncClient(
host=host,
@@ -388,7 +390,7 @@ def default_client(host=settings.CLICKHOUSE_HOST):
database="system",
secure=settings.CLICKHOUSE_SECURE,
user=settings.CLICKHOUSE_USER,
- password=settings.CLICKHOUSE_PASSWORD,
+ password=settings.CLICKHOUSE_PASSWORD if password is None else password,
ca_certs=settings.CLICKHOUSE_CA,
verify=settings.CLICKHOUSE_VERIFY,
)
diff --git a/posthog/management/commands/migrate_clickhouse.py b/posthog/management/commands/migrate_clickhouse.py
index e28bb382bd06..0c57b7ac24ec 100644
--- a/posthog/management/commands/migrate_clickhouse.py
+++ b/posthog/management/commands/migrate_clickhouse.py
@@ -11,9 +11,9 @@ from infi.clickhouse_orm import Database
from infi.clickhouse_orm.migrations import MigrationHistory
from infi.clickhouse_orm.utils import import_submodules
-from posthog.clickhouse.client.connection import default_client
+from posthog.clickhouse.client.connection import ClickHouseCredentials, default_client
from posthog.settings import CLICKHOUSE_DATABASE, CLICKHOUSE_HTTP_URL, CLICKHOUSE_PASSWORD, CLICKHOUSE_USER
-from posthog.settings.data_stores import CLICKHOUSE_MIGRATIONS_CLUSTER
+from posthog.settings.data_stores import CLICKHOUSE_MIGRATIONS_CLUSTER, CLICKHOUSE_PASSWORD_FILE
MIGRATIONS_PACKAGE_NAME = "posthog.clickhouse.migrations"
@@ -53,14 +53,29 @@ class Command(BaseCommand):
self.migrate(CLICKHOUSE_HTTP_URL, options)
def migrate(self, host, options):
+ # Read-only --check and --plan finish in seconds, so they use the short-lived
+ # token from CLICKHOUSE_PASSWORD_FILE, and fall back to the static password when
+ # no token file is set. infi.clickhouse_orm never re-reads the password, and an
+ # apply can outlast the token, so the apply keeps the static password. The setup
+ # steps below share this password, so they never authenticate differently than
+ # the migration itself.
+ if options["check"] or options["plan"]:
+ password = ClickHouseCredentials(
+ user=CLICKHOUSE_USER, password=CLICKHOUSE_PASSWORD, password_file=CLICKHOUSE_PASSWORD_FILE
+ ).read_password()
+ else:
+ password = CLICKHOUSE_PASSWORD
+
# Infi only creates the DB in one node, but not the rest. Create it before running migrations.
- self._create_database_if_not_exists(CLICKHOUSE_DATABASE, CLICKHOUSE_MIGRATIONS_CLUSTER)
- self._create_migration_tracking_tables_if_not_exist(CLICKHOUSE_DATABASE, CLICKHOUSE_MIGRATIONS_CLUSTER)
+ self._create_database_if_not_exists(CLICKHOUSE_DATABASE, CLICKHOUSE_MIGRATIONS_CLUSTER, password)
+ self._create_migration_tracking_tables_if_not_exist(
+ CLICKHOUSE_DATABASE, CLICKHOUSE_MIGRATIONS_CLUSTER, password
+ )
database = Database(
CLICKHOUSE_DATABASE,
db_url=host,
username=CLICKHOUSE_USER,
- password=CLICKHOUSE_PASSWORD,
+ password=password,
cluster=CLICKHOUSE_MIGRATIONS_CLUSTER,
verify_ssl_cert=False,
randomize_replica_paths=settings.TEST or settings.E2E_TESTING,
@@ -120,19 +135,19 @@ class Command(BaseCommand):
def get_applied_migrations(self, database) -> set[str]:
return database._get_applied_migrations(MIGRATIONS_PACKAGE_NAME, replicated=True)
- def _create_database_if_not_exists(self, database: str, cluster: str):
+ def _create_database_if_not_exists(self, database: str, cluster: str, password: str):
# MULTINODE_CLICKHOUSE: infi.clickhouse_orm creates the Distributed
# migration-tracking table across the migrations cluster before the
# first migration runs, so the database has to exist on every node up
# front — otherwise the CREATE TABLE fans out to satellites that have
# no `posthog` database yet and fails with UNKNOWN_DATABASE.
if settings.TEST or settings.E2E_TESTING or settings.MULTINODE_CLICKHOUSE:
- with default_client() as client:
+ with default_client(password=password) as client:
client.execute(
f"CREATE DATABASE IF NOT EXISTS {database} ON CLUSTER {cluster}",
)
- def _create_migration_tracking_tables_if_not_exist(self, database: str, cluster: str):
+ def _create_migration_tracking_tables_if_not_exist(self, database: str, cluster: str, password: str):
# MULTINODE_CLICKHOUSE only: infi.clickhouse_orm's auto-create path
# issues `CREATE TABLE` without `ON CLUSTER`, so the underlying
# ReplicatedMergeTree only lands on the migrations host. With a real
@@ -148,7 +163,7 @@ class Command(BaseCommand):
# pre-create will silently diverge — keep the two in sync.
if not settings.MULTINODE_CLICKHOUSE:
return
- with default_client() as client:
+ with default_client(password=password) as client:
client.execute(
f"""
CREATE TABLE IF NOT EXISTS {database}.infi_clickhouse_orm_migrations