import postgres from "postgres"; const sql = postgres(process.env.DATABASE_URL ?? "postgres://pingql:pingql@localhost:5432/pingql", { max: 10, idle_timeout: 30, connect_timeout: 10, }); export default sql; export async function migrate() { // Plan expiry on accounts (may already exist from API/web migrations) await sql`ALTER TABLE accounts ADD COLUMN IF NOT EXISTS plan_expires_at TIMESTAMPTZ`; await sql` CREATE TABLE IF NOT EXISTS payments ( id BIGSERIAL PRIMARY KEY, account_id UUID NOT NULL REFERENCES accounts(id) ON DELETE CASCADE, plan TEXT NOT NULL, months INTEGER, amount_usd NUMERIC(10,2) NOT NULL, coin TEXT NOT NULL, amount_crypto TEXT NOT NULL, address TEXT NOT NULL, derivation_index INTEGER NOT NULL UNIQUE, status TEXT NOT NULL DEFAULT 'pending', created_at TIMESTAMPTZ DEFAULT now(), paid_at TIMESTAMPTZ, expires_at TIMESTAMPTZ NOT NULL, txid TEXT ) `; await sql`CREATE INDEX IF NOT EXISTS idx_payments_status ON payments(status)`; await sql`CREATE INDEX IF NOT EXISTS idx_payments_account ON payments(account_id)`; console.log("Pay DB ready"); }