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(persons): add the unique person keys hobby is missing (#126578)

Zach Brownf8e3f02bd15f
diff --git a/Dockerfile b/Dockerfile
index 2b48c7f5d5b1..6a00f304eff3 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -407,6 +407,7 @@ COPY --from=frontend-build --chown=posthog:posthog /code/frontend/src/products.j
COPY --chown=posthog:posthog ./bin ./bin/
# Persons SQL migration files (read by apply_persons_migrations management command for hobby deploys)
COPY --chown=posthog:posthog ./rust/persons_migrations ./rust/persons_migrations/
+COPY --chown=posthog:posthog ./rust/persons_migrations_hobby ./rust/persons_migrations_hobby/
COPY --chown=posthog:posthog manage.py manage.py
COPY --chown=posthog:posthog posthog posthog/
COPY --chown=posthog:posthog ee ee/
diff --git a/posthog/management/commands/apply_persons_migrations.py b/posthog/management/commands/apply_persons_migrations.py
index a9466fd1e4aa..94adaed9f77c 100644
--- a/posthog/management/commands/apply_persons_migrations.py
+++ b/posthog/management/commands/apply_persons_migrations.py
@@ -5,6 +5,10 @@ persons database identified by PERSONS_DB_WRITER_URL. Hobby deploys keep persons
main database and skip the partitioning migrations (via --hobby); local dev and
production apply all migrations.
+Hobby deploys also apply rust/persons_migrations_hobby/, in filename order alongside the
+main directory. Those files give the unpartitioned tables what the partitioning migrations
+give production, and no other runner reads that directory.
+
Tracks applied migrations in a _persons_migrations_applied table so each
migration is only executed once. Also bridges the sqlx _sqlx_migrations
tracking table so that environments transitioning from sqlx don't re-apply
@@ -254,7 +258,13 @@ class Command(BaseCommand):
parser.add_argument(
"--hobby",
action="store_true",
- help="Skip partitioning migrations (for hobby deploys where posthog_person is not partitioned).",
+ help="Skip partitioning migrations and apply the hobby-only migrations (for hobby deploys where posthog_person is not partitioned).",
+ )
+ parser.add_argument(
+ "--hobby-migrations-dir",
+ type=str,
+ default=None,
+ help="Path to the hobby-only migrations directory, applied with --hobby. Defaults to rust/persons_migrations_hobby/ relative to BASE_DIR.",
)
parser.add_argument(
"--ensure-database",
@@ -270,8 +280,15 @@ class Command(BaseCommand):
if options["ensure_database"] and not dry_run:
_ensure_database_exists(persons_url)
- migrations_path = self._resolve_migrations_dir(options["migrations_dir"])
- sql_files = sorted(f for f in migrations_path.iterdir() if f.suffix == ".sql")
+ migrations_path = self._resolve_migrations_dir(options["migrations_dir"], "persons_migrations")
+ sql_files = [f for f in migrations_path.iterdir() if f.suffix == ".sql"]
+ if hobby:
+ hobby_path = self._resolve_migrations_dir(options["hobby_migrations_dir"], "persons_migrations_hobby")
+ sql_files += [f for f in hobby_path.iterdir() if f.suffix == ".sql"]
+ names = [f.name for f in sql_files]
+ if len(names) != len(set(names)):
+ raise CommandError("A hobby migration shares its filename with a main persons migration.")
+ sql_files.sort(key=lambda f: f.name)
if not sql_files:
self.stdout.write("No SQL migration files found.")
return
@@ -326,11 +343,11 @@ class Command(BaseCommand):
)
)
- def _resolve_migrations_dir(self, override: str | None) -> Path:
+ def _resolve_migrations_dir(self, override: str | None, default_name: str) -> Path:
if override:
path = Path(override)
else:
- path = Path(settings.BASE_DIR) / "rust" / "persons_migrations"
+ path = Path(settings.BASE_DIR) / "rust" / default_name
if not path.is_dir():
raise CommandError(f"Migrations directory not found: {path}")
diff --git a/posthog/management/commands/test/test_apply_persons_migrations.py b/posthog/management/commands/test/test_apply_persons_migrations.py
index 0ef4b4e04ad7..2cd78970f369 100644
--- a/posthog/management/commands/test/test_apply_persons_migrations.py
+++ b/posthog/management/commands/test/test_apply_persons_migrations.py
@@ -95,6 +95,8 @@ class TestApplyPersonsMigrations(TestCase):
self.other_index = f"no_tx_test_{self.suffix}_other_idx"
self._migrations_tmp = TemporaryDirectory()
self.migrations_dir = Path(self._migrations_tmp.name)
+ self._hobby_migrations_tmp = TemporaryDirectory()
+ self.hobby_migrations_dir = Path(self._hobby_migrations_tmp.name)
def tearDown(self) -> None:
_persons_execute(
@@ -107,13 +109,16 @@ class TestApplyPersonsMigrations(TestCase):
]
)
self._migrations_tmp.cleanup()
+ self._hobby_migrations_tmp.cleanup()
super().tearDown()
- def _write_migration(self, version: str, name: str, body: str) -> None:
- (self.migrations_dir / f"{version}_{self.suffix}_{name}.sql").write_text(body)
+ def _write_migration(self, version: str, name: str, body: str, *, hobby: bool = False) -> None:
+ directory = self.hobby_migrations_dir if hobby else self.migrations_dir
+ (directory / f"{version}_{self.suffix}_{name}.sql").write_text(body)
- def _apply(self) -> None:
- call_command("apply_persons_migrations", "--migrations-dir", str(self.migrations_dir))
+ def _apply(self, *, hobby: bool = False) -> None:
+ args = ["--migrations-dir", str(self.migrations_dir), "--hobby-migrations-dir", str(self.hobby_migrations_dir)]
+ call_command("apply_persons_migrations", *args, *(["--hobby"] if hobby else []))
def _leave_an_invalid_index(self, table: str, index: str) -> None:
_persons_execute([f"CREATE TABLE {table} (id INT)", f"INSERT INTO {table} (id) VALUES (1), (1)"])
@@ -204,3 +209,18 @@ class TestApplyPersonsMigrations(TestCase):
assert "INVALID" in str(error.value)
assert len(self._recorded_migrations()) == 1
+
+ @parameterized.expand([("hobby", True), ("not_hobby", False)])
+ def test_applies_hobby_migrations_only_with_the_hobby_flag(self, _name: str, hobby: bool) -> None:
+ self._write_migration("20990101000001", "create_table", f"CREATE TABLE {self.table} (id INT);")
+ self._write_migration(
+ "20990101000002",
+ "add_index",
+ f"-- no-transaction\nCREATE INDEX CONCURRENTLY IF NOT EXISTS {self.index} ON {self.table} (id);",
+ hobby=True,
+ )
+
+ self._apply(hobby=hobby)
+
+ assert _persons_fetchall("SELECT to_regclass(%s) IS NOT NULL", [self.index]) == [(hobby,)]
+ assert len(self._recorded_migrations()) == (2 if hobby else 1)
diff --git a/rust/persons_migrations_hobby/20261030000001_add_person_team_id_id_unique_index.sql b/rust/persons_migrations_hobby/20261030000001_add_person_team_id_id_unique_index.sql
new file mode 100644
index 000000000000..e92d449480df
--- /dev/null
+++ b/rust/persons_migrations_hobby/20261030000001_add_person_team_id_id_unique_index.sql
@@ -0,0 +1,2 @@
+-- no-transaction
+CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS posthog_person_team_id_id_uniq ON posthog_person (team_id, id);
diff --git a/rust/persons_migrations_hobby/20261030000002_check_person_team_id_uuid_duplicates.sql b/rust/persons_migrations_hobby/20261030000002_check_person_team_id_uuid_duplicates.sql
new file mode 100644
index 000000000000..8181327dedc1
--- /dev/null
+++ b/rust/persons_migrations_hobby/20261030000002_check_person_team_id_uuid_duplicates.sql
@@ -0,0 +1,17 @@
+-- A duplicate pair would fail the next migration's concurrent build and leave its index INVALID.
+DO $$
+DECLARE
+ duplicate_pairs BIGINT;
+BEGIN
+ SELECT count(*) INTO duplicate_pairs
+ FROM (
+ SELECT 1
+ FROM posthog_person
+ GROUP BY team_id, uuid
+ HAVING count(*) > 1
+ ) AS duplicates;
+
+ IF duplicate_pairs > 0 THEN
+ RAISE EXCEPTION 'posthog_person has % duplicate (team_id, uuid) pairs. Remove the duplicates, then re-run the migrations; the unique (team_id, uuid) index cannot be built until they are gone.', duplicate_pairs;
+ END IF;
+END $$;
diff --git a/rust/persons_migrations_hobby/20261030000003_add_person_team_id_uuid_unique_index.sql b/rust/persons_migrations_hobby/20261030000003_add_person_team_id_uuid_unique_index.sql
new file mode 100644
index 000000000000..67ba6a83b61a
--- /dev/null
+++ b/rust/persons_migrations_hobby/20261030000003_add_person_team_id_uuid_unique_index.sql
@@ -0,0 +1,2 @@
+-- no-transaction
+CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS posthog_person_team_id_uuid_uniq ON posthog_person (team_id, uuid);
diff --git a/tools/hogbox-preview/hogbox_preview/stack.py b/tools/hogbox-preview/hogbox_preview/stack.py
index 4342c2a287f6..105e0926da11 100644
--- a/tools/hogbox-preview/hogbox_preview/stack.py
+++ b/tools/hogbox-preview/hogbox_preview/stack.py
@@ -265,6 +265,7 @@ class PostHogPreviewStack:
("products", "/code/products"),
("packages/personhog-proto/personhog", "/code/personhog"),
("rust/persons_migrations", "/code/rust/persons_migrations"),
+ ("rust/persons_migrations_hobby", "/code/rust/persons_migrations_hobby"),
("frontend/src/lib/constants.tsx", "/code/frontend/src/lib/constants.tsx"),
]