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`ALTER TABLE payments ADD COLUMN IF NOT EXISTS amount_received TEXT NOT NULL DEFAULT '0'`; await sql` CREATE TABLE IF NOT EXISTS payment_txs ( id BIGSERIAL PRIMARY KEY, payment_id BIGINT NOT NULL REFERENCES payments(id) ON DELETE CASCADE, txid TEXT NOT NULL, amount TEXT NOT NULL, confirmed BOOLEAN NOT NULL DEFAULT false, detected_at TIMESTAMPTZ DEFAULT now(), UNIQUE(payment_id, txid) ) `; await sql`CREATE INDEX IF NOT EXISTS idx_payment_txs_payment ON payment_txs(payment_id)`; await sql`CREATE INDEX IF NOT EXISTS idx_payment_txs_txid ON payment_txs(txid)`; 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"); }