-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase_schema.sql
More file actions
131 lines (107 loc) · 6.11 KB
/
Copy pathsupabase_schema.sql
File metadata and controls
131 lines (107 loc) · 6.11 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
-- ============================================================
-- OJT Attendance System — Full Supabase Schema
-- ISUFST Dingle Campus | CICT Department
-- ============================================================
-- Run this entire file in your Supabase SQL Editor.
-- It is safe to re-run (uses IF NOT EXISTS / OR REPLACE).
-- ============================================================
-- ────────────────────────────────────────────────────────────
-- 1. INTERNS TABLE
-- ────────────────────────────────────────────────────────────
create table if not exists public.interns (
id bigint generated by default as identity primary key,
uuid uuid default gen_random_uuid() not null unique,
full_name text not null,
username text not null unique,
password text not null,
required_hours numeric default 600 not null,
created_at timestamp with time zone default timezone('utc', now()) not null,
updated_at timestamp with time zone default timezone('utc', now()) not null
);
-- Index for fast UUID lookups (used by QR scanner)
create index if not exists interns_uuid_idx on public.interns (uuid);
-- Index for username lookups (used by login)
create index if not exists interns_username_idx on public.interns (username);
-- Row Level Security
alter table public.interns enable row level security;
drop policy if exists "Allow all operations on interns" on public.interns;
create policy "Allow all operations on interns"
on public.interns for all
to public
using (true)
with check (true);
-- ────────────────────────────────────────────────────────────
-- 2. ATTENDANCE TABLE
-- ────────────────────────────────────────────────────────────
create table if not exists public.attendance (
id bigint generated by default as identity primary key,
intern_id bigint not null references public.interns(id) on delete cascade,
time_in timestamp with time zone not null,
time_out timestamp with time zone,
created_at timestamp with time zone default timezone('utc', now()) not null
);
-- Index for common query patterns
create index if not exists attendance_intern_id_idx on public.attendance (intern_id);
create index if not exists attendance_time_in_idx on public.attendance (time_in);
-- Row Level Security
alter table public.attendance enable row level security;
drop policy if exists "Allow all operations on attendance" on public.attendance;
create policy "Allow all operations on attendance"
on public.attendance for all
to public
using (true)
with check (true);
-- ────────────────────────────────────────────────────────────
-- 3. LEAVE REQUESTS TABLE
-- ────────────────────────────────────────────────────────────
create table if not exists public.leave_requests (
id uuid default gen_random_uuid() primary key,
intern_id bigint not null references public.interns(id) on delete cascade,
-- Request details
date_of_leave date not null,
reason text not null,
status text not null default 'pending'
check (status in ('pending', 'approved', 'rejected')),
-- Admin response
admin_notes text,
-- Audit
created_at timestamp with time zone default timezone('utc', now()) not null,
updated_at timestamp with time zone default timezone('utc', now()) not null
);
-- Indexes
create index if not exists leave_requests_intern_id_idx on public.leave_requests (intern_id);
create index if not exists leave_requests_status_idx on public.leave_requests (status);
-- Row Level Security
alter table public.leave_requests enable row level security;
drop policy if exists "Allow all operations on leave_requests" on public.leave_requests;
create policy "Allow all operations on leave_requests"
on public.leave_requests for all
to public
using (true)
with check (true);
-- ────────────────────────────────────────────────────────────
-- 4. OPTIONAL: Auto-update updated_at trigger
-- (Makes interns.updated_at and leave_requests.updated_at
-- update automatically on every row change)
-- ────────────────────────────────────────────────────────────
create or replace function public.set_updated_at()
returns trigger language plpgsql as $$
begin
new.updated_at := timezone('utc', now());
return new;
end;
$$;
-- Attach trigger to interns
drop trigger if exists trg_interns_updated_at on public.interns;
create trigger trg_interns_updated_at
before update on public.interns
for each row execute function public.set_updated_at();
-- Attach trigger to leave_requests
drop trigger if exists trg_leave_requests_updated_at on public.leave_requests;
create trigger trg_leave_requests_updated_at
before update on public.leave_requests
for each row execute function public.set_updated_at();
-- ────────────────────────────────────────────────────────────
-- DONE ✓
-- Tables: interns, attendance, leave_requests
-- ────────────────────────────────────────────────────────────