-
Notifications
You must be signed in to change notification settings - Fork 236
fix: auto-reassign default project on delete instead of refusing #1139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,6 +55,26 @@ async def load_project_for_delete_acceptance( | |
| return result.scalars().one_or_none() | ||
|
|
||
|
|
||
| async def select_replacement_default( | ||
| session: AsyncSession, | ||
| *, | ||
| deleted_project_id: int, | ||
| ) -> Project | None: | ||
| """Pick the active project that should inherit the default flag. | ||
|
|
||
| Deleting the default project can't simply drop the flag: a workspace must | ||
| always resolve a default for project-less writes. The oldest remaining | ||
| active project wins so the promotion is deterministic. | ||
| """ | ||
| result = await session.execute( | ||
| select(Project) | ||
| .where(Project.is_active.is_(True), Project.id != deleted_project_id) | ||
| .order_by(Project.created_at.asc(), Project.id.asc()) | ||
| .limit(1) | ||
| ) | ||
| return result.scalars().first() | ||
|
|
||
|
|
||
| async def reactivate_accepted_project( | ||
| session_maker: async_sessionmaker[AsyncSession], | ||
| *, | ||
|
|
@@ -90,12 +110,24 @@ async def delete_project( | |
| 404, | ||
| f"Project with external_id '{request.project_external_id}' not found", | ||
| ) | ||
| # A workspace must always resolve a default project for project-less | ||
| # writes, so the flag can't just vanish on delete. Cloud team | ||
| # workspaces hide the "set default" control (basic-memory-cloud | ||
| # #968), which previously left the default project undeletable there. | ||
| # Promote another active project instead of refusing; only block when | ||
| # nothing remains to inherit the flag. | ||
| replacement_default: Project | None = None | ||
| if project.is_default: | ||
| raise ProjectDeleteAcceptanceError( | ||
| 400, | ||
| f"Cannot delete default project '{project.name}'. " | ||
| "Set another project as default first.", | ||
| replacement_default = await select_replacement_default( | ||
| session, | ||
| deleted_project_id=project.id, | ||
| ) | ||
| if replacement_default is None: | ||
| raise ProjectDeleteAcceptanceError( | ||
| 400, | ||
| f"Cannot delete '{project.name}' because it is the only " | ||
| "project in the workspace.", | ||
| ) | ||
|
|
||
| runtime_request = RuntimeProjectDeleteJobRequest( | ||
| project_id=project.id, | ||
|
|
@@ -112,6 +144,11 @@ async def delete_project( | |
| is_default=project.is_default or False, | ||
| ) | ||
| project.is_active = False | ||
| # Hand the default flag to the promoted project in the same | ||
| # transaction so exactly one active default always exists. | ||
| if replacement_default is not None: | ||
| project.is_default = None | ||
| replacement_default.is_default = True | ||
|
Comment on lines
+149
to
+151
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If deleting a default project reaches this promotion and Useful? React with 👍 / 👎. |
||
| await session.commit() | ||
|
|
||
| try: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When one request deletes the current default while another simultaneously deletes the sibling selected here, this query reads the replacement without locking it. The sibling delete can commit after this selection but before this transaction sets
replacement_default.is_default = True, leaving an inactive row marked as the default and no active default project; selecting the replacement withFOR UPDATEor rechecking its active state before commit would keep the promotion and delete paths serialized.Useful? React with 👍 / 👎.