Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

revert(db): remove 0018 migration that causes data loss on D1

Gener63fb218c380e
diff --git a/src/shared/src/db/queries/runtime.ts b/src/shared/src/db/queries/runtime.ts
index acc725e20..427992741 100644
--- a/src/shared/src/db/queries/runtime.ts
+++ b/src/shared/src/db/queries/runtime.ts
@@ -1,5 +1,5 @@
import { eq, and, asc, sql } from "drizzle-orm";
-import { agentRuntime, machine } from "../schema";
+import { agentRuntime, agent, machine } from "../schema";
import type { Database } from "../index";
export async function upsertAgentRuntime(
@@ -114,6 +114,25 @@ export async function deleteRuntimesByDaemonId(
daemonId: string,
workspaceId: string
) {
+ const runtimes = await db
+ .select({ id: agentRuntime.id })
+ .from(agentRuntime)
+ .where(
+ and(
+ eq(agentRuntime.daemonId, daemonId),
+ eq(agentRuntime.workspaceId, workspaceId)
+ )
+ );
+
+ if (runtimes.length === 0) return;
+
+ for (const r of runtimes) {
+ await db
+ .update(agent)
+ .set({ runtimeId: null, updatedAt: new Date().toISOString() })
+ .where(eq(agent.runtimeId, r.id));
+ }
+
await db
.delete(agentRuntime)
.where(
diff --git a/src/shared/src/db/schema.ts b/src/shared/src/db/schema.ts
index 5edf7e450..870031c52 100644
--- a/src/shared/src/db/schema.ts
+++ b/src/shared/src/db/schema.ts
@@ -211,13 +211,13 @@ export const agent = sqliteTable(
description: text("description").notNull().default(""),
instructions: text("instructions").notNull().default(""),
avatarUrl: text("avatar_url"),
- runtimeId: text("runtime_id").references(() => agentRuntime.id, { onDelete: "set null" }),
+ runtimeId: text("runtime_id").references(() => agentRuntime.id),
runtimeMode: text("runtime_mode").notNull().default("local"),
runtimeConfig: text("runtime_config", { mode: "json" }),
visibility: text("visibility").notNull().default("private"),
status: text("status").notNull().default("idle"),
maxConcurrentTasks: integer("max_concurrent_tasks").notNull().default(6),
- ownerId: text("owner_id").references(() => user.id, { onDelete: "cascade" }),
+ ownerId: text("owner_id").references(() => user.id),
tools: text("tools", { mode: "json" }),
triggers: text("triggers", { mode: "json" }),
emailHandle: text("email_handle").unique(),
@@ -582,9 +582,5 @@ export const workspaceFileRequest = sqliteTable(
},
(t) => [
index("idx_wfr_workspace_status").on(t.workspaceId, t.status),
- foreignKey({
- columns: [t.agentId, t.workspaceId],
- foreignColumns: [agent.id, agent.workspaceId],
- }).onDelete("cascade"),
]
);
diff --git a/src/web/migrations/0018_schema_cascade_cleanup.sql b/src/web/migrations/0018_schema_cascade_cleanup.sql
deleted file mode 100644
index 495c431d3..000000000
--- a/src/web/migrations/0018_schema_cascade_cleanup.sql
+++ /dev/null
@@ -1,61 +0,0 @@
--- Fix FK cascade behavior for agent and workspace_file_request tables:
--- 1. agent.runtime_id: nullable + ON DELETE SET NULL — runtime removal keeps agent alive
--- 2. agent.owner_id: ON DELETE CASCADE — deleting user deletes their agents
--- 3. workspace_file_request: add composite FK to agent(id, workspace_id) ON DELETE CASCADE
-
-PRAGMA foreign_keys = OFF;
-
--- Rebuild agent table with corrected FK constraints
-CREATE TABLE agent_new (
- id TEXT NOT NULL,
- workspace_id TEXT NOT NULL REFERENCES workspace(id) ON DELETE CASCADE,
- name TEXT NOT NULL,
- description TEXT NOT NULL DEFAULT '',
- instructions TEXT NOT NULL DEFAULT '',
- avatar_url TEXT,
- runtime_id TEXT REFERENCES agent_runtime(id) ON DELETE SET NULL,
- runtime_mode TEXT NOT NULL DEFAULT 'local',
- runtime_config TEXT,
- visibility TEXT NOT NULL DEFAULT 'private',
- status TEXT NOT NULL DEFAULT 'idle',
- max_concurrent_tasks INTEGER NOT NULL DEFAULT 6,
- owner_id TEXT REFERENCES user(id) ON DELETE CASCADE,
- tools TEXT,
- triggers TEXT,
- email_handle TEXT UNIQUE,
- created_at TEXT NOT NULL DEFAULT (datetime('now')),
- updated_at TEXT NOT NULL DEFAULT (datetime('now')),
- PRIMARY KEY (id, workspace_id)
-);
-
-INSERT INTO agent_new SELECT * FROM agent;
-
-DROP TABLE agent;
-
-ALTER TABLE agent_new RENAME TO agent;
-
--- Rebuild workspace_file_request with composite FK to agent
-CREATE TABLE workspace_file_request_new (
- id TEXT PRIMARY KEY,
- workspace_id TEXT NOT NULL REFERENCES workspace(id) ON DELETE CASCADE,
- agent_id TEXT NOT NULL,
- request_type TEXT NOT NULL,
- path TEXT NOT NULL DEFAULT '.',
- status TEXT NOT NULL DEFAULT 'pending',
- result TEXT,
- created_at TEXT NOT NULL DEFAULT (datetime('now')),
- updated_at TEXT NOT NULL DEFAULT (datetime('now')),
- FOREIGN KEY (agent_id, workspace_id) REFERENCES agent(id, workspace_id) ON DELETE CASCADE
-);
-
-INSERT INTO workspace_file_request_new SELECT * FROM workspace_file_request;
-
-DROP TABLE workspace_file_request;
-
-ALTER TABLE workspace_file_request_new RENAME TO workspace_file_request;
-
-CREATE INDEX idx_wfr_workspace_status ON workspace_file_request(workspace_id, status);
-
-PRAGMA foreign_keys = ON;
-
-PRAGMA foreign_key_check;