51 lines
2.8 KiB
TypeScript
51 lines
2.8 KiB
TypeScript
// PEP-246: after the move to the server (PEP-230) finished sessions kept their working copy paths
|
|
// from the Mac (`/Users/…`); `server-cutover.sh` cleared only the live ones. A relaunch of such a
|
|
// session failed with ENOENT. `runnerCwd` now makes a new copy itself; this finds what is left
|
|
// (CLI `peppa session paths [--fix]`): sessions and projects with a path this host doesn't have,
|
|
// settings that mention `/Users/`.
|
|
import { existsSync } from "node:fs";
|
|
import { FINISHED_SESSION_STATUSES } from "@peppa/core";
|
|
import { projects, sessions, settings, type Db } from "@peppa/db";
|
|
import { and, eq, isNotNull, isNull, sql } from "drizzle-orm";
|
|
import { worktreePath } from "./worktree.ts";
|
|
|
|
export interface ForeignPaths {
|
|
sessions: Array<{ id: number; projectId: number; status: string; path: string; fixTo: string | null }>;
|
|
projects: Array<{ id: number; key: string; path: string }>;
|
|
settings: Array<{ key: string }>;
|
|
}
|
|
|
|
/** What still points to a path this host doesn't have. Sessions on a separate host are skipped. */
|
|
export async function findForeignPaths(db: Db): Promise<ForeignPaths> {
|
|
const slugs = new Map((await db.select({ id: projects.id, slug: projects.slug }).from(projects)).map((p) => [p.id, p.slug]));
|
|
const rows = await db
|
|
.select({ id: sessions.id, projectId: sessions.projectId, status: sessions.status, path: sessions.workspacePath })
|
|
.from(sessions)
|
|
.where(and(isNotNull(sessions.workspacePath), isNull(sessions.hostId)));
|
|
const out: ForeignPaths = { sessions: [], projects: [], settings: [] };
|
|
for (const r of rows) {
|
|
if (!r.path || existsSync(r.path)) continue;
|
|
const slug = slugs.get(r.projectId);
|
|
// Only finished sessions are rewritten: a live one is the watchdog's (its relaunch makes the copy).
|
|
const fixTo = slug && FINISHED_SESSION_STATUSES.includes(r.status) ? worktreePath({ slug }, r.id) : null;
|
|
out.sessions.push({ id: r.id, projectId: r.projectId, status: r.status, path: r.path, fixTo: fixTo !== r.path ? fixTo : null });
|
|
}
|
|
for (const p of await db.select({ id: projects.id, key: projects.key, path: projects.workspacePath }).from(projects).where(isNull(projects.deletedAt))) {
|
|
if (!existsSync(p.path)) out.projects.push(p);
|
|
}
|
|
// Values are JSON text: a LIKE over the raw value finds a path anywhere inside (a one-off scan).
|
|
for (const s of await db.select({ key: settings.key }).from(settings).where(sql`${settings.value} LIKE '%/Users/%'`)) out.settings.push(s);
|
|
return out;
|
|
}
|
|
|
|
/** Rewrites the paths of finished sessions to the standard place (where `runnerCwd` would make the copy). */
|
|
export async function fixForeignPaths(db: Db, found: ForeignPaths): Promise<number> {
|
|
let n = 0;
|
|
for (const s of found.sessions) {
|
|
if (!s.fixTo) continue;
|
|
await db.update(sessions).set({ workspacePath: s.fixTo }).where(eq(sessions.id, s.id));
|
|
n++;
|
|
}
|
|
return n;
|
|
}
|