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.

fix(tests): protect development databases from test migrations (#87911)

Rossa75665deaaf8
diff --git a/posthog/clickhouse/test/test_cluster.py b/posthog/clickhouse/test/test_cluster.py
index 14be3de94f01..53bfac398f14 100644
--- a/posthog/clickhouse/test/test_cluster.py
+++ b/posthog/clickhouse/test/test_cluster.py
@@ -28,6 +28,8 @@ from posthog.clickhouse.cluster import (
)
from posthog.models.event.sql import EVENTS_DATA_TABLE
+pytestmark = pytest.mark.django_db
+
@pytest.fixture
def cluster(django_db_setup) -> Iterator[ClickhouseCluster]:
diff --git a/posthog/clickhouse/test/test_custom_metrics.py b/posthog/clickhouse/test/test_custom_metrics.py
index aff467c1697a..686f0ed54a93 100644
--- a/posthog/clickhouse/test/test_custom_metrics.py
+++ b/posthog/clickhouse/test/test_custom_metrics.py
@@ -6,6 +6,8 @@ from posthog.test.base import reset_clickhouse_database
from posthog.clickhouse.cluster import ClickhouseCluster, Query, get_cluster
from posthog.clickhouse.custom_metrics import MetricsClient
+pytestmark = pytest.mark.django_db
+
@pytest.fixture
def cluster(django_db_setup) -> Iterator[ClickhouseCluster]:
diff --git a/posthog/conftest.py b/posthog/conftest.py
index 2e0f5b6bb849..7331d29ea88b 100644
--- a/posthog/conftest.py
+++ b/posthog/conftest.py
@@ -190,6 +190,15 @@ def reset_clickhouse_tables():
run_clickhouse_statement_in_parallel(list(CREATE_DATA_QUERIES()))
+def _sqlx_error_output(error: subprocess.CalledProcessError) -> str:
+ output = "\n".join(
+ stream.decode(errors="replace") if isinstance(stream, bytes) else stream
+ for stream in (error.stdout, error.stderr)
+ if stream
+ )
+ return output or str(error)
+
+
def run_persons_sqlx_migrations(keepdb: bool = False):
"""Run sqlx migrations for persons tables in separate test_posthog_persons database.
@@ -204,6 +213,11 @@ def run_persons_sqlx_migrations(keepdb: bool = False):
db_config = settings.DATABASES["default"]
# Use separate persons database name to mirror production
persons_db_name = db_config["NAME"] + "_persons"
+ if not persons_db_name.startswith("test_"):
+ raise RuntimeError(
+ f"Refusing to run persons migrations against '{persons_db_name}', which is not a test database. "
+ "Add a pytest.mark.django_db marker to the test module."
+ )
db_user = db_config["USER"]
db_password = db_config["PASSWORD"]
db_host = db_config["HOST"]
@@ -246,7 +260,7 @@ def run_persons_sqlx_migrations(keepdb: bool = False):
if not keepdb:
raise RuntimeError(
f"Failed to create test database with sqlx. "
- f"Ensure sqlx-cli is installed. Error: {e.stderr.decode() if e.stderr else str(e)}"
+ f"Ensure sqlx-cli is installed. Error: {_sqlx_error_output(e)}"
) from e
# Run migrations (idempotent - sqlx tracks which migrations have run)
@@ -259,7 +273,7 @@ def run_persons_sqlx_migrations(keepdb: bool = False):
)
except subprocess.CalledProcessError as e:
raise RuntimeError(
- f"Failed to run sqlx migrations from {migrations_path}. Error: {e.stderr.decode() if e.stderr else str(e)}"
+ f"Failed to run sqlx migrations from {migrations_path}. Error: {_sqlx_error_output(e)}"
) from e
diff --git a/posthog/dags/tests/test_events_backfill_to_duckling.py b/posthog/dags/tests/test_events_backfill_to_duckling.py
index 54774845f89b..eec88a8dbd7c 100644
--- a/posthog/dags/tests/test_events_backfill_to_duckling.py
+++ b/posthog/dags/tests/test_events_backfill_to_duckling.py
@@ -22,6 +22,8 @@ from posthog.dags.events_backfill_to_duckling import (
from products.managed_warehouse.backend.facade.contracts import DuckgresStoredBucketConfig
+pytestmark = pytest.mark.django_db
+
@dataclass
class _FakeRow:
diff --git a/posthog/temporal/tests/test_clickhouse.py b/posthog/temporal/tests/test_clickhouse.py
index c4ff40671d58..c4597619db47 100644
--- a/posthog/temporal/tests/test_clickhouse.py
+++ b/posthog/temporal/tests/test_clickhouse.py
@@ -22,6 +22,8 @@ from posthog.temporal.common.clickhouse import (
encode_clickhouse_data,
)
+pytestmark = pytest.mark.django_db
+
async def _wait_for_query_status(
client: ClickHouseClient,