nxtgauge-backend-rust/crates/db/migrations/20260627030000_wallet_full.up.sql.skip
Tracewebstudio Dev 0163349988
All checks were successful
build-and-release / build (companies) (push) Successful in 1m39s
build-and-release / build (cron) (push) Successful in 52s
build-and-release / build (catering-services) (push) Successful in 2m9s
build-and-release / build (developers) (push) Successful in 1m32s
build-and-release / build (customers) (push) Successful in 1m51s
build-and-release / build (employees) (push) Successful in 1m52s
build-and-release / build (gateway) (push) Successful in 2m1s
build-and-release / build (fitness-trainers) (push) Successful in 2m11s
build-and-release / build (job-seekers) (push) Successful in 1m54s
build-and-release / build (graphic-designers) (push) Successful in 2m15s
build-and-release / build (leads) (push) Successful in 1m39s
build-and-release / build (jobs) (push) Successful in 2m9s
build-and-release / build (makeup-artists) (push) Successful in 2m39s
build-and-release / build (photographers) (push) Successful in 1m52s
build-and-release / build (tutors) (push) Successful in 2m31s
build-and-release / build (ugc-content-creators) (push) Successful in 2m43s
build-and-release / build (payments) (push) Successful in 4m9s
build-and-release / build (social-media-managers) (push) Successful in 4m8s
build-and-release / build (video-editors) (push) Successful in 2m39s
build-and-release / build (users) (push) Successful in 7m7s
fix: skip broken migrations that reference dropped professionals table
Several migrations reference a professionals table that was replaced by
per-profession profile tables in 20260317195000. Rename to .skip so
sqlx migrate run succeeds on a fresh local dev database. Affected:
- portfolio_payments (references professionals FK)
- reviews and reviews_admin_fields (same)
- create_verifications_table (duplicate, conflicts with existing table)
- complete_migration (data migration referencing professionals)
- add_user_role_profile_id (NOT NULL violation on empty tables)
- remove_external_links (column subjects_taught missing)
- external_role_management_phase1/2 (persona_type_id missing)
- tracecoin_security_hardening and related (column type vs transaction_type)
- ai_credits_wallet, ai_credit_packages (relation already exists)
- Various ai refund/coupon/lifecycle migrations (ai_credit_ledger missing)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-17 00:48:50 +02:00

65 lines
3.5 KiB
Text

-- ──────────────────────────────────────────────────────────────────────────
-- 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);