import {readFile,readdir} from "node:fs/promises";
import {dirname,join} from "node:path";
import {fileURLToPath} from "node:url";
import pg from "pg";
const cfg=process.env.DATABASE_URL?{connectionString:process.env.DATABASE_URL}:{host:process.env.PGHOST||"127.0.0.1",port:Number(process.env.PGPORT||5432),database:process.env.PGDATABASE,user:process.env.PGUSER,password:process.env.PGPASSWORD};
const root=dirname(dirname(fileURLToPath(import.meta.url))),dir=join(root,"server-migrations"),files=(await readdir(dir)).filter(n=>n.endsWith(".sql")).sort();if(!files.length)throw Error("No PostgreSQL migration found");const pool=new pg.Pool({...cfg,max:1});try{for(const f of files){await pool.query((await readFile(join(dir,f),"utf8")).replaceAll("--> statement-breakpoint",""));console.log(`Applied ${f}`)}}finally{await pool.end()}
