This repository was archived by the owner on Jun 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
Hard delete workspaces #685
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,8 @@ | |
from alembic import command as alembic_command | ||
from alembic.config import Config as AlembicConfig | ||
from pydantic import BaseModel | ||
from sqlalchemy import CursorResult, TextClause, text | ||
from sqlalchemy import CursorResult, TextClause, event, text | ||
from sqlalchemy.engine import Engine | ||
from sqlalchemy.exc import IntegrityError, OperationalError | ||
from sqlalchemy.ext.asyncio import create_async_engine | ||
|
||
|
@@ -35,6 +36,21 @@ class AlreadyExistsError(Exception): | |
pass | ||
|
||
|
||
@event.listens_for(Engine, "connect") | ||
def set_sqlite_pragma(dbapi_connection, connection_record): | ||
""" | ||
Ensures that foreign keys are enabled for the SQLite database at every connection. | ||
|
||
SQLite does not enforce foreign keys by default, so we need to enable them manually. | ||
[SQLAlchemy docs](https://docs.sqlalchemy.org/en/20/dialects/sqlite.html#foreign-key-support) | ||
[SQLite docs](https://www.sqlite.org/foreignkeys.html) | ||
[SO](https://stackoverflow.com/questions/2614984/sqlite-sqlalchemy-how-to-enforce-foreign-keys) | ||
""" | ||
cursor = dbapi_connection.cursor() | ||
cursor.execute("PRAGMA foreign_keys=ON") | ||
cursor.close() | ||
|
||
|
||
class DbCodeGate: | ||
_instance = None | ||
|
||
|
@@ -318,6 +334,19 @@ async def soft_delete_workspace(self, workspace: Workspace) -> Optional[Workspac | |
) | ||
return deleted_workspace | ||
|
||
async def hard_delete_workspace(self, workspace: Workspace) -> Optional[Workspace]: | ||
sql = text( | ||
""" | ||
DELETE FROM workspaces | ||
WHERE id = :id | ||
RETURNING * | ||
""" | ||
) | ||
deleted_workspace = await self._execute_update_pydantic_model( | ||
workspace, sql, should_raise=True | ||
) | ||
return deleted_workspace | ||
|
||
|
||
class DbReader(DbCodeGate): | ||
|
||
|
@@ -431,7 +460,7 @@ async def get_workspaces(self) -> List[WorkspaceActive]: | |
workspaces = await self._execute_select_pydantic_model(WorkspaceActive, sql) | ||
return workspaces | ||
|
||
async def get_workspace_by_name(self, name: str) -> Optional[Workspace]: | ||
async def get_non_deleted_workspace_by_name(self, name: str) -> Optional[Workspace]: | ||
sql = text( | ||
""" | ||
SELECT | ||
|
@@ -446,6 +475,21 @@ async def get_workspace_by_name(self, name: str) -> Optional[Workspace]: | |
) | ||
return workspaces[0] if workspaces else None | ||
|
||
async def get_workspace_by_name(self, name: str) -> Optional[Workspace]: | ||
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. should we rename this to deal with the expectation that we're dealing with soft deleted or archived workspaces? |
||
sql = text( | ||
""" | ||
SELECT | ||
id, name, system_prompt, deleted_at | ||
FROM workspaces | ||
WHERE name = :name | ||
""" | ||
) | ||
conditions = {"name": name} | ||
workspaces = await self._exec_select_conditions_to_pydantic( | ||
Workspace, sql, conditions, should_raise=True | ||
) | ||
return workspaces[0] if workspaces else None | ||
|
||
async def get_sessions(self) -> List[Session]: | ||
sql = text( | ||
""" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
most of the time we'd be dealing with non deleted workspaces. I would suggest keeping the name
get_workspace_by_name
since it adheres to the expectation.