-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase_split_migration.sql
More file actions
60 lines (51 loc) · 2.24 KB
/
Copy pathsupabase_split_migration.sql
File metadata and controls
60 lines (51 loc) · 2.24 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
54
55
56
57
58
59
60
-- ExpoPay SPLIT Feature Migration
-- Run this in your Supabase SQL Editor
CREATE TABLE IF NOT EXISTS split_bills (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
creator_id UUID REFERENCES profiles(id) ON DELETE CASCADE NOT NULL,
creator_universal_id TEXT NOT NULL,
title TEXT NOT NULL,
note TEXT,
total_amount DECIMAL(18, 7) NOT NULL,
currency TEXT NOT NULL DEFAULT 'XLM',
status TEXT NOT NULL DEFAULT 'active', -- active | completed | cancelled
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS split_participants (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
split_id UUID REFERENCES split_bills(id) ON DELETE CASCADE NOT NULL,
user_id UUID REFERENCES profiles(id) ON DELETE CASCADE NOT NULL,
universal_id TEXT NOT NULL,
stellar_address TEXT NOT NULL,
amount_owed DECIMAL(18, 7) NOT NULL,
status TEXT NOT NULL DEFAULT 'pending', -- pending | paid
tx_hash TEXT,
paid_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
-- Indexes for performance
CREATE INDEX IF NOT EXISTS idx_split_bills_creator ON split_bills(creator_id);
CREATE INDEX IF NOT EXISTS idx_split_participants_split ON split_participants(split_id);
CREATE INDEX IF NOT EXISTS idx_split_participants_user ON split_participants(user_id);
CREATE INDEX IF NOT EXISTS idx_split_participants_status ON split_participants(status);
-- RLS Policies
ALTER TABLE split_bills ENABLE ROW LEVEL SECURITY;
ALTER TABLE split_participants ENABLE ROW LEVEL SECURITY;
-- Anyone who is creator OR participant can view the split
CREATE POLICY "split_bills_select" ON split_bills FOR SELECT
USING (
auth.uid() = creator_id OR
id IN (SELECT split_id FROM split_participants WHERE user_id = auth.uid())
);
CREATE POLICY "split_bills_insert" ON split_bills FOR INSERT
WITH CHECK (auth.uid() = creator_id);
CREATE POLICY "split_bills_update" ON split_bills FOR UPDATE
USING (auth.uid() = creator_id);
CREATE POLICY "split_participants_select" ON split_participants FOR SELECT
USING (
user_id = auth.uid() OR
split_id IN (SELECT id FROM split_bills WHERE creator_id = auth.uid())
);
CREATE POLICY "split_participants_update" ON split_participants FOR UPDATE
USING (user_id = auth.uid());