-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsupabase_migration_transactions.sql
More file actions
74 lines (64 loc) · 2.42 KB
/
supabase_migration_transactions.sql
File metadata and controls
74 lines (64 loc) · 2.42 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
-- Migration: Add transactions table for payment history tracking
-- This table stores transaction records for all payments made by users
-- Create transactions table
CREATE TABLE IF NOT EXISTS transactions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
spot_id UUID NOT NULL REFERENCES spots(id) ON DELETE CASCADE,
amount NUMERIC NOT NULL,
payment_method TEXT NOT NULL DEFAULT 'UPI',
status TEXT NOT NULL DEFAULT 'not_paid' CHECK (status IN ('paid', 'not_paid')),
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Create indexes for faster queries
CREATE INDEX IF NOT EXISTS transactions_user_id_idx ON transactions(user_id);
CREATE INDEX IF NOT EXISTS transactions_spot_id_idx ON transactions(spot_id);
CREATE INDEX IF NOT EXISTS transactions_created_at_idx ON transactions(created_at DESC);
-- Enable Row Level Security (RLS)
ALTER TABLE transactions ENABLE ROW LEVEL SECURITY;
-- Policy: Users can read their own transactions
CREATE POLICY "Users can read own transactions" ON transactions
FOR SELECT USING (auth.uid() = user_id);
-- Policy: Admins can read all transactions
CREATE POLICY "Admins can read all transactions" ON transactions
FOR SELECT USING (
EXISTS (
SELECT 1 FROM profiles
WHERE profiles.id = auth.uid()
AND profiles.role = 'admin'
)
);
-- Policy: Admins can create transactions
CREATE POLICY "Admins can create transactions" ON transactions
FOR INSERT WITH CHECK (
EXISTS (
SELECT 1 FROM profiles
WHERE profiles.id = auth.uid()
AND profiles.role = 'admin'
)
);
-- Policy: Admins can update transactions
CREATE POLICY "Admins can update transactions" ON transactions
FOR UPDATE USING (
EXISTS (
SELECT 1 FROM profiles
WHERE profiles.id = auth.uid()
AND profiles.role = 'admin'
)
);
-- Add trigger to update updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER update_transactions_updated_at
BEFORE UPDATE ON transactions
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- Enable real-time for transactions table
-- Note: You need to enable this in Supabase Dashboard > Database > Replication
-- or run: ALTER PUBLICATION supabase_realtime ADD TABLE transactions;