
Code testing
Published Apr 29, 2026Revision 14
Julian KorsCodeTYPESCRIPT
```bash
npm run dev
```CodeSQL
create table if not exists public.time_entries (
id uuid primary key default gen_random_uuid(),
organization_id uuid not null references public.organizations(id) on delete cascade,
workspace_id uuid not null references public.workspaces(id) on delete cascade,
user_id uuid not null references auth.users(id) on delete cascade,
project_id uuid references public.projects(id) on delete set null,
task_id uuid references public.tasks(id) on delete set null,
status text not null default 'running' check (status in ('running', 'paused', 'stopped')),
started_at timestamptz not null default now(),
last_resumed_at timestamptz,
ended_at timestamptz,
accumulated_seconds integer not null default 0,
total_seconds integer,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now(),
constraint time_entries_target_required check (project_id is not null or task_id is not null)
);
create index if not exists idx_time_entries_workspace_user_status
on public.time_entries (workspace_id, user_id, status, started_at desc);
create index if not exists idx_time_entries_workspace_user_ended
on public.time_entries (workspace_id, user_id, ended_at desc);
create trigger trg_time_entries_set_updated_at
before update on public.time_entries
for each row execute function public.set_updated_at();
create trigger trg_time_entries_sync_org
before insert or update on public.time_entries
for each row execute function public.sync_tenant_organization_id();
alter table public.time_entries enable row level security;
create policy time_entries_owner_select on public.time_entries
for select
using (auth.uid() = user_id and public.is_workspace_member(workspace_id));
create policy time_entries_owner_manage on public.time_entries
for all
using (auth.uid() = user_id and public.is_workspace_member(workspace_id))
with check (auth.uid() = user_id and public.is_workspace_member(workspace_id));