Changed categories to be linked to calendars and worlds

This commit is contained in:
Alexis
2025-03-14 22:25:50 +01:00
parent e6b82e9b85
commit f3fd97e3cd
2 changed files with 111 additions and 98 deletions

View File

@@ -14,40 +14,40 @@ create type public.calendar_state as enum ('published', 'draft', 'archived');
-- DATA STRUCTURES
-- Users
create table public.users (
id uuid references auth.users not null primary key, -- UUID from auth.users
username text
id uuid references auth.users not null primary key, -- UUID from auth.users
username text
);
comment on table public.users is 'Profile data for each user.';
comment on column public.users.id is 'References the internal Supabase Auth user.';
-- Roles
create table public.user_roles (
id bigint generated by default as identity primary key,
role app_role not null,
user_id uuid references public.users on delete cascade not null,
id bigint generated by default as identity primary key,
role app_role not null,
user_id uuid references public.users on delete cascade not null,
unique (user_id, role)
);
comment on table public.user_roles is 'Application roles for each user.';
-- Permissions
create table public.role_permissions (
id bigint generated by default as identity primary key,
role app_role not null,
permission app_permission not null,
id bigint generated by default as identity primary key,
role app_role not null,
permission app_permission not null,
unique (role, permission)
);
comment on table public.role_permissions is 'Application permissions for each role.';
-- Worlds
create table public.worlds (
id bigint generated by default as identity primary key,
name text not null,
description text,
color app_colors default 'black',
state world_state default 'draft',
created_at timestamptz default now(),
updated_at timestamptz,
gm_id uuid references public.users on delete cascade
id bigint generated by default as identity primary key,
name text not null,
description text,
color app_colors default 'black',
state world_state default 'draft',
created_at timestamptz default now(),
updated_at timestamptz,
gm_id uuid references public.users on delete cascade
);
comment on table public.worlds is 'Worlds belonging to a single user ; a game master.';
create trigger handle_updated_at before update on public.worlds
@@ -55,9 +55,9 @@ create trigger handle_updated_at before update on public.worlds
-- World Players (join table)
create table public.world_players (
world_id bigint references public.worlds on delete cascade,
player_id uuid references public.users on delete cascade,
joined_at timestamptz default now(),
world_id bigint references public.worlds on delete cascade,
player_id uuid references public.users on delete cascade,
joined_at timestamptz default now(),
primary key (world_id, player_id)
);
comment on table public.world_players is 'Players (users) that belong to specific worlds';
@@ -91,27 +91,27 @@ comment on table public.calendar_months is 'A calendar month.';
-- Calendar Event categories
create table public.calendar_event_categories (
id bigint generated by default as identity primary key,
name text not null,
color app_colors default 'black',
unique (name)
id bigint generated by default as identity primary key,
name text not null,
color app_colors default 'black',
calendar_id bigint references public.calendars on delete cascade not null
);
comment on table public.calendar_event_categories is 'Categories describing events.';
-- Calendar Events
create table public.calendar_events (
id bigint generated by default as identity primary key,
title text not null,
description text,
location text,
start_date json not null,
end_date json,
category bigint references public.calendar_event_categories on delete cascade,
hidden boolean default false,
wiki text,
created_at timestamptz default now(),
updated_at timestamptz,
calendar_id bigint references public.calendars on delete cascade not null
id bigint generated by default as identity primary key,
title text not null,
description text,
location text,
start_date json not null,
end_date json,
category bigint references public.calendar_event_categories on delete cascade,
hidden boolean default false,
wiki text,
created_at timestamptz default now(),
updated_at timestamptz,
calendar_id bigint references public.calendars on delete cascade not null
);
comment on table public.calendar_events is 'Events linked to a world';
@@ -134,12 +134,12 @@ comment on table public.calendar_event_categories_links is 'Link tables for mult
-- Character categories
create table public.character_categories (
id bigint generated by default as identity primary key,
name text not null,
color app_colors default 'black',
created_at timestamptz default now(),
updated_at timestamptz,
unique (name)
id bigint generated by default as identity primary key,
name text not null,
color app_colors default 'black',
created_at timestamptz default now(),
updated_at timestamptz,
world_id bigint references public.worlds on delete cascade not null
);
comment on table public.character_categories is 'Categories describing characters';
@@ -420,12 +420,25 @@ create policy "Allow individual update access for GMs" on public.characters for
)
);
-- Categories are public to view but not to insert
-- Needs to be refactored maybe, if in the future we want a default set AND user defined ones
create policy "Allow all read access" on public.calendar_event_categories for select to authenticated, anon using ( true );
create policy "Allow all read access" on public.calendar_event_categories_links for select to authenticated, anon using ( true );
create policy "Allow logged-in read access" on public.character_categories for select using ( auth.role() = 'authenticated' );
create policy "Allow logged-in read access" on public.character_categories_links for select using ( auth.role() = 'authenticated' );
-- Categories policies
create policy "Allow anonymous access to published event categories" on public.calendar_event_categories
for select
to authenticated, anon
using (
exists (
select 1
from public.calendars c
join public.calendar_event_categories_links l on l.calendar_event_category_id = calendar_event_categories.id
where c.id = calendar_event_categories.calendar_id
and c.state = 'published'
)
or exists (
select 1
from public.calendars c
where c.id = calendar_event_categories.calendar_id
and c.state = 'published'
)
);
-- Send "previous data" on change
alter table public.users replica identity full;