62 lines
2.3 KiB
TypeScript
62 lines
2.3 KiB
TypeScript
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 columns 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`ALTER TABLE accounts ADD COLUMN IF NOT EXISTS plan_stack JSONB NOT NULL DEFAULT '[]'`;
|
|
|
|
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,
|
|
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)`;
|
|
|
|
await sql`ALTER TABLE payments ADD COLUMN IF NOT EXISTS receipt_html TEXT`;
|
|
|
|
// Derivation index should be unique per coin, not globally
|
|
await sql`DROP INDEX IF EXISTS payments_derivation_index_key`;
|
|
await sql`CREATE UNIQUE INDEX IF NOT EXISTS idx_payments_coin_derivation ON payments(coin, derivation_index)`;
|
|
|
|
console.log("Pay DB ready");
|
|
}
|