## Summary CLI commands, daemon/dashboard startup, packaged desktop startup, and live-data maintenance scripts now share the mandatory PostgreSQL lifecycle. Operators no longer risk a command silently reading or writing a disconnected SQLite shadow when PostgreSQL setup fails. ## Design decisions - Every startup owner retains and awaits its PostgreSQL shutdown callback, including partial-startup failure paths. - CLI project context and lock-retry flows resolve through asynchronous project stores. - Maintenance scripts use the shared backend helper; explicit database migration/inspection remains the only CLI surface allowed to read legacy SQLite sources. ## Validation - CLI and Desktop typechecks pass on the stacked branch. - `pnpm test:gate` passes all 478 gate tests. - This PR changes 54 files. ## Stack - Depends on #2109, which depends on #2108. - Bundled plugins and docs/release follow in later PRs. Related: #2105 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * PostgreSQL is now the authoritative store for structured project and task metadata. * Projects can be recognized and initialized using `.fusion/project.json`, without creating a legacy SQLite database. * CLI commands now retry transient PostgreSQL contention errors. * **Bug Fixes** * Improved cleanup when commands complete, fail, or run in the background, preventing lingering resources. * Improved desktop, server, and session shutdown reliability. * **Documentation** * Updated storage and standalone binary guidance to reflect PostgreSQL and legacy SQLite compatibility. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
96 lines
3.9 KiB
JavaScript
96 lines
3.9 KiB
JavaScript
/**
|
|
* FNXC:PostgresCutover 2026-07-05-13:00:
|
|
* Shared PostgreSQL backend access for operational scripts.
|
|
*
|
|
* The ops/maintenance scripts under scripts/ used to open `.fusion/fusion.db`
|
|
* directly (node:sqlite or the sqlite3 CLI). After the PostgreSQL cutover the
|
|
* live data lives in the embedded PostgreSQL cluster (or an external cluster
|
|
* via DATABASE_URL), so a direct SQLite open would silently operate on a
|
|
* stale/empty marker file. Every script now boots the real backend through
|
|
* @fusion/core's startup factory via this helper.
|
|
*
|
|
* Requires packages/core to be built (`pnpm --filter @fusion/core build`).
|
|
*/
|
|
import { cpSync, existsSync, readdirSync } from "node:fs";
|
|
import { resolve, dirname } from "node:path";
|
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
|
|
const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..");
|
|
|
|
/**
|
|
* Stage the PostgreSQL migration SQL into core's dist. `tsc` emits
|
|
* only JS, so dist lacks src/postgres/migrations/*.sql; the schema applier
|
|
* resolves them relative to the compiled file (__dirname/migrations). The CLI
|
|
* bundle does the same staging in packages/cli/tsup.config.ts.
|
|
*/
|
|
function ensureMigrationsStaged() {
|
|
const src = resolve(repoRoot, "packages/core/src/postgres/migrations");
|
|
const dest = resolve(repoRoot, "packages/core/dist/postgres/migrations");
|
|
/*
|
|
* FNXC:AutomationIsolation 2026-07-13-22:37:
|
|
* Operational scripts must stage every versioned PostgreSQL migration, not merely the initial baseline, so an already-initialized database receives the automation project-isolation upgrade before scripts open it.
|
|
*/
|
|
const requiredMigrations = existsSync(src)
|
|
? readdirSync(src).filter((file) => file.endsWith(".sql"))
|
|
: [];
|
|
if (existsSync(src) && requiredMigrations.some((file) => !existsSync(resolve(dest, file)))) {
|
|
cpSync(src, dest, { recursive: true });
|
|
}
|
|
}
|
|
|
|
async function importCore() {
|
|
ensureMigrationsStaged();
|
|
try {
|
|
return await import(pathToFileURL(resolve(repoRoot, "packages/core/dist/index.js")).href);
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : String(error);
|
|
throw new Error(
|
|
`Unable to import packages/core/dist/index.js (${message}). Run: pnpm --filter @fusion/core build`,
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Boot a PostgreSQL-backed TaskStore for the given project root.
|
|
*
|
|
* Returns `{ core, store, asyncLayer, sql, schema, shutdown }`:
|
|
* - `core` — the @fusion/core module (for helpers like recordRunAuditEvent).
|
|
* - `store` — the initialized TaskStore (backend mode).
|
|
* - `asyncLayer` — the AsyncDataLayer; `asyncLayer.db.execute(sql\`...\`)`
|
|
* runs raw SQL. Project tables are schema-qualified (`project."tasks"`).
|
|
* - `sql` — the drizzle-orm `sql` template tag (re-exported as drizzleSql).
|
|
* - `schema` — postgresSchema (drizzle table objects, e.g. schema.project.tasks).
|
|
* - `shutdown` — releases the pool and stops an embedded cluster this boot
|
|
* started. Always call it in `finally`.
|
|
*
|
|
* Throws when PostgreSQL cannot start. These scripts must never fall back to
|
|
* the removed SQLite runtime.
|
|
*/
|
|
export async function openBackend(rootDir = process.cwd()) {
|
|
const core = await importCore();
|
|
const boot = await core.createTaskStoreForBackend({ rootDir });
|
|
const asyncLayer = boot.taskStore.getAsyncLayer();
|
|
if (!asyncLayer) {
|
|
await boot.shutdown().catch(() => {});
|
|
throw new Error("Backend TaskStore has no AsyncDataLayer; cannot run this script.");
|
|
}
|
|
return {
|
|
core,
|
|
store: boot.taskStore,
|
|
asyncLayer,
|
|
sql: core.drizzleSql,
|
|
schema: core.postgresSchema,
|
|
shutdown: boot.shutdown,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Normalize a drizzle `db.execute(...)` result to a plain array of rows.
|
|
* postgres-js returns a RowList (array-like); this keeps call sites simple.
|
|
*/
|
|
export function rowsOf(result) {
|
|
if (Array.isArray(result)) return [...result];
|
|
if (result && Array.isArray(result.rows)) return [...result.rows];
|
|
return [];
|
|
}
|