: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(devex): forget squash addons rows when restoring a schema dump (#96400)
Julian Bez04d6b8e8fc1a
diff --git a/tools/hogli-commands/hogli_commands/db_schema.py b/tools/hogli-commands/hogli_commands/db_schema.pyindex 210f92f8b051..25c7074ab13b 100644--- a/tools/hogli-commands/hogli_commands/db_schema.py+++ b/tools/hogli-commands/hogli_commands/db_schema.py@@ -35,6 +35,7 @@ DOCKER_COMPOSE = ["docker", "compose", "-f", "docker-compose.dev.yml"]DB_IDENTIFIER_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]{0,62}$")PRODUCT_DB_ROUTING_PATH = Path("products/db_routing.yaml")APP_LABEL_RE = re.compile(r"^[a-z][a-z0-9_]*$")+SQUASH_SCHEMA_ADDONS_NAME_PATTERN = r"%\_squash\_%\_schema\_addons"T = TypeVar("T")@@ -198,6 +199,11 @@ def _forget_product_app_migrations(target_db: str) -> None:environment that configures no product database then applies their NEXT migration for realand dies on the missing table. Clearing the rows lets each consumer replay them under its ownrouting: a no-op where the app is routed away, real tables where it isn't.++ The squash schema-addons migrations go with them. Each one depends on the leaf squash of every+ squashed app, the product-routed apps included, so a database that keeps an addons row while+ its dependency row is gone fails Django's consistency check before `migrate` does anything.+ Replay is cheap: the addons operations probe the schema and skip what is already there."""app_labels = _product_routed_app_labels()if not app_labels:@@ -217,7 +223,8 @@ def _forget_product_app_migrations(target_db: str) -> None:"posthog",target_db,"-c",- f"DELETE FROM django_migrations WHERE app IN ({in_list});",+ f"DELETE FROM django_migrations WHERE app IN ({in_list}) "+ f"OR name LIKE '{SQUASH_SCHEMA_ADDONS_NAME_PATTERN}';",])diff --git a/tools/hogli-commands/hogli_commands/tests/test_db_schema.py b/tools/hogli-commands/hogli_commands/tests/test_db_schema.pyindex ed9901325dbc..0f1983c3bedd 100644--- a/tools/hogli-commands/hogli_commands/tests/test_db_schema.py+++ b/tools/hogli-commands/hogli_commands/tests/test_db_schema.py@@ -307,7 +307,9 @@ def test_restore_schema_dump_recreate_drops_and_creates(tmp_path: Path, monkeypadef test_restore_schema_dump_forgets_product_app_migrations(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:# The dump comes from a database that routes product apps elsewhere, so it records their# migrations as applied without ever creating their tables. Left in place, the next product- # migration is applied for real here and fails on the missing table.+ # migration is applied for real here and fails on the missing table. The squash schema-addons+ # rows must go too: they depend on the product apps' leaf squashes, so keeping them while the+ # product rows are gone fails Django's migration consistency check.schema_path = tmp_path / "schema.sql.gz"_write_schema(schema_path)commands: list[list[str]] = []@@ -324,6 +326,7 @@ def test_restore_schema_dump_forgets_product_app_migrations(tmp_path: Path, monkassert app_labels, "expected products/db_routing.yaml to route at least one app"for app_label in app_labels:assert f"'{app_label}'" in deletes[0]+ assert r"name LIKE '%\_squash\_%\_schema\_addons'" in deletes[0]def test_restore_schema_dump_recreate_cleans_up_after_failure(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: