-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase_setup.sql
More file actions
125 lines (99 loc) · 3.92 KB
/
supabase_setup.sql
File metadata and controls
125 lines (99 loc) · 3.92 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
-- Create a table for Job Configurations
create table public.job_configs (
id uuid not null default gen_random_uuid(),
user_id uuid references auth.users not null,
config_id text not null, -- The string ID used in the app (e.g., 'RA', 'STF')
name text not null,
color text not null,
default_hours_weekday numeric not null,
default_hours_weekend numeric not null,
hourly_rate_weekday numeric not null,
hourly_rate_saturday numeric not null,
hourly_rate_sunday numeric not null,
hourly_rate_holiday numeric not null,
created_at timestamp with time zone default timezone('utc'::text, now()) not null,
primary key (id),
unique(user_id, config_id)
);
-- Create a table for Shifts
create table public.shifts (
id uuid not null default gen_random_uuid(),
user_id uuid references auth.users not null,
date text not null, -- Storing as 'YYYY-MM-DD' text to match app logic
type text not null, -- Corresponds to job_configs.config_id
hours numeric not null,
created_at timestamp with time zone default timezone('utc'::text, now()) not null,
primary key (id),
unique(user_id, date, type) -- Prevent duplicate shifts for same job on same day
);
-- Enable Row Level Security (RLS)
alter table public.job_configs enable row level security;
alter table public.shifts enable row level security;
-- Create policies for Job Configs
create policy "Users can view their own job configs"
on public.job_configs for select
using ( auth.uid() = user_id );
create policy "Users can insert their own job configs"
on public.job_configs for insert
with check ( auth.uid() = user_id );
create policy "Users can update their own job configs"
on public.job_configs for update
using ( auth.uid() = user_id );
create policy "Users can delete their own job configs"
on public.job_configs for delete
using ( auth.uid() = user_id );
-- Create policies for Shifts
create policy "Users can view their own shifts"
on public.shifts for select
using ( auth.uid() = user_id );
create policy "Users can insert their own shifts"
on public.shifts for insert
with check ( auth.uid() = user_id );
create policy "Users can update their own shifts"
on public.shifts for update
using ( auth.uid() = user_id );
create policy "Users can delete their own shifts"
on public.shifts for delete
using ( auth.uid() = user_id );
-- Create profiles table
create table public.profiles (
id uuid not null references auth.users on delete cascade,
is_student_visa_holder boolean default false,
vacation_periods jsonb default '[]'::jsonb,
savings_goal numeric default 0,
holidays jsonb default '[]'::jsonb,
expenses jsonb default '[]'::jsonb,
is_admin boolean default false,
primary key (id)
);
-- Enable RLS for profiles
alter table public.profiles enable row level security;
-- Create policies for profiles
create policy "Users can view their own profile"
on public.profiles for select
using ( auth.uid() = id );
create policy "Users can update their own profile"
on public.profiles for update
using ( auth.uid() = id );
create policy "Users can insert their own profile"
on public.profiles for insert
with check ( auth.uid() = id );
-- =============================================
-- MIGRATION: Add new columns to existing tables
-- Run this if you already have the tables created
-- =============================================
-- Add rate_history column to job_configs
ALTER TABLE public.job_configs
ADD COLUMN IF NOT EXISTS rate_history jsonb DEFAULT '[]'::jsonb;
-- Add note column to shifts
ALTER TABLE public.shifts
ADD COLUMN IF NOT EXISTS note text;
-- Add new columns to profiles
ALTER TABLE public.profiles
ADD COLUMN IF NOT EXISTS savings_goal numeric DEFAULT 0;
ALTER TABLE public.profiles
ADD COLUMN IF NOT EXISTS holidays jsonb DEFAULT '[]'::jsonb;
ALTER TABLE public.profiles
ADD COLUMN IF NOT EXISTS expenses jsonb DEFAULT '[]'::jsonb;
ALTER TABLE public.profiles
ADD COLUMN IF NOT EXISTS is_admin boolean DEFAULT false;