mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-12-04 02:29:06 +00:00
feat(pwa): move app.logicsrc.com from Turso/libSQL to Postgres via @profullstack/libsql-pg (#218)
Production reads DATABASE_URL (postgres://) through @profullstack/libsql-pg, which keeps the @libsql/client surface; no query changed. A missing, non-Postgres or leftover libsql:// URL fails at boot in production. Dev and tests keep libSQL (file:/:memory:) as a devDependency. Postgres migrations in src/migrations-pg/ (converted with libsql-pg convert-schema, same file names so the copied _migrations ledger matches); migrate.mjs picks the dialect. Tests run against Postgres with PWA_TEST_DATABASE_URL. Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
c535aaa32c
commit
4ac750e65b
12 changed files with 507 additions and 29 deletions
|
|
@ -1,11 +1,19 @@
|
|||
// Apply SQL migrations in order. Idempotent — tracks applied files in _migrations.
|
||||
//
|
||||
// Two copies of every migration exist, one per dialect, with the SAME file
|
||||
// names: `migrations/` (SQLite, for the local dev/test database) and
|
||||
// `migrations-pg/` (Postgres, generated with `npx libsql-pg convert-schema`
|
||||
// and reviewed). The ledger is keyed by file name, so a database copied from
|
||||
// Turso with `libsql-pg copy` carries its `_migrations` rows across and the
|
||||
// Postgres side recognises them as applied.
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { db, run, all } from "./db.mjs";
|
||||
import { db, run, all, isPostgres } from "./db.mjs";
|
||||
|
||||
const HERE = path.dirname(fileURLToPath(import.meta.url));
|
||||
const DIR = path.join(HERE, "migrations");
|
||||
export const MIGRATIONS_DIR = path.join(HERE, isPostgres ? "migrations-pg" : "migrations");
|
||||
const DIR = MIGRATIONS_DIR;
|
||||
|
||||
export async function migrate() {
|
||||
// Bootstrap the tracking table (the first migration also declares it IF NOT EXISTS).
|
||||
|
|
@ -16,7 +24,7 @@ export async function migrate() {
|
|||
for (const file of files) {
|
||||
if (done.has(file)) { console.log(`· ${file} (already applied)`); continue; }
|
||||
const sql = fs.readFileSync(path.join(DIR, file), "utf8");
|
||||
// libSQL executes one statement per call — split on semicolons at line ends.
|
||||
// One statement per call (libSQL requires it; the Postgres shim binds per statement) — split on semicolons at line ends.
|
||||
const statements = sql.split(/;\s*(?:\n|$)/).map((s) => s.trim()).filter(Boolean);
|
||||
for (const stmt of statements) await run(stmt);
|
||||
await run(`INSERT INTO _migrations (name, applied_at) VALUES (?, ?)`, [file, Date.now()]);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue