mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-05 14:07:05 +01:00
Merge SLH into surf-your-life on prod, drop orphaned box DB, remove SLH from hetzner manifest, and wire Bitbaum user_projects dir for local dispatch. Co-authored-by: Cursor <cursoragent@cursor.com>
81 lines
3.3 KiB
TypeScript
81 lines
3.3 KiB
TypeScript
/**
|
|
* Wire prod user_projects rows to local dir paths (Fleet Runner dispatch).
|
|
*
|
|
* Usage:
|
|
* npx tsx scripts/db/link-prod-runtime.ts # dry run
|
|
* npx tsx scripts/db/link-prod-runtime.ts --apply # execute
|
|
*
|
|
* Env: FLEETCROWN_DB_PASSWORD + HETZNER_IP from .env.hetzner.local
|
|
*/
|
|
import { config } from "dotenv";
|
|
|
|
config({ path: ".env.hetzner.local" });
|
|
|
|
const APPLY = process.argv.includes("--apply");
|
|
|
|
/** Entity display name → absolute dir on the operator machine. */
|
|
const RUNTIME_LINKS: Array<{ name: string; dirPath: string; reason: string }> = [
|
|
{ name: "Bitbaum", dirPath: "/home/g/dev/bitbaum", reason: "Entity enriched but missing user_projects dir" },
|
|
];
|
|
|
|
async function main() {
|
|
const password = process.env.FLEETCROWN_DB_PASSWORD;
|
|
const host = process.env.HETZNER_IP;
|
|
if (!password || !host) throw new Error("FLEETCROWN_DB_PASSWORD / HETZNER_IP missing from .env.hetzner.local");
|
|
process.env.DATABASE_URL = `postgres://fleetcrown:${encodeURIComponent(password)}@${host}:5432/fleetcrown?sslmode=require`;
|
|
|
|
const { db } = await import("../../src/db");
|
|
const { entities, users, userProjects } = await import("../../src/db/schema");
|
|
const { and, eq, sql } = await import("drizzle-orm");
|
|
const { ENTITY_TYPE } = await import("../../src/lib/constants/statuses");
|
|
const { upsertLocalUserProject } = await import("../../src/db/queries/user-projects");
|
|
const { findProjectEntityByName } = await import("../../src/db/queries/project-merge");
|
|
|
|
const projectOwners = await db
|
|
.select({ userId: entities.userId, count: sql<number>`count(*)::int` })
|
|
.from(entities)
|
|
.where(eq(entities.type, ENTITY_TYPE.PROJECT))
|
|
.groupBy(entities.userId);
|
|
const ownerCounts = new Map(projectOwners.map((r) => [r.userId, r.count]));
|
|
const allUsers = await db.select({ id: users.id, email: users.email, name: users.name }).from(users);
|
|
const ownerUser = allUsers
|
|
.filter((u) => (ownerCounts.get(u.id) ?? 0) > 0)
|
|
.sort((a, b) => (ownerCounts.get(b.id) ?? 0) - (ownerCounts.get(a.id) ?? 0))[0];
|
|
if (!ownerUser) throw new Error("No project owner found");
|
|
const userId = ownerUser.id;
|
|
|
|
console.log(`${APPLY ? "Applying" : "Dry run"} link-prod-runtime for ${ownerUser.name ?? ownerUser.email}\n`);
|
|
|
|
for (const link of RUNTIME_LINKS) {
|
|
const entity = await findProjectEntityByName(userId, link.name);
|
|
if (!entity) {
|
|
console.log(`— skip ${link.name}: entity not found`);
|
|
continue;
|
|
}
|
|
|
|
const [existing] = await db
|
|
.select({ dir: userProjects.dirPath, entityId: userProjects.entityProjectId })
|
|
.from(userProjects)
|
|
.where(and(eq(userProjects.userId, userId), sql`lower(${userProjects.name}) = lower(${link.name.trim()})`));
|
|
|
|
if (existing?.dir === link.dirPath && existing.entityId === entity.id) {
|
|
console.log(`— skip ${link.name}: already linked (${link.dirPath})`);
|
|
continue;
|
|
}
|
|
|
|
console.log(`${APPLY ? "✚" : "DRY"} ${link.name} → ${link.dirPath} — ${link.reason}`);
|
|
if (APPLY) {
|
|
await upsertLocalUserProject({ userId, name: link.name, dirPath: link.dirPath });
|
|
console.log(` linked entity ${entity.id.slice(0, 8)}`);
|
|
}
|
|
}
|
|
|
|
if (!APPLY) console.log("\nRe-run with --apply to write changes.");
|
|
process.exit(0);
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error(err);
|
|
process.exit(1);
|
|
});
|