From c6d4e5df7c84a3201b6b4fe3b7b20cfbc734f7eb Mon Sep 17 00:00:00 2001 From: CREDO23 Date: Fri, 26 Jun 2026 13:27:16 +0200 Subject: [PATCH] refactor(db): rename searchspaces table to workspaces Phase 1 (rename DB) commit 1i: rename the SearchSpace table to workspaces and flip all 24 inbound FK target strings (20 in db.py + the 4 satellite models), plus the raw-SQL searchspaces references in the obsidian and google-unification integration fixtures. This completes the ORM half of Phase 1. The SearchSpace class name, relationship/back_populates attribute names, and the /searchspaces API route URLs are intentionally left for Phase 2. Verified: unit 2375 passed/1 skip, integration 346 passed (baseline parity); create_all builds every table with the workspaces name and all FKs resolving to workspaces.id. --- .../persistence/models/automation.py | 2 +- surfsense_backend/app/db.py | 42 +++++++++---------- .../app/file_storage/persistence/models.py | 2 +- .../app/notifications/persistence/models.py | 2 +- .../app/podcasts/persistence/models.py | 2 +- .../google_unification/conftest.py | 4 +- .../test_obsidian_plugin_routes.py | 2 +- 7 files changed, 28 insertions(+), 28 deletions(-) diff --git a/surfsense_backend/app/automations/persistence/models/automation.py b/surfsense_backend/app/automations/persistence/models/automation.py index f82e9c750..76ba5e2e6 100644 --- a/surfsense_backend/app/automations/persistence/models/automation.py +++ b/surfsense_backend/app/automations/persistence/models/automation.py @@ -27,7 +27,7 @@ class Automation(BaseModel, TimestampMixin): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False, index=True, ) diff --git a/surfsense_backend/app/db.py b/surfsense_backend/app/db.py index 60a243346..28e0f159a 100644 --- a/surfsense_backend/app/db.py +++ b/surfsense_backend/app/db.py @@ -608,7 +608,7 @@ class NewChatThread(BaseModel, TimestampMixin): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False, ) @@ -791,7 +791,7 @@ class ExternalChatAccount(Base, TimestampMixin): owner_search_space_id = Column( "owner_workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=True, ) is_system_account = Column( @@ -906,7 +906,7 @@ class ExternalChatBinding(Base, TimestampMixin): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False, ) state = Column( @@ -1125,7 +1125,7 @@ class TokenUsage(BaseModel, TimestampMixin): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False, index=True, ) @@ -1331,7 +1331,7 @@ class Folder(BaseModel, TimestampMixin): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False, index=True, ) @@ -1396,7 +1396,7 @@ class Document(BaseModel, TimestampMixin): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False, ) @@ -1523,7 +1523,7 @@ class VideoPresentation(BaseModel, TimestampMixin): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False, ) search_space = relationship("SearchSpace", back_populates="video_presentations") @@ -1553,7 +1553,7 @@ class Report(BaseModel, TimestampMixin): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False, ) search_space = relationship("SearchSpace", back_populates="reports") @@ -1584,7 +1584,7 @@ class Connection(BaseModel, TimestampMixin): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=True, ) user_id = Column( @@ -1698,7 +1698,7 @@ class ImageGeneration(BaseModel, TimestampMixin): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False, ) created_by_id = Column( @@ -1714,7 +1714,7 @@ class ImageGeneration(BaseModel, TimestampMixin): class SearchSpace(BaseModel, TimestampMixin): - __tablename__ = "searchspaces" + __tablename__ = "workspaces" name = Column(String(100), nullable=False, index=True) description = Column(String(500), nullable=True) @@ -1910,7 +1910,7 @@ class SearchSourceConnector(BaseModel, TimestampMixin): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False, ) search_space = relationship( @@ -1940,7 +1940,7 @@ class Log(BaseModel, TimestampMixin): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False, ) search_space = relationship("SearchSpace", back_populates="logs") @@ -2083,7 +2083,7 @@ class SearchSpaceRole(BaseModel, TimestampMixin): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False, ) search_space = relationship("SearchSpace", back_populates="roles") @@ -2117,7 +2117,7 @@ class SearchSpaceMembership(BaseModel, TimestampMixin): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False, ) role_id = Column( @@ -2162,7 +2162,7 @@ class SearchSpaceInvite(BaseModel, TimestampMixin): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False, ) # Role to assign when invite is used (null means use default role) @@ -2223,7 +2223,7 @@ class Prompt(BaseModel, TimestampMixin): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=True, index=True, ) @@ -2553,7 +2553,7 @@ class AgentActionLog(BaseModel): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False, index=True, ) @@ -2625,7 +2625,7 @@ class DocumentRevision(BaseModel): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False, index=True, ) @@ -2667,7 +2667,7 @@ class FolderRevision(BaseModel): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False, index=True, ) @@ -2704,7 +2704,7 @@ class AgentPermissionRule(BaseModel): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False, index=True, ) diff --git a/surfsense_backend/app/file_storage/persistence/models.py b/surfsense_backend/app/file_storage/persistence/models.py index a3aed0205..bc6cef07f 100644 --- a/surfsense_backend/app/file_storage/persistence/models.py +++ b/surfsense_backend/app/file_storage/persistence/models.py @@ -32,7 +32,7 @@ class DocumentFile(BaseModel, TimestampMixin): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False, index=True, ) diff --git a/surfsense_backend/app/notifications/persistence/models.py b/surfsense_backend/app/notifications/persistence/models.py index e0d3d8dfc..d75a9d8dc 100644 --- a/surfsense_backend/app/notifications/persistence/models.py +++ b/surfsense_backend/app/notifications/persistence/models.py @@ -50,7 +50,7 @@ class Notification(BaseModel, TimestampMixin): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=True, index=True, ) diff --git a/surfsense_backend/app/podcasts/persistence/models.py b/surfsense_backend/app/podcasts/persistence/models.py index 0a93d31ed..559998d4e 100644 --- a/surfsense_backend/app/podcasts/persistence/models.py +++ b/surfsense_backend/app/podcasts/persistence/models.py @@ -71,7 +71,7 @@ class Podcast(BaseModel, TimestampMixin): search_space_id = Column( "workspace_id", Integer, - ForeignKey("searchspaces.id", ondelete="CASCADE"), + ForeignKey("workspaces.id", ondelete="CASCADE"), nullable=False, ) search_space = relationship("SearchSpace", back_populates="podcasts") diff --git a/surfsense_backend/tests/integration/google_unification/conftest.py b/surfsense_backend/tests/integration/google_unification/conftest.py index 151ee98e3..e096b30d6 100644 --- a/surfsense_backend/tests/integration/google_unification/conftest.py +++ b/surfsense_backend/tests/integration/google_unification/conftest.py @@ -199,7 +199,7 @@ async def committed_google_data(async_engine): async with async_engine.begin() as conn: await conn.execute( - text("DELETE FROM searchspaces WHERE id = :sid"), {"sid": space_id} + text("DELETE FROM workspaces WHERE id = :sid"), {"sid": space_id} ) @@ -306,5 +306,5 @@ async def cleanup_space(async_engine, space_id: int): """Delete a search space (cascades to connectors/documents).""" async with async_engine.begin() as conn: await conn.execute( - text("DELETE FROM searchspaces WHERE id = :sid"), {"sid": space_id} + text("DELETE FROM workspaces WHERE id = :sid"), {"sid": space_id} ) diff --git a/surfsense_backend/tests/integration/test_obsidian_plugin_routes.py b/surfsense_backend/tests/integration/test_obsidian_plugin_routes.py index 899359931..576255fe0 100644 --- a/surfsense_backend/tests/integration/test_obsidian_plugin_routes.py +++ b/surfsense_backend/tests/integration/test_obsidian_plugin_routes.py @@ -133,7 +133,7 @@ async def race_user_and_space(async_engine): {"id": space_id}, ) await cleanup.execute( - text("DELETE FROM searchspaces WHERE id = :id"), + text("DELETE FROM workspaces WHERE id = :id"), {"id": space_id}, ) await cleanup.execute(