import { readdir,readFile } from "node:fs/promises"; import { db } from "../src/lib/db"; import { ensureBucket } from "../src/lib/storage"; import { setTimeout } from "node:timers/promises"; async function main(){ for(let i=0;;i++){try{await db().query('SELECT 1');break;}catch(e){if(i>=60)throw e;await setTimeout(2000);}} const client=await db().connect(); try{ await client.query('SELECT pg_advisory_lock(18131001)'); for(const name of (await readdir('db')).filter(n=>n.endsWith('.sql')).sort()){ const present=await client.query("SELECT to_regclass('public.schema_migrations') AS table"); if(present.rows[0].table && (await client.query('SELECT 1 FROM schema_migrations WHERE version=$1',[name.replace('.sql','')])).rowCount)continue; await client.query(await readFile('db/'+name,'utf8'));console.log('Applied migration '+name); } }catch(error){await client.query('ROLLBACK');throw error;} finally{await client.query('SELECT pg_advisory_unlock(18131001)');client.release();} for(let i=0;;i++){try{await ensureBucket();break;}catch(e){if(i>=90)throw e;await setTimeout(2000);}} console.log('PostgreSQL and S3 are ready');await db().end(); } main().catch(error=>{console.error('Storage bootstrap failed:',error instanceof Error?error.message:'Unknown error');process.exit(1);});