forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path012_user_balance_and_trigger.sql
More file actions
53 lines (47 loc) · 1.81 KB
/
Copy path012_user_balance_and_trigger.sql
File metadata and controls
53 lines (47 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
-- Migration 012: Create user_balance table and sync trigger
-- Keeps a running balance per user, updated atomically on every point_transaction insert.
-- Requirements: #190
CREATE TABLE IF NOT EXISTS user_balance (
user_id INTEGER PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE,
balance INTEGER NOT NULL DEFAULT 0 CHECK (balance >= 0),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- Index for fast balance lookups (PK covers it, but explicit for clarity)
CREATE INDEX IF NOT EXISTS idx_user_balance_user_id ON user_balance (user_id);
-- -----------------------------------------------------------------------
-- Trigger function: upsert user_balance on every point_transaction insert
-- -----------------------------------------------------------------------
CREATE OR REPLACE FUNCTION sync_user_balance()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO user_balance (user_id, balance, updated_at)
VALUES (NEW.user_id, NEW.balance_after, NOW())
ON CONFLICT (user_id)
DO UPDATE SET
balance = NEW.balance_after,
updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Attach trigger to point_transactions (fires after each INSERT)
DROP TRIGGER IF EXISTS trg_sync_user_balance ON point_transactions;
CREATE TRIGGER trg_sync_user_balance
AFTER INSERT ON point_transactions
FOR EACH ROW
EXECUTE FUNCTION sync_user_balance();
-- Seed user_balance from existing point_transactions (idempotent)
INSERT INTO user_balance (user_id, balance)
SELECT
user_id,
GREATEST(0, SUM(
CASE
WHEN type IN ('earned', 'bonus', 'referral') THEN amount
WHEN type IN ('redeemed', 'expired') THEN -amount
ELSE 0
END
))
FROM point_transactions
GROUP BY user_id
ON CONFLICT (user_id) DO UPDATE
SET balance = EXCLUDED.balance,
updated_at = NOW();