nxtgauge-backend-rust/crates/db/migrations/20260627030000_wallet_full.up.sql
Ashwin Kumar Sivakumar f007ca0999
All checks were successful
build-and-release / build (gateway) (push) Successful in 49s
build-and-release / build (employees) (push) Successful in 1m46s
build-and-release / build (ugc-content-creators) (push) Successful in 2m36s
build-and-release / build (companies) (push) Successful in 1m55s
build-and-release / build (catering-services) (push) Successful in 2m48s
build-and-release / build (fitness-trainers) (push) Successful in 1m45s
build-and-release / build (makeup-artists) (push) Successful in 1m43s
build-and-release / build (payments) (push) Successful in 2m52s
build-and-release / build (tutors) (push) Successful in 2m54s
build-and-release / build (job-seekers) (push) Successful in 7m28s
build-and-release / build (developers) (push) Successful in 2m28s
build-and-release / build (photographers) (push) Successful in 2m37s
build-and-release / build (video-editors) (push) Successful in 2m32s
build-and-release / build (users) (push) Successful in 4m55s
build-and-release / build (social-media-managers) (push) Successful in 7m32s
build-and-release / build (customers) (push) Successful in 2m11s
build-and-release / build (cron) (push) Successful in 2m14s
build-and-release / build (jobs) (push) Successful in 39s
build-and-release / build (graphic-designers) (push) Successful in 2m0s
fix(db): re-enable wallet_full migration (tracecoin_holds/buckets)
20260627030000_wallet_full.up.sql was disabled in 0163349 as part of a
bulk cleanup of migrations referencing the dropped professionals
table, but this one doesn't touch that table at all - it only
references tracecoin_wallets, tracecoin_ledger and users, all of
which already exist in prod. It was swept up by mistake.

Without it, tracecoin_holds/tracecoin_buckets never got created, so
the new GET /wallet/me/holds route (crates/contracts/src/profession_shared.rs)
500s with 'relation "tracecoin_holds" does not exist'.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-12 23:18:51 +05:30

65 lines
3.5 KiB
SQL

-- ──────────────────────────────────────────────────────────────────────────
-- Tracecoin wallet — full virtual-currency semantics
-- ──────────────────────────────────────────────────────────────────────────
-- Adds the safety machinery the wallet needs without putting a cap on
-- what users can buy or spend:
--
-- * Holds / escrow for pending actions (lead requests, contact
-- unlocks, AI credits, etc.). Holds have an explicit lifecycle
-- `ACTIVE → SETTLED / RELEASED / EXPIRED` and an optional
-- `expires_at` so the cron can auto-release them. This is what
-- prevents double-spending and spam without limiting purchases.
-- * Per-purchase bucket tracking (FIFO consumption, optional expiry).
-- Each row is one "bucket" of tracecoins the user bought. The
-- "first in, first out" rule means tracecoins purchased earlier
-- expire first if expiry is enabled.
-- 1. Per-purchase bucket tracking (FIFO consumption)
-- Each row is one "bucket" of tracecoins the user bought. amount_remaining
-- shrinks as the user spends; when it hits zero, the bucket is "drained"
-- and ignored by future spend operations. expires_at drives automatic
-- bucket-level expiry (NULL = no expiry for this bucket).
CREATE TABLE IF NOT EXISTS tracecoin_buckets (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
source VARCHAR(50) NOT NULL,
amount INTEGER NOT NULL,
amount_remaining INTEGER NOT NULL,
reference_id UUID,
source_kind VARCHAR(50) NOT NULL DEFAULT 'PURCHASE',
expires_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CHECK (amount_remaining >= 0)
);
CREATE INDEX IF NOT EXISTS idx_tracecoin_buckets_user_expiry
ON tracecoin_buckets(user_id, expires_at NULLS LAST, created_at);
CREATE UNIQUE INDEX IF NOT EXISTS uq_tracecoin_buckets_reference
ON tracecoin_buckets(reference_id)
WHERE reference_id IS NOT NULL;
-- 2. Holds / escrow for pending actions
-- ACTIVE: tracecoins are still on hold (subtracted from `available`)
-- SETTLED: the action completed and the hold was converted to a permanent
-- debit (a ledger row is written and linked)
-- RELEASED: the action was cancelled by the user or the system
-- EXPIRED: the hold passed `expires_at` and was auto-released by cron
CREATE TABLE IF NOT EXISTS tracecoin_holds (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
wallet_id UUID NOT NULL REFERENCES tracecoin_wallets(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
amount INTEGER NOT NULL CHECK (amount > 0),
reason VARCHAR(100) NOT NULL,
reference_id UUID,
status VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
expires_at TIMESTAMPTZ,
settled_at TIMESTAMPTZ,
settled_ledger_id UUID REFERENCES tracecoin_ledger(id),
released_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CHECK (status IN ('ACTIVE', 'SETTLED', 'RELEASED', 'EXPIRED'))
);
CREATE UNIQUE INDEX IF NOT EXISTS uq_tracecoin_holds_reference
ON tracecoin_holds(reference_id)
WHERE reference_id IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_tracecoin_holds_user
ON tracecoin_holds(user_id, status, expires_at);