-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase_schema_simple.sql
More file actions
155 lines (137 loc) · 5.78 KB
/
supabase_schema_simple.sql
File metadata and controls
155 lines (137 loc) · 5.78 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
-- Supabase Database Schema for Langiu Valymas CRM
-- Run this ENTIRE file at once in Supabase SQL Editor
-- ==========================================
-- STEP 1: Drop existing tables (BE CAREFUL IN PRODUCTION)
-- ==========================================
DROP TABLE IF EXISTS memories;
DROP TABLE IF EXISTS settings;
DROP TABLE IF EXISTS expenses;
DROP TABLE IF EXISTS orders;
DROP TABLE IF EXISTS clients;
DROP TABLE IF EXISTS profiles;
-- ==========================================
-- STEP 2: Create all tables
-- ==========================================
-- PROFILES TABLE
CREATE TABLE profiles (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
uid UUID NOT NULL,
email TEXT NOT NULL,
name TEXT,
phone TEXT,
role TEXT DEFAULT 'staff',
client_id UUID,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- CLIENTS TABLE
CREATE TABLE clients (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
phone TEXT,
address TEXT,
building_type TEXT DEFAULT 'butas',
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
owner_id UUID NOT NULL
);
-- ORDERS TABLE
CREATE TABLE orders (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
client_id UUID,
date TIMESTAMP WITH TIME ZONE,
windows INTEGER DEFAULT 0,
floors INTEGER DEFAULT 0,
balkonai INTEGER DEFAULT 0,
vitrinos INTEGER DEFAULT 0,
terasa INTEGER DEFAULT 0,
kiti TEXT,
status TEXT DEFAULT 'pending',
price DECIMAL(10,2),
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
owner_id UUID NOT NULL
);
-- EXPENSES TABLE
CREATE TABLE expenses (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
amount DECIMAL(10,2) NOT NULL,
description TEXT,
category TEXT,
date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
owner_id UUID NOT NULL
);
-- SETTINGS TABLE
CREATE TABLE settings (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
owner_id UUID NOT NULL,
price_per_window DECIMAL(10,2) DEFAULT 5.00,
price_per_floor DECIMAL(10,2) DEFAULT 2.00,
price_balkonai DECIMAL(10,2) DEFAULT 8.00,
price_vitrinos DECIMAL(10,2) DEFAULT 12.00,
price_terasa DECIMAL(10,2) DEFAULT 15.00,
price_kiti DECIMAL(10,2) DEFAULT 10.00,
sms_template TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- MEMORIES TABLE
CREATE TABLE memories (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
content TEXT NOT NULL,
type TEXT DEFAULT 'fact',
priority INTEGER DEFAULT 5,
owner_id UUID NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- ==========================================
-- STEP 3: Enable Row Level Security
-- ==========================================
ALTER TABLE profiles ENABLE ROW LEVEL SECURITY;
ALTER TABLE clients ENABLE ROW LEVEL SECURITY;
ALTER TABLE orders ENABLE ROW LEVEL SECURITY;
ALTER TABLE expenses ENABLE ROW LEVEL SECURITY;
ALTER TABLE settings ENABLE ROW LEVEL SECURITY;
ALTER TABLE memories ENABLE ROW LEVEL SECURITY;
-- ==========================================
-- STEP 4: Create RLS Policies
-- ==========================================
-- Profiles policies
CREATE POLICY "profiles_select" ON profiles FOR SELECT USING (auth.uid() = uid);
CREATE POLICY "profiles_insert" ON profiles FOR INSERT WITH CHECK (auth.uid() = uid);
CREATE POLICY "profiles_update" ON profiles FOR UPDATE USING (auth.uid() = uid);
CREATE POLICY "profiles_delete" ON profiles FOR DELETE USING (auth.uid() = uid);
-- Clients policies
CREATE POLICY "clients_select" ON clients FOR SELECT USING (auth.uid() = owner_id);
CREATE POLICY "clients_insert" ON clients FOR INSERT WITH CHECK (auth.uid() = owner_id);
CREATE POLICY "clients_update" ON clients FOR UPDATE USING (auth.uid() = owner_id);
CREATE POLICY "clients_delete" ON clients FOR DELETE USING (auth.uid() = owner_id);
-- Orders policies
CREATE POLICY "orders_select" ON orders FOR SELECT USING (auth.uid() = owner_id);
CREATE POLICY "orders_insert" ON orders FOR INSERT WITH CHECK (auth.uid() = owner_id);
CREATE POLICY "orders_update" ON orders FOR UPDATE USING (auth.uid() = owner_id);
CREATE POLICY "orders_delete" ON orders FOR DELETE USING (auth.uid() = owner_id);
-- Expenses policies
CREATE POLICY "expenses_select" ON expenses FOR SELECT USING (auth.uid() = owner_id);
CREATE POLICY "expenses_insert" ON expenses FOR INSERT WITH CHECK (auth.uid() = owner_id);
CREATE POLICY "expenses_update" ON expenses FOR UPDATE USING (auth.uid() = owner_id);
CREATE POLICY "expenses_delete" ON expenses FOR DELETE USING (auth.uid() = owner_id);
-- Settings policies
CREATE POLICY "settings_select" ON settings FOR SELECT USING (auth.uid() = owner_id);
CREATE POLICY "settings_insert" ON settings FOR INSERT WITH CHECK (auth.uid() = owner_id);
CREATE POLICY "settings_update" ON settings FOR UPDATE USING (auth.uid() = owner_id);
CREATE POLICY "settings_delete" ON settings FOR DELETE USING (auth.uid() = owner_id);
-- Memories policies
CREATE POLICY "memories_select" ON memories FOR SELECT USING (auth.uid() = owner_id);
CREATE POLICY "memories_insert" ON memories FOR INSERT WITH CHECK (auth.uid() = owner_id);
CREATE POLICY "memories_update" ON memories FOR UPDATE USING (auth.uid() = owner_id);
CREATE POLICY "memories_delete" ON memories FOR DELETE USING (auth.uid() = owner_id);
-- ==========================================
-- STEP 5: Create indexes
-- ==========================================
CREATE INDEX idx_clients_owner ON clients(owner_id);
CREATE INDEX idx_orders_owner ON orders(owner_id);
CREATE INDEX idx_orders_client ON orders(client_id);
CREATE INDEX idx_expenses_owner ON expenses(owner_id);
CREATE INDEX idx_settings_owner ON settings(owner_id);
CREATE INDEX idx_memories_owner ON memories(owner_id);
CREATE INDEX idx_profiles_uid ON profiles(uid);
-- ==========================================
-- DONE! Tables are ready to use.
-- ==========================================