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(projects): drop moved project from the old organization's list (#109848)

Eleftheria Trivyzakifcece220fbcf
diff --git a/posthog/api/project.py b/posthog/api/project.py
index 6b58949a80b3..a890fbf106e2 100644
--- a/posthog/api/project.py
+++ b/posthog/api/project.py
@@ -60,6 +60,7 @@ from posthog.api.team import (
)
from posthog.api.utils import validate_authorized_url_wildcards
from posthog.auth import SessionAuthentication
+from posthog.caching.organization_serializer_cache import _bump_org_serializer_cache_version
from posthog.cloud_utils import get_cached_instance_license, is_cloud
from posthog.constants import AvailableFeature
from posthog.decorators import disallow_if_impersonated
@@ -2156,6 +2157,13 @@ class ProjectViewSet(
after=str(target_organization.id),
)
+ # The cache invalidation receiver reads organization_id from the saved row, which already
+ # points at the target organization. Without this bump the source organization keeps
+ # listing the project until its cached project list expires. Register it before the saves,
+ # because Django skips the remaining commit hooks when an earlier one raises.
+ source_organization_id = str(current_organization.id)
+ transaction.on_commit(lambda: _bump_org_serializer_cache_version(source_organization_id))
+
project.organization_id = target_organization.id
project.save()
diff --git a/posthog/api/test/test_team.py b/posthog/api/test/test_team.py
index 576aed68ff69..b664d89f6b89 100644
--- a/posthog/api/test/test_team.py
+++ b/posthog/api/test/test_team.py
@@ -1631,9 +1631,14 @@ def team_api_test_factory():
other_org, _ = self._create_other_org_and_team(OrganizationMembership.Level.ADMIN)
self.organization_membership.level = OrganizationMembership.Level.ADMIN
self.organization_membership.save()
- res = self.client.post(
- f"/api/projects/{self.team.project.id}/change_organization/", {"organization_id": other_org.id}
- )
+ source_org_url = f"/api/organizations/{self.organization.id}/"
+ # Populates the source organization's cached project list before the move.
+ assert [team["id"] for team in self.client.get(source_org_url).json()["teams"]] == [self.team.id]
+
+ with self.captureOnCommitCallbacks(execute=True):
+ res = self.client.post(
+ f"/api/projects/{self.team.project.id}/change_organization/", {"organization_id": other_org.id}
+ )
assert res.status_code == status.HTTP_200_OK, res.json()
assert res.json()["id"] == self.team.id
@@ -1642,6 +1647,9 @@ def team_api_test_factory():
self.team.refresh_from_db()
assert self.project.organization == other_org
assert self.team.organization == other_org
+ source_org = self.client.get(source_org_url).json()
+ assert source_org["teams"] == []
+ assert source_org["projects"] == []
def test_change_organization_reconciles_current_project_of_affected_users(self):
other_org, _ = self._create_other_org_and_team(OrganizationMembership.Level.ADMIN)