ChandimaPrabath commited on
Commit
5fb463b
·
1 Parent(s): aece130
init_supabase_sql.txt CHANGED
@@ -3,7 +3,7 @@ create extension if not exists "uuid-ossp";
3
 
4
  -- Users table
5
  create table if not exists public.users (
6
- id uuid primary key,
7
  username text unique not null,
8
  password text not null,
9
  email text,
@@ -11,17 +11,18 @@ create table if not exists public.users (
11
  access_level text not null
12
  );
13
 
14
- -- Sessions table
15
  create table if not exists public.sessions (
16
- id uuid primary key default uuid_generate_v4(),
17
- user_id uuid references public.users(id),
18
  token text not null,
19
  expires timestamp with time zone not null,
20
- device text not null
 
21
  );
22
 
23
  -- Create indexes for better performance
24
  create index if not exists idx_users_username on public.users(username);
 
25
  create index if not exists idx_sessions_user_id on public.sessions(user_id);
26
  create index if not exists idx_sessions_token on public.sessions(token);
27
 
 
3
 
4
  -- Users table
5
  create table if not exists public.users (
6
+ user_id uuid primary key,
7
  username text unique not null,
8
  password text not null,
9
  email text,
 
11
  access_level text not null
12
  );
13
 
14
+ -- Sessions table with composite primary key (user_id, token)
15
  create table if not exists public.sessions (
16
+ user_id uuid references public.users(user_id),
 
17
  token text not null,
18
  expires timestamp with time zone not null,
19
+ device text not null,
20
+ primary key (user_id, token) -- Composite primary key
21
  );
22
 
23
  -- Create indexes for better performance
24
  create index if not exists idx_users_username on public.users(username);
25
+ create index if not exists idx_users_user_id on public.users(user_id);
26
  create index if not exists idx_sessions_user_id on public.sessions(user_id);
27
  create index if not exists idx_sessions_token on public.sessions(token);
28
 
init_supabase_sql2.txt DELETED
@@ -1,47 +0,0 @@
1
- -- Enable UUID extension
2
- create extension if not exists "uuid-ossp";
3
-
4
- -- Users table
5
- create table if not exists public.users (
6
- id uuid primary key,
7
- username text unique not null,
8
- password text not null,
9
- email text,
10
- date_joined timestamp with time zone not null,
11
- access_level text not null
12
- );
13
-
14
- -- Sessions table with composite primary key (user_id, token)
15
- create table if not exists public.sessions (
16
- user_id uuid references public.users(id),
17
- token text not null,
18
- expires timestamp with time zone not null,
19
- device text not null,
20
- primary key (user_id, token) -- Composite primary key
21
- );
22
-
23
- -- Create indexes for better performance
24
- create index if not exists idx_users_username on public.users(username);
25
- create index if not exists idx_sessions_user_id on public.sessions(user_id);
26
- create index if not exists idx_sessions_token on public.sessions(token);
27
-
28
- -- Set up Row Level Security (RLS)
29
- alter table public.users enable row level security;
30
- alter table public.sessions enable row level security;
31
-
32
- -- Create policies
33
- create policy "Enable read access for all users"
34
- on public.users for select
35
- using (true);
36
-
37
- create policy "Enable insert for authenticated users only"
38
- on public.users for insert
39
- with check (true);
40
-
41
- create policy "Enable update for authenticated users"
42
- on public.users for update
43
- using (true);
44
-
45
- create policy "Enable all access for sessions"
46
- on public.sessions for all
47
- using (true);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
token_cleanup cron_job_supabase_sql.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ CREATE OR REPLACE FUNCTION delete_expired_sessions()
2
+ RETURNS void AS $$
3
+ BEGIN
4
+ DELETE FROM sessions
5
+ WHERE expires < NOW();
6
+ END;
7
+ $$ LANGUAGE plpgsql;
8
+
9
+ CREATE EXTENSION IF NOT EXISTS pg_cron;
10
+
11
+ SELECT cron.schedule('0 0 * * *', 'SELECT delete_expired_sessions();');