Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Delete a person's template installs with their account, and give updates their engine

Simon Flack735de363bef5
diff --git a/core/switch_core/accounts/deletion.py b/core/switch_core/accounts/deletion.py
index 3e07b80bc..c2a842d41 100644
--- a/core/switch_core/accounts/deletion.py
+++ b/core/switch_core/accounts/deletion.py
@@ -122,6 +122,7 @@ USER_REFERENCES: dict[tuple[str, str], Handling] = {
("workspace_service_settings", "updated_by"): "handed over",
("service_app_credentials", "updated_by"): "handed over",
("provider_connections", "user_id"): "database cascades",
+ ("template_installs", "owner_id"): "database cascades",
("external_user_claims", "user_id"): "database cascades",
("chat_operations", "user_id"): "database cascades",
("chat_hidden", "user_id"): "database cascades",
diff --git a/core/switch_core/db/models.py b/core/switch_core/db/models.py
index 47ecb9852..1477dffdc 100644
--- a/core/switch_core/db/models.py
+++ b/core/switch_core/db/models.py
@@ -1376,7 +1376,10 @@ class TemplateInstall(TenantScoped, Base):
)
id: Mapped[str] = mapped_column(Text, primary_key=True, default=_uuid)
- owner_id: Mapped[str] = mapped_column(Text, ForeignKey("users.id"), nullable=False)
+ # Goes with its owner, whose agents it created go too.
+ owner_id: Mapped[str] = mapped_column(
+ Text, ForeignKey("users.id", ondelete="CASCADE"), nullable=False
+ )
source: Mapped[str] = mapped_column(Text, nullable=False)
template_id: Mapped[str] = mapped_column(Text, nullable=False)
template_name: Mapped[str] = mapped_column(Text, nullable=False)
diff --git a/core/switch_core/main.py b/core/switch_core/main.py
index 7fc134c76..df373be96 100644
--- a/core/switch_core/main.py
+++ b/core/switch_core/main.py
@@ -1424,6 +1424,7 @@ async def run(config: SwitchConfig, process_id: str) -> None:
template_updates_task = (
asyncio.create_task(
TemplateUpdates(
+ engine=engine,
session_factory=session_factory,
management=management.service,
protocol=protocol,
diff --git a/core/switch_core/management/template_updates.py b/core/switch_core/management/template_updates.py
index e0924fa17..ae6cb0354 100644
--- a/core/switch_core/management/template_updates.py
+++ b/core/switch_core/management/template_updates.py
@@ -119,12 +119,14 @@ class TemplateUpdates:
def __init__(
self,
*,
+ engine: AsyncEngine,
session_factory: async_sessionmaker[AsyncSession],
management: ManagementService,
protocol: AgentCore,
agent_management: bool,
switch_buddy: bool,
) -> None:
+ self._engine = engine
self._session_factory = session_factory
self._management = management
self._protocol = protocol
@@ -176,11 +178,7 @@ class TemplateUpdates:
throughout, since a session-level lock is released only on the
connection that took it."""
key = func.hashtext(f"template-updates:{tenant_id}")
- async with self._session_factory() as session:
- engine = session.bind
- if not isinstance(engine, AsyncEngine):
- raise RuntimeError("template updates need sessions bound to an engine")
- async with engine.connect() as connection:
+ async with self._engine.connect() as connection:
locking = await connection.execution_options(isolation_level="AUTOCOMMIT")
held = bool(
(await locking.execute(select(func.pg_try_advisory_lock(key)))).scalar()
diff --git a/core/switch_core/migrations/versions/a4c1e7b93d20_template_installs.py b/core/switch_core/migrations/versions/a4c1e7b93d20_template_installs.py
index 4f2924f39..2dcf3e716 100644
--- a/core/switch_core/migrations/versions/a4c1e7b93d20_template_installs.py
+++ b/core/switch_core/migrations/versions/a4c1e7b93d20_template_installs.py
@@ -74,7 +74,7 @@ def upgrade() -> None:
sa.ForeignKeyConstraint(
["tenant_id"], ["tenants.id"], name="fk_template_installs_tenant"
),
- sa.ForeignKeyConstraint(["owner_id"], ["users.id"]),
+ sa.ForeignKeyConstraint(["owner_id"], ["users.id"], ondelete="CASCADE"),
sa.CheckConstraint(
"source IN ('official', 'saved')", name="ck_template_installs_source"
),
diff --git a/core/tests/switch_core/management/test_template_updates.py b/core/tests/switch_core/management/test_template_updates.py
index 4eb12bb54..75440e08d 100644
--- a/core/tests/switch_core/management/test_template_updates.py
+++ b/core/tests/switch_core/management/test_template_updates.py
@@ -105,6 +105,7 @@ def _next_revision(
def _updates(harness: Harness) -> TemplateUpdates:
return TemplateUpdates(
+ engine=harness.session_factory.kw["bind"],
session_factory=harness.session_factory,
management=harness.management.service,
protocol=harness.protocol,