SPB Git forge
38commits 1branches 0releases
338.7 MBsize
maindefault branch
3 h agolast push
HTML 53.9% TypeScript 44.5% JavaScript 0.6% SQL 0.5%
1.2 KB · 27 lines typescript
Raw Blame History
1import { readdirSync, readFileSync } from "node:fs";2import { join, dirname } from "node:path";3import { fileURLToPath } from "node:url";4import { getSql, closeDb } from "./index.js";56/** Plain-SQL migrations in ./migrations, applied in lexical order, tracked in _dci_migrations. Idempotent. */7async function main() {8  const sql = getSql();9  const dir = join(dirname(fileURLToPath(import.meta.url)), "..", "migrations");10  await sql`create table if not exists _dci_migrations (name text primary key, applied_at timestamptz not null default now())`;11  const done = new Set((await sql`select name from _dci_migrations`).map((r) => r.name as string));12  const files = readdirSync(dir).filter((f) => f.endsWith(".sql")).sort();13  for (const f of files) {14    if (done.has(f)) continue;15    const body = readFileSync(join(dir, f), "utf8");16    process.stdout.write(`applying ${f} … `);17    await sql.begin(async (tx) => {18      await tx.unsafe(body);19      await tx`insert into _dci_migrations (name) values (${f})`;20    });21    console.log("ok");22  }23  console.log(`migrations: ${files.length} total, ${files.length - done.size} applied now`);24  await closeDb();25}26main().catch((e) => { console.error(e); process.exit(1); });27