-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_fix_complete.sql
More file actions
60 lines (49 loc) · 2.31 KB
/
Copy pathdatabase_fix_complete.sql
File metadata and controls
60 lines (49 loc) · 2.31 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
-- Complete database fix - add all missing columns
-- Run this in Supabase SQL Editor
-- ==========================================
-- FIX 1: Add missing columns to expenses table
-- ==========================================
ALTER TABLE expenses
ADD COLUMN IF NOT EXISTS owner_id UUID,
ADD COLUMN IF NOT EXISTS created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW();
-- ==========================================
-- FIX 2: Add employees table (completely missing)
-- ==========================================
CREATE TABLE IF NOT EXISTS employees (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
email TEXT,
phone TEXT,
role TEXT DEFAULT 'staff',
status TEXT DEFAULT 'active',
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
owner_id UUID NOT NULL
);
ALTER TABLE employees ENABLE ROW LEVEL SECURITY;
CREATE POLICY IF NOT EXISTS "employees_select" ON employees FOR SELECT USING (auth.uid() = owner_id);
CREATE POLICY IF NOT EXISTS "employees_insert" ON employees FOR INSERT WITH CHECK (auth.uid() = owner_id);
CREATE POLICY IF NOT EXISTS "employees_update" ON employees FOR UPDATE USING (auth.uid() = owner_id);
CREATE POLICY IF NOT EXISTS "employees_delete" ON employees FOR DELETE USING (auth.uid() = owner_id);
-- ==========================================
-- FIX 3: Add missing indexes
-- ==========================================
CREATE INDEX IF NOT EXISTS idx_employees_owner ON employees(owner_id);
CREATE INDEX IF NOT EXISTS idx_expenses_owner ON expenses(owner_id);
-- ==========================================
-- FIX 4: Ensure all tables have required columns
-- ==========================================
-- Check and add to clients if missing
ALTER TABLE clients
ADD COLUMN IF NOT EXISTS created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW();
-- Check and add to orders if missing
ALTER TABLE orders
ADD COLUMN IF NOT EXISTS created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW();
-- Check and add to settings if missing
ALTER TABLE settings
ADD COLUMN IF NOT EXISTS created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW();
-- Check and add to memories if missing
ALTER TABLE memories
ADD COLUMN IF NOT EXISTS created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW();
-- ==========================================
-- DONE! All tables should now be complete.
-- ==========================================