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.

chore(tests): keep the team id sequence across test flushes (#126377)

Georges-Antoine Assi16ebd69d63fb
diff --git a/posthog/test/base.py b/posthog/test/base.py
index f05be13f4aa3..363572bd8f71 100644
--- a/posthog/test/base.py
+++ b/posthog/test/base.py
@@ -928,6 +928,9 @@ logger = logging.getLogger(__name__)
# never re-seeded, TransactionTestCase tests must not mutate these tables - a
# mutation would silently poison every later test in the process.
_SELECTIVE_FLUSH_PRESERVED_TABLES = frozenset({"django_migrations", "django_content_type", "auth_permission"})
+# ClickHouse rows outlive every Postgres flush and are scoped by team_id, so restarting
+# the team sequence lets a later test's team inherit an earlier test's events.
+_SEQUENCE_NEVER_RESTARTED_TABLES = frozenset({"posthog_team"})
def _selective_flush(db_name: str, *, reset_sequences: bool) -> None:
@@ -1003,7 +1006,7 @@ def _selective_flush(db_name: str, *, reset_sequences: bool) -> None:
WHERE seq.relkind = 'S' AND tbl.relname = ANY(%s)
)
""",
- [list(_SELECTIVE_FLUSH_PRESERVED_TABLES)],
+ [list(_SELECTIVE_FLUSH_PRESERVED_TABLES | _SEQUENCE_NEVER_RESTARTED_TABLES)],
)
for (seq_name,) in cursor.fetchall():
cursor.execute(f"ALTER SEQUENCE {quote_name(seq_name)} RESTART")
diff --git a/posthog/test/test_base.py b/posthog/test/test_base.py
index 2a45bfd148ba..5d183e476061 100644
--- a/posthog/test/test_base.py
+++ b/posthog/test/test_base.py
@@ -1,6 +1,7 @@
import pytest
from posthog.test.base import (
QueryMatchingTest,
+ _selective_flush,
run_clickhouse_statement_in_parallel,
skip_clickhouse_query_snapshots,
snapshot_clickhouse_queries,
@@ -9,7 +10,7 @@ from posthog.test.base import (
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
-from django.db import reset_queries
+from django.db import DEFAULT_DB_ALIAS, reset_queries
from django.test import TestCase
from clickhouse_driver.errors import ServerException
@@ -22,9 +23,20 @@ from posthog.models.event.sql import (
WRITABLE_EVENTS_DATA_TABLE,
WRITABLE_EVENTS_JSON_TABLE,
)
+from posthog.models.organization import Organization
from posthog.models.team.team import Team
[email protected]_db(transaction=True)
+def test_sequence_resetting_flush_never_reuses_a_team_id() -> None:
+ team_before_flush = Team.objects.create(organization=Organization.objects.create(name="before"))
+
+ _selective_flush(DEFAULT_DB_ALIAS, reset_sequences=True)
+
+ team_after_flush = Team.objects.create(organization=Organization.objects.create(name="after"))
+ assert team_after_flush.id > team_before_flush.id
+
+
def test_snapshot_clickhouse_queries_skips_marked_methods() -> None:
class ExampleTests:
@skip_clickhouse_query_snapshots