Calendar
calendarCalendars, events with RFC 5545 recurrence (RRULE, EXDATE and per-occurrence overrides) and attendees, working hours, out-of-office and focus-time blocks, and a free/busy query that counts approved leave. Series expand in TypeScript with the reminders block's expandRule and in SQL for free/busy; a share link serves an iCal feed.
SchedulingReact hooksmanagedcustom
OctoberWeek 42
12345678910111213141516171819202122232425262728
Install
The command also installs updated-at, memberships, permissions, recurrence, the modules it needs.
npx better-supabase add calendarConfigure
The blocks entries in your config, with the default mode.
import { defineConfig } from "better-supabase/config";
export default defineConfig({
blocks: {
// Required by calendar; add installs them too.
"updated-at": {},
memberships: {},
permissions: {},
recurrence: {},
// Modes: "managed", "custom". The default is "managed".
calendar: { mode: "managed" },
},
});Migrations
The files
npx better-supabase add calendar writes into your declarative schema with the default layout.11 files
supabase/schemas/900_better_supabase_072_calendar.sqlschema2315 lines-- better-supabase module: calendar
-- @bsb-block calendar@1 managed
-- @bsb-hash sha256:c40954afee40e281d1c78d4f063dd5c9bfddc7e1201d47e445d946cc68c97e4a
-- Calendars, events with RFC 5545 recurrence (RRULE, EXDATE and per-occurrence overrides) and attendees, working hours, out-of-office and focus-time blocks, and a free/busy query that counts approved leave. Series expand in TypeScript with the reminders block's expandRule and in SQL for free/busy; a share link serves an iCal feed.
-- Managed by `better-supabase add`; re-running it rewrites this file unless it was edited by hand.
-- Change it through `blocks` in better-supabase.config.ts and the module's SQL hooks.
create table "better_supabase"."calendars" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"name" text not null constraint bsb_calendars_name_check check (length("name") >= 1 and length("name") <= 120),
"tz" text not null default 'UTC',
"color" text,
"owner_type" text,
"owner_id" text,
"created_at" timestamptz not null default now()
);
alter table "better_supabase"."calendars" enable row level security;
revoke all on "better_supabase"."calendars" from public, anon, authenticated;
grant select on "better_supabase"."calendars" to authenticated;
grant all on "better_supabase"."calendars" to service_role;
create index "calendars_tenant_idx" on "better_supabase"."calendars" ("tenant_id");
create policy bsb_calendars_read on "better_supabase"."calendars"
for select to authenticated
using ("tenant_id" in (select better_supabase.tenant_ids_with('calendar.read')));
create table "better_supabase"."calendar_events" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"calendar_id" uuid not null references "better_supabase"."calendars" ("id") on delete cascade,
"uid" text not null,
"title" text not null default '',
"description" text,
"location" text,
"starts_at" timestamptz not null,
"ends_at" timestamptz not null,
"all_day" boolean not null default false,
"tz" text not null default 'UTC',
"status" text not null default 'confirmed' constraint bsb_calendar_events_status_check check ("status" in ('confirmed', 'tentative', 'cancelled')),
"rrule" text,
"exdates" timestamptz[] not null default '{}',
"recurrence_id" uuid references "better_supabase"."calendar_events" ("id") on delete cascade,
"original_starts_at" timestamptz,
"subject_type" text,
"subject_id" text,
"color" text,
"kind" text not null default 'event',
"transparency" text not null default 'opaque',
"auto_decline" boolean not null default false,
"created_by" uuid,
"created_at" timestamptz not null default now(),
"updated_at" timestamptz not null default now(),
constraint bsb_calendar_events_check check ("ends_at" >= "starts_at"),
constraint bsb_calendar_events_check1 check (("recurrence_id" is null) = ("original_starts_at" is null)),
unique nulls not distinct ("calendar_id", "uid", "original_starts_at")
);
create index calendar_events_recurrence_idx on "better_supabase"."calendar_events" ("recurrence_id") where "recurrence_id" is not null;
create index calendar_events_range_idx on "better_supabase"."calendar_events" ("calendar_id", "starts_at");
alter table "better_supabase"."calendar_events" add constraint bsb_calendar_events_kind_check
check ("kind" in ('event', 'out_of_office', 'focus_time'));
alter table "better_supabase"."calendar_events" add constraint bsb_calendar_events_transparency_check
check ("transparency" in ('opaque', 'transparent'));
alter table "better_supabase"."calendar_events" enable row level security;
revoke all on "better_supabase"."calendar_events" from public, anon, authenticated;
grant select on "better_supabase"."calendar_events" to authenticated;
grant all on "better_supabase"."calendar_events" to service_role;
create policy bsb_calendar_events_read on "better_supabase"."calendar_events"
for select to authenticated
using ("tenant_id" in (select better_supabase.tenant_ids_with('calendar.read')));
create table "better_supabase"."calendar_attendees" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"event_id" uuid not null references "better_supabase"."calendar_events" ("id") on delete cascade,
"email" text not null,
"name" text,
"user_id" uuid,
"role" text not null default 'required',
"response" text not null default 'needs-action' constraint bsb_calendar_attendees_response_check check ("response" in ('needs-action', 'accepted', 'declined', 'tentative')),
unique ("event_id", "email")
);
alter table "better_supabase"."calendar_attendees" enable row level security;
revoke all on "better_supabase"."calendar_attendees" from public, anon, authenticated;
grant select on "better_supabase"."calendar_attendees" to authenticated;
grant all on "better_supabase"."calendar_attendees" to service_role;
alter table "better_supabase"."calendar_attendees" add constraint bsb_calendar_attendees_role_check
check ("role" in ('chair', 'required', 'optional', 'non-participant'));
create policy bsb_calendar_attendees_read on "better_supabase"."calendar_attendees"
for select to authenticated
using ("tenant_id" in (select better_supabase.tenant_ids_with('calendar.read')) or "user_id" = (select auth.uid()));
create table "better_supabase"."calendar_event_alarms" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"event_id" uuid not null references "better_supabase"."calendar_events" ("id") on delete cascade,
"user_id" uuid,
"method" text not null default 'display',
"offset_minutes" integer,
"related_to" text not null default 'start',
"trigger_at" timestamptz,
"repeat_count" integer not null default 0,
"repeat_minutes" integer,
"description" text,
"summary" text,
"created_at" timestamptz not null default now()
);
create index "calendar_event_alarms_event_idx" on "better_supabase"."calendar_event_alarms" ("event_id");
alter table "better_supabase"."calendar_event_alarms" enable row level security;
revoke all on "better_supabase"."calendar_event_alarms" from public, anon, authenticated;
grant select on "better_supabase"."calendar_event_alarms" to authenticated;
grant all on "better_supabase"."calendar_event_alarms" to service_role;
alter table "better_supabase"."calendar_event_alarms" add constraint bsb_calendar_event_alarms_method_check
check ("method" in ('display', 'email', 'audio'));
alter table "better_supabase"."calendar_event_alarms" add constraint bsb_calendar_event_alarms_offset_check
check ("offset_minutes" >= -525600 and "offset_minutes" <= 525600);
alter table "better_supabase"."calendar_event_alarms" add constraint bsb_calendar_event_alarms_trigger_check
check (("offset_minutes" is null) <> ("trigger_at" is null));
alter table "better_supabase"."calendar_event_alarms" add constraint bsb_calendar_event_alarms_related_check
check ("related_to" in ('start', 'end'));
alter table "better_supabase"."calendar_event_alarms" add constraint bsb_calendar_event_alarms_repeat_check
check ("repeat_count" >= 0 and ("repeat_count" = 0) = ("repeat_minutes" is null) and coalesce("repeat_minutes", 1) > 0);
create policy bsb_calendar_event_alarms_read on "better_supabase"."calendar_event_alarms"
for select to authenticated
using ("tenant_id" in (select better_supabase.tenant_ids_with('calendar.read')) and ("user_id" is null or "user_id" = (select auth.uid())));
create table "better_supabase"."calendar_working_hours" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"user_id" uuid not null,
"weekday" smallint not null constraint bsb_calendar_working_hours_weekday_check check ("weekday" >= 1 and "weekday" <= 7),
"starts_at" time not null,
"ends_at" time not null,
"tz" text not null default 'UTC',
constraint bsb_calendar_working_hours_check check ("ends_at" > "starts_at"),
unique ("tenant_id", "user_id", "weekday", "starts_at")
);
alter table "better_supabase"."calendar_working_hours" enable row level security;
revoke all on "better_supabase"."calendar_working_hours" from public, anon, authenticated;
grant select on "better_supabase"."calendar_working_hours" to authenticated;
grant all on "better_supabase"."calendar_working_hours" to service_role;
create policy bsb_calendar_working_hours_read on "better_supabase"."calendar_working_hours"
for select to authenticated
using ("tenant_id" in (select better_supabase.tenant_ids_with('calendar.read')) or "user_id" = (select auth.uid()));
create or replace function "better_supabase"."create_calendar"(tenant uuid, name text, tz text, owner_type text, owner_id text)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_id uuid;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', create_calendar.tenant, 'calendar.manage'), false)) then
raise exception 'not allowed to manage calendars' using errcode = '42501', hint = 'CALENDAR_FORBIDDEN';
end if;
if not exists (select 1 from pg_catalog.pg_timezone_names where name = coalesce(create_calendar.tz, 'UTC')) then
raise exception 'tz must be an IANA time zone' using errcode = '22023', hint = 'CALENDAR_TZ_INVALID';
end if;
insert into "better_supabase"."calendars" ("tenant_id", "name", "tz", "owner_type", "owner_id")
values (create_calendar.tenant, create_calendar.name, coalesce(create_calendar.tz, 'UTC'), create_calendar.owner_type, create_calendar.owner_id)
returning "id" into v_id;
null;
return jsonb_build_object('id', v_id);
end;
$$;
revoke execute on function "better_supabase"."create_calendar"(uuid, text, text, text, text) from public, anon;
grant execute on function "better_supabase"."create_calendar"(uuid, text, text, text, text) to authenticated, service_role;
create or replace function "better_supabase"."delete_calendar"(calendar_id uuid)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_tenant uuid;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select "tenant_id" into v_tenant from "better_supabase"."calendars" where "id" = delete_calendar.calendar_id;
if not found then
raise exception 'calendar not found' using errcode = 'P0002', hint = 'CALENDAR_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_tenant, 'calendar.manage'), false)) then
raise exception 'not allowed to manage calendars' using errcode = '42501', hint = 'CALENDAR_FORBIDDEN';
end if;
delete from "better_supabase"."calendars" where "id" = delete_calendar.calendar_id;
return true;
end;
$$;
revoke execute on function "better_supabase"."delete_calendar"(uuid) from public, anon;
grant execute on function "better_supabase"."delete_calendar"(uuid) to authenticated, service_role;
-- Inserts an event, or updates the one named by item.id (keys left out keep
-- their value). An item with recurrenceId and originalStart overrides one
-- occurrence of that series and takes the series' uid. Only the service role
-- may pass an actor other than the caller; a new event's creator and owner are
-- the actor, else the caller.
create or replace function "better_supabase"."upsert_calendar_event"(calendar_id uuid, item jsonb, actor uuid default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_item jsonb := coalesce(upsert_calendar_event.item, '{}'::jsonb);
v_tenant uuid;
v_tz text;
v_id uuid := nullif(v_item ->> 'id', '')::uuid;
v_master uuid := nullif(v_item ->> 'recurrenceId', '')::uuid;
v_uid text := nullif(v_item ->> 'uid', '');
v_created boolean := false;
v_actor uuid := coalesce(upsert_calendar_event.actor, auth.uid());
v_exdates timestamptz[];
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select "tenant_id", "tz" into v_tenant, v_tz from "better_supabase"."calendars" where "id" = upsert_calendar_event.calendar_id;
if not found then
raise exception 'calendar not found' using errcode = 'P0002', hint = 'CALENDAR_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_tenant, 'calendar.write'), false)) then
raise exception 'not allowed to change this calendar' using errcode = '42501', hint = 'CALENDAR_FORBIDDEN';
end if;
if upsert_calendar_event.actor is not null and upsert_calendar_event.actor is distinct from auth.uid() and auth.role() <> 'service_role' then
raise exception 'only the service role saves an event as another user' using errcode = '42501', hint = 'CALENDAR_FORBIDDEN';
end if;
if v_item ? 'status' and v_item ->> 'status' not in ('confirmed', 'tentative', 'cancelled') then
raise exception 'status must be confirmed, tentative or cancelled' using errcode = '22023', hint = 'CALENDAR_EVENT_INVALID';
end if;
if v_item ? 'kind' and v_item ->> 'kind' not in ('event', 'out_of_office', 'focus_time') then
raise exception 'kind must be event, out_of_office or focus_time' using errcode = '22023', hint = 'CALENDAR_EVENT_INVALID';
end if;
if v_item ? 'transparency' and v_item ->> 'transparency' not in ('opaque', 'transparent') then
raise exception 'transparency must be opaque or transparent' using errcode = '22023', hint = 'CALENDAR_EVENT_INVALID';
end if;
if v_item ? 'tz' and not exists (select 1 from pg_catalog.pg_timezone_names where name = v_item ->> 'tz') then
raise exception 'tz must be an IANA time zone' using errcode = '22023', hint = 'CALENDAR_TZ_INVALID';
end if;
select coalesce(array_agg(value::timestamptz order by value::timestamptz), '{}')
into v_exdates from jsonb_array_elements_text(coalesce(v_item -> 'exdates', '[]'::jsonb));
declare
v_hook regprocedure := to_regprocedure('"public"."before_calendar_event_save"(uuid, uuid, jsonb)');
begin
if v_hook is not null then
execute format('select %s($1::uuid, $2::uuid, $3::jsonb)', v_hook::oid::regproc)
using v_tenant, upsert_calendar_event.calendar_id, v_item;
end if;
end;
if v_id is null then
if v_master is not null then
select "uid" into v_uid from "better_supabase"."calendar_events"
where "id" = v_master and "calendar_id" = upsert_calendar_event.calendar_id and "recurrence_id" is null and "rrule" is not null;
if not found or v_item ->> 'originalStart' is null then
raise exception 'an override needs a recurring event and an originalStart' using errcode = '22023', hint = 'CALENDAR_EVENT_INVALID';
end if;
end if;
if v_item ->> 'startsAt' is null or v_item ->> 'endsAt' is null then
raise exception 'an event needs startsAt and endsAt' using errcode = '22023', hint = 'CALENDAR_EVENT_INVALID';
end if;
v_id := gen_random_uuid();
v_uid := coalesce(v_uid, v_id::text);
insert into "better_supabase"."calendar_events" ("id", "tenant_id", "calendar_id", "uid", "recurrence_id", "original_starts_at", "title", "starts_at", "ends_at", "all_day", "tz", "status", "rrule", "exdates", "description", "location", "color", "kind", "transparency", "auto_decline", "subject_type", "subject_id", "created_by")
values (v_id, v_tenant, upsert_calendar_event.calendar_id, v_uid, v_master, (v_item ->> 'originalStart')::timestamptz, coalesce(v_item ->> 'title', ''), (v_item ->> 'startsAt')::timestamptz, (v_item ->> 'endsAt')::timestamptz, coalesce((v_item ->> 'allDay')::boolean, false), coalesce(v_item ->> 'tz', v_tz), coalesce(v_item ->> 'status', 'confirmed'), nullif(v_item ->> 'rrule', ''), v_exdates, v_item ->> 'description', v_item ->> 'location', v_item ->> 'color', coalesce(v_item ->> 'kind', 'event'), coalesce(v_item ->> 'transparency', 'opaque'), coalesce((v_item ->> 'autoDecline')::boolean, false), v_item ->> 'subjectType', v_item ->> 'subjectId', v_actor);
v_created := true;
else
update "better_supabase"."calendar_events" set
"title" = case when v_item ? 'title' then coalesce(v_item ->> 'title', '') else "title" end,
"starts_at" = case when v_item ? 'startsAt' then (v_item ->> 'startsAt')::timestamptz else "starts_at" end,
"ends_at" = case when v_item ? 'endsAt' then (v_item ->> 'endsAt')::timestamptz else "ends_at" end,
"all_day" = case when v_item ? 'allDay' then coalesce((v_item ->> 'allDay')::boolean, false) else "all_day" end,
"tz" = case when v_item ? 'tz' then coalesce(v_item ->> 'tz', v_tz) else "tz" end,
"status" = case when v_item ? 'status' then coalesce(v_item ->> 'status', 'confirmed') else "status" end,
"rrule" = case when v_item ? 'rrule' then nullif(v_item ->> 'rrule', '') else "rrule" end,
"description" = case when v_item ? 'description' then v_item ->> 'description' else "description" end,
"location" = case when v_item ? 'location' then v_item ->> 'location' else "location" end,
"color" = case when v_item ? 'color' then v_item ->> 'color' else "color" end,
"kind" = case when v_item ? 'kind' then coalesce(v_item ->> 'kind', 'event') else "kind" end,
"transparency" = case when v_item ? 'transparency' then coalesce(v_item ->> 'transparency', 'opaque') else "transparency" end,
"auto_decline" = case when v_item ? 'autoDecline' then coalesce((v_item ->> 'autoDecline')::boolean, false) else "auto_decline" end,
"subject_type" = case when v_item ? 'subjectType' then v_item ->> 'subjectType' else "subject_type" end,
"subject_id" = case when v_item ? 'subjectId' then v_item ->> 'subjectId' else "subject_id" end,
"exdates" = case when v_item ? 'exdates' then v_exdates else "exdates" end,
"updated_at" = now()
where "id" = v_id and "calendar_id" = upsert_calendar_event.calendar_id
returning "uid" into v_uid;
if not found then
raise exception 'event not found' using errcode = 'P0002', hint = 'CALENDAR_EVENT_NOT_FOUND';
end if;
end if;
if v_item ? 'alarms' then
if jsonb_typeof(v_item -> 'alarms') <> 'array' or jsonb_array_length(v_item -> 'alarms') > 20 then
raise exception 'alarms must be an array of at most 20' using errcode = '22023', hint = 'CALENDAR_EVENT_INVALID';
end if;
if exists (
select 1 from jsonb_array_elements(v_item -> 'alarms') x
where jsonb_typeof(x) <> 'object'
or coalesce(x ->> 'method', 'display') not in ('display', 'email', 'audio')
or coalesce(x ->> 'related', 'start') not in ('start', 'end')
or (x ->> 'offsetMinutes' is null) = (x ->> 'at' is null)
or abs(coalesce((x ->> 'offsetMinutes')::integer, 0)) > 525600
or coalesce((x ->> 'repeat')::integer, 0) < 0
or (coalesce((x ->> 'repeat')::integer, 0) > 0) <> (x ->> 'repeatMinutes' is not null)
or coalesce((x ->> 'repeatMinutes')::integer, 1) <= 0
) then
raise exception 'each alarm needs a method of display, email or audio, offsetMinutes or at, and repeat with repeatMinutes' using errcode = '22023', hint = 'CALENDAR_EVENT_INVALID';
end if;
delete from "better_supabase"."calendar_event_alarms" where "event_id" = v_id and "user_id" is null;
insert into "better_supabase"."calendar_event_alarms" ("tenant_id", "event_id", "offset_minutes", "method", "related_to", "trigger_at", "repeat_count", "repeat_minutes", "description", "summary")
select v_tenant, v_id, (x ->> 'offsetMinutes')::integer, coalesce(x ->> 'method', 'display'), coalesce(x ->> 'related', 'start'), (x ->> 'at')::timestamptz, coalesce((x ->> 'repeat')::integer, 0), (x ->> 'repeatMinutes')::integer, x ->> 'description', x ->> 'summary'
from jsonb_array_elements(v_item -> 'alarms') x;
end if;
if exists (select 1 from "better_supabase"."calendar_events" where "id" = v_id and "ends_at" < "starts_at") then
raise exception 'endsAt must not be before startsAt' using errcode = '22023', hint = 'CALENDAR_EVENT_INVALID';
end if;
if v_created then
null;
else
null;
end if;
declare
v_hook regprocedure := to_regprocedure('"public"."after_calendar_event_save"(uuid)');
begin
if v_hook is not null then
execute format('select %s($1::uuid)', v_hook::oid::regproc)
using v_id;
end if;
end;
return jsonb_build_object('id', v_id, 'uid', v_uid, 'created', v_created);
end;
$$;
revoke execute on function "better_supabase"."upsert_calendar_event"(uuid, jsonb, uuid) from public, anon;
grant execute on function "better_supabase"."upsert_calendar_event"(uuid, jsonb, uuid) to authenticated, service_role;
create or replace function "better_supabase"."purge_calendar_event"(event_id uuid)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_id uuid := purge_calendar_event.event_id;
v_tenant uuid;
v_calendar uuid;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select "tenant_id", "calendar_id" into v_tenant, v_calendar from "better_supabase"."calendar_events" where "id" = v_id;
if not found then
raise exception 'event not found' using errcode = 'P0002', hint = 'CALENDAR_EVENT_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_tenant, 'calendar.write'), false)) then
raise exception 'not allowed to change this calendar' using errcode = '42501', hint = 'CALENDAR_FORBIDDEN';
end if;
delete from "better_supabase"."calendar_events" where "id" = v_id;
null;
return true;
end;
$$;
revoke execute on function "better_supabase"."purge_calendar_event"(uuid) from public, anon;
grant execute on function "better_supabase"."purge_calendar_event"(uuid) to authenticated, service_role;
-- Removes one occurrence of a series: adds an EXDATE and drops its override.
create or replace function "better_supabase"."cancel_calendar_occurrence"(event_id uuid, original_starts_at timestamptz)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_id uuid := cancel_calendar_occurrence.event_id;
v_tenant uuid;
v_calendar uuid;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select "tenant_id", "calendar_id" into v_tenant, v_calendar from "better_supabase"."calendar_events"
where "id" = v_id and "rrule" is not null for update;
if not found then
raise exception 'recurring event not found' using errcode = 'P0002', hint = 'CALENDAR_EVENT_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_tenant, 'calendar.write'), false)) then
raise exception 'not allowed to change this calendar' using errcode = '42501', hint = 'CALENDAR_FORBIDDEN';
end if;
update "better_supabase"."calendar_events" set "exdates" = (
select array_agg(distinct d order by d) from unnest(array_append("exdates", cancel_calendar_occurrence.original_starts_at)) d
), "updated_at" = now()
where "id" = v_id;
delete from "better_supabase"."calendar_events" where "recurrence_id" = v_id and "original_starts_at" = cancel_calendar_occurrence.original_starts_at;
null;
return true;
end;
$$;
revoke execute on function "better_supabase"."cancel_calendar_occurrence"(uuid, timestamptz) from public, anon;
grant execute on function "better_supabase"."cancel_calendar_occurrence"(uuid, timestamptz) to authenticated, service_role;
-- "This and following": the series ends before split_at (before_rrule, which
-- splitRule computes), and item starts a new series from split_at with a new
-- uid. Overrides and EXDATEs from split_at on move to the new series.
create or replace function "better_supabase"."split_calendar_event"(event_id uuid, split_at timestamptz, before_rrule text, item jsonb)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_id uuid := split_calendar_event.event_id;
v_tenant uuid;
v_calendar uuid;
v_uid text;
v_starts timestamptz;
v_exdates timestamptz[];
v_new jsonb;
v_new_id uuid;
v_new_uid text;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select "tenant_id", "calendar_id", "uid", "starts_at", "exdates"
into v_tenant, v_calendar, v_uid, v_starts, v_exdates
from "better_supabase"."calendar_events" where "id" = v_id and "rrule" is not null for update;
if not found then
raise exception 'recurring event not found' using errcode = 'P0002', hint = 'CALENDAR_EVENT_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_tenant, 'calendar.write'), false)) then
raise exception 'not allowed to change this calendar' using errcode = '42501', hint = 'CALENDAR_FORBIDDEN';
end if;
if split_calendar_event.split_at <= v_starts or nullif(split_calendar_event.before_rrule, '') is null then
raise exception 'split at a later occurrence, with the rule that ends the series before it' using errcode = '22023', hint = 'CALENDAR_SPLIT_INVALID';
end if;
v_new_uid := v_uid || '-' || to_char(split_calendar_event.split_at at time zone 'UTC', 'YYYYMMDD"T"HH24MISS"Z"');
update "better_supabase"."calendar_events" set "rrule" = split_calendar_event.before_rrule,
"exdates" = coalesce((select array_agg(d order by d) from unnest(v_exdates) d where d < split_calendar_event.split_at), '{}'),
"updated_at" = now()
where "id" = v_id;
v_new := "better_supabase"."upsert_calendar_event"(v_calendar, (coalesce(split_calendar_event.item, '{}'::jsonb) - 'id' - 'recurrenceId' - 'originalStart') || jsonb_build_object(
'uid', v_new_uid,
'exdates', coalesce((select jsonb_agg(d order by d) from unnest(v_exdates) d where d >= split_calendar_event.split_at), '[]'::jsonb)
));
v_new_id := (v_new ->> 'id')::uuid;
update "better_supabase"."calendar_events" set "recurrence_id" = v_new_id, "uid" = v_new_uid, "updated_at" = now()
where "recurrence_id" = v_id and "original_starts_at" >= split_calendar_event.split_at;
null;
return jsonb_build_object('id', v_new_id, 'uid', v_new_uid);
end;
$$;
revoke execute on function "better_supabase"."split_calendar_event"(uuid, timestamptz, text, jsonb) from public, anon;
grant execute on function "better_supabase"."split_calendar_event"(uuid, timestamptz, text, jsonb) to authenticated, service_role;
-- Upserts source.events, parsed iCalendar events, by uid (and originalStart for overrides).
-- Overrides whose series is not in the calendar are skipped.
create or replace function "better_supabase"."import_calendar_events"(calendar_id uuid, source jsonb)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_item jsonb;
v_id uuid;
v_master uuid;
v_created integer := 0;
v_updated integer := 0;
v_skipped integer := 0;
begin
for v_item in
select value from jsonb_array_elements(coalesce(import_calendar_events.source -> 'events', '[]'::jsonb))
order by value ? 'originalStart', value ->> 'uid'
loop
v_id := null;
if v_item ->> 'originalStart' is null then
select "id" into v_id from "better_supabase"."calendar_events"
where "calendar_id" = import_calendar_events.calendar_id and "uid" = v_item ->> 'uid' and "original_starts_at" is null;
else
select "id" into v_master from "better_supabase"."calendar_events"
where "calendar_id" = import_calendar_events.calendar_id and "uid" = v_item ->> 'uid' and "original_starts_at" is null and "rrule" is not null;
if v_master is null then
v_skipped := v_skipped + 1;
continue;
end if;
select "id" into v_id from "better_supabase"."calendar_events"
where "calendar_id" = import_calendar_events.calendar_id and "uid" = v_item ->> 'uid'
and "original_starts_at" = (v_item ->> 'originalStart')::timestamptz;
v_item := v_item || jsonb_build_object('recurrenceId', v_master);
end if;
if v_id is null then
perform "better_supabase"."upsert_calendar_event"(import_calendar_events.calendar_id, v_item - 'id');
v_created := v_created + 1;
else
perform "better_supabase"."upsert_calendar_event"(import_calendar_events.calendar_id, (v_item - 'recurrenceId' - 'originalStart') || jsonb_build_object('id', v_id));
v_updated := v_updated + 1;
end if;
end loop;
return jsonb_build_object('created', v_created, 'updated', v_updated, 'skipped', v_skipped);
end;
$$;
revoke execute on function "better_supabase"."import_calendar_events"(uuid, jsonb) from public, anon;
grant execute on function "better_supabase"."import_calendar_events"(uuid, jsonb) to authenticated, service_role;
-- The events that can fall in [range_start, range_end): single events that
-- overlap it, series that start before its end, and the overrides of those
-- series. occurrences() in TypeScript expands the series. Runs as the
-- caller, so the tables' policies decide what it returns.
create or replace function "better_supabase"."list_calendar_events"(calendar_ids uuid[], range_start timestamptz, range_end timestamptz)
returns jsonb
language sql
stable
security invoker
set search_path = ''
as $$
select coalesce(jsonb_agg(jsonb_build_object('id', e."id", 'calendarId', e."calendar_id", 'uid', e."uid", 'title', e."title", 'startsAt', e."starts_at", 'endsAt', e."ends_at", 'allDay', e."all_day", 'tz', e."tz", 'status', e."status", 'rrule', e."rrule", 'exdates', to_jsonb(e."exdates"), 'recurrenceId', e."recurrence_id", 'originalStart', e."original_starts_at", 'description', e."description", 'location', e."location", 'color', e."color", 'subjectType', e."subject_type", 'subjectId', e."subject_id") || jsonb_build_object('updatedAt', e."updated_at", 'kind', e."kind", 'transparency', e."transparency", 'autoDecline', e."auto_decline", 'attendees', coalesce((select jsonb_agg(jsonb_build_object('email', a."email", 'name', a."name", 'userId', a."user_id", 'role', a."role", 'response', a."response") order by a."email") from "better_supabase"."calendar_attendees" a where a."event_id" = e."id"), '[]'::jsonb), 'alarms', coalesce((select jsonb_agg(jsonb_build_object('id', l."id", 'method', l."method", 'offsetMinutes', l."offset_minutes", 'related', l."related_to", 'at', l."trigger_at", 'repeat', l."repeat_count", 'repeatMinutes', l."repeat_minutes", 'description', l."description", 'summary', l."summary") order by l."offset_minutes" nulls last, l."trigger_at", l."id") from "better_supabase"."calendar_event_alarms" l where l."event_id" = e."id" and (l."user_id" is null or l."user_id" = (select auth.uid()))), '[]'::jsonb)) order by e."starts_at", e."id"), '[]'::jsonb)
from "better_supabase"."calendar_events" e
where e."calendar_id" = any (list_calendar_events.calendar_ids)
and e."starts_at" < list_calendar_events.range_end
and (
(e."rrule" is null and e."recurrence_id" is null and e."ends_at" > list_calendar_events.range_start)
or e."rrule" is not null
or (e."recurrence_id" is not null and (e."ends_at" > list_calendar_events.range_start or e."original_starts_at" >= list_calendar_events.range_start - interval '1 year'))
)
$$;
revoke execute on function "better_supabase"."list_calendar_events"(uuid[], timestamptz, timestamptz) from public, anon;
grant execute on function "better_supabase"."list_calendar_events"(uuid[], timestamptz, timestamptz) to authenticated, service_role;
create or replace function "better_supabase"."list_calendars"(tenant uuid)
returns jsonb
language sql
stable
security invoker
set search_path = ''
as $$
select coalesce(jsonb_agg(jsonb_build_object('id', k."id", 'name', k."name", 'tz', k."tz", 'color', k."color", 'ownerType', k."owner_type", 'ownerId', k."owner_id") order by k."name"), '[]'::jsonb)
from "better_supabase"."calendars" k
where k."tenant_id" = list_calendars.tenant
$$;
revoke execute on function "better_supabase"."list_calendars"(uuid) from public, anon;
grant execute on function "better_supabase"."list_calendars"(uuid) to authenticated, service_role;
-- The stored calendar events as a keyset page, the earliest start first (ADR
-- 1077): single events, series and their overrides. filters is { calendarId,
-- status, subjectType, subjectId (each a value or a list), from, to (the first
-- value counts: events that overlap the window), search (title, location),
-- sort (startsAt, title, updatedAt), direction, facet, count, limit, after,
-- offset, paged, total, totalCap }.
create or replace function "better_supabase"."page_calendar_events"(filters jsonb default '{}')
returns jsonb
language plpgsql
stable
security invoker
set search_path = ''
as $$
#variable_conflict use_variable
declare
v_filters jsonb := coalesce(page_calendar_events.filters, '{}'::jsonb);
v_search text := case when nullif(trim(v_filters ->> 'search'), '') is null then null
else '%' || replace(replace(replace(trim(v_filters ->> 'search'), '\', '\\'), '%', '\%'), '_', '\_') || '%' end;
v_limit integer := least(greatest(coalesce((v_filters ->> 'limit')::integer, 50), 1), 200);
v_offset integer := greatest(coalesce((v_filters ->> 'offset')::integer, 0), 0);
v_after jsonb := v_filters -> 'after';
v_paged boolean := coalesce((v_filters ->> 'paged')::boolean, false);
v_total_cap integer := least(greatest(coalesce((v_filters ->> 'totalCap')::integer, 10000), 1), 100000);
v_items jsonb;
v_more boolean;
v_next jsonb;
v_total integer;
v_sort text := nullif(v_filters ->> 'sort', '');
v_dir text := nullif(v_filters ->> 'direction', '');
v_facet text := nullif(v_filters ->> 'facet', '');
v_count boolean := coalesce((v_filters ->> 'count')::boolean, false);
v_result jsonb;
begin
if v_filters ? 'after' and v_filters ? 'offset' then
raise exception 'after and offset are two ways to page; pass one' using errcode = '22023', hint = 'CALENDAR_INVALID';
end if;
if v_facet is not null then
if v_facet not in ('status', 'calendarId') then
raise exception 'facet is one of status, calendarId' using errcode = '22023', hint = 'CALENDAR_INVALID';
end if;
with s as (
select (case v_facet when 'status' then (e."status")::text when 'calendarId' then (e."calendar_id"::text)::text end) as v
from "better_supabase"."calendar_events" e
where (not (v_filters -> 'calendarId' is not null and jsonb_typeof(v_filters -> 'calendarId') <> 'null' and not (jsonb_typeof(v_filters -> 'calendarId') = 'array' and jsonb_array_length(v_filters -> 'calendarId') = 0)) or e."calendar_id" = any((case when jsonb_typeof(v_filters -> 'calendarId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'calendarId')) else array[v_filters ->> 'calendarId'] end)::uuid[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (e."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (e."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (e."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or e."ends_at" > (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or e."starts_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or e."title" ilike v_search or e."location" ilike v_search)
limit v_total_cap + 1
)
select jsonb_build_object('facet', v_facet, 'values', coalesce(jsonb_agg(jsonb_build_object('value', g.v, 'count', jsonb_build_object('value', g.n, 'exact', g.exact)) order by g.n desc, g.v), '[]'::jsonb))
into v_result
from (
select s.v, count(*) as n, (select count(*) from s) <= v_total_cap as exact
from s where s.v is not null group by s.v order by n desc, s.v limit 100
) g;
return v_result;
end if;
if v_count then
select count(*) into v_total from (
select 1 from "better_supabase"."calendar_events" e
where (not (v_filters -> 'calendarId' is not null and jsonb_typeof(v_filters -> 'calendarId') <> 'null' and not (jsonb_typeof(v_filters -> 'calendarId') = 'array' and jsonb_array_length(v_filters -> 'calendarId') = 0)) or e."calendar_id" = any((case when jsonb_typeof(v_filters -> 'calendarId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'calendarId')) else array[v_filters ->> 'calendarId'] end)::uuid[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (e."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (e."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (e."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or e."ends_at" > (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or e."starts_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or e."title" ilike v_search or e."location" ilike v_search)
limit v_total_cap + 1
) c;
return jsonb_build_object('value', least(v_total, v_total_cap), 'exact', v_total <= v_total_cap);
end if;
if v_sort is not null and v_sort not in ('startsAt', 'title', 'updatedAt') then
raise exception 'sort is one of startsAt, title, updatedAt' using errcode = '22023', hint = 'CALENDAR_INVALID';
end if;
if v_dir is not null and v_dir not in ('asc', 'desc') then
raise exception 'direction is asc or desc' using errcode = '22023', hint = 'CALENDAR_INVALID';
end if;
if v_sort is null and coalesce(v_dir, 'asc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'CALENDAR_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', e."id", 'calendarId', e."calendar_id", 'uid', e."uid", 'title', e."title", 'startsAt', e."starts_at", 'endsAt', e."ends_at", 'allDay', e."all_day", 'tz', e."tz", 'status', e."status", 'rrule', e."rrule", 'exdates', to_jsonb(e."exdates"), 'recurrenceId', e."recurrence_id", 'originalStart', e."original_starts_at", 'description', e."description", 'location', e."location", 'color', e."color", 'subjectType', e."subject_type", 'subjectId', e."subject_id") || jsonb_build_object('updatedAt', e."updated_at", 'kind', e."kind", 'transparency', e."transparency", 'autoDecline', e."auto_decline", 'attendees', coalesce((select jsonb_agg(jsonb_build_object('email', a."email", 'name', a."name", 'userId', a."user_id", 'role', a."role", 'response', a."response") order by a."email") from "better_supabase"."calendar_attendees" a where a."event_id" = e."id"), '[]'::jsonb), 'alarms', coalesce((select jsonb_agg(jsonb_build_object('id', l."id", 'method', l."method", 'offsetMinutes', l."offset_minutes", 'related', l."related_to", 'at', l."trigger_at", 'repeat', l."repeat_count", 'repeatMinutes', l."repeat_minutes", 'description', l."description", 'summary', l."summary") order by l."offset_minutes" nulls last, l."trigger_at", l."id") from "better_supabase"."calendar_event_alarms" l where l."event_id" = e."id" and (l."user_id" is null or l."user_id" = (select auth.uid()))), '[]'::jsonb)) as j, jsonb_build_array((e."starts_at")::text, (e."id")::text) as k
from (
select * from "better_supabase"."calendar_events" e
where (not (v_filters -> 'calendarId' is not null and jsonb_typeof(v_filters -> 'calendarId') <> 'null' and not (jsonb_typeof(v_filters -> 'calendarId') = 'array' and jsonb_array_length(v_filters -> 'calendarId') = 0)) or e."calendar_id" = any((case when jsonb_typeof(v_filters -> 'calendarId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'calendarId')) else array[v_filters ->> 'calendarId'] end)::uuid[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (e."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (e."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (e."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or e."ends_at" > (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or e."starts_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or e."title" ilike v_search or e."location" ilike v_search)
and (v_after is null or (e."starts_at", e."id") > ((v_after ->> 0)::timestamptz, (v_after ->> 1)::uuid))
order by e."starts_at" asc, e."id" asc
limit v_limit + 1 offset v_offset
) e
) p;
elsif v_sort is null and coalesce(v_dir, 'asc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'CALENDAR_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', e."id", 'calendarId', e."calendar_id", 'uid', e."uid", 'title', e."title", 'startsAt', e."starts_at", 'endsAt', e."ends_at", 'allDay', e."all_day", 'tz', e."tz", 'status', e."status", 'rrule', e."rrule", 'exdates', to_jsonb(e."exdates"), 'recurrenceId', e."recurrence_id", 'originalStart', e."original_starts_at", 'description', e."description", 'location', e."location", 'color', e."color", 'subjectType', e."subject_type", 'subjectId', e."subject_id") || jsonb_build_object('updatedAt', e."updated_at", 'kind', e."kind", 'transparency', e."transparency", 'autoDecline', e."auto_decline", 'attendees', coalesce((select jsonb_agg(jsonb_build_object('email', a."email", 'name', a."name", 'userId', a."user_id", 'role', a."role", 'response', a."response") order by a."email") from "better_supabase"."calendar_attendees" a where a."event_id" = e."id"), '[]'::jsonb), 'alarms', coalesce((select jsonb_agg(jsonb_build_object('id', l."id", 'method', l."method", 'offsetMinutes', l."offset_minutes", 'related', l."related_to", 'at', l."trigger_at", 'repeat', l."repeat_count", 'repeatMinutes', l."repeat_minutes", 'description', l."description", 'summary', l."summary") order by l."offset_minutes" nulls last, l."trigger_at", l."id") from "better_supabase"."calendar_event_alarms" l where l."event_id" = e."id" and (l."user_id" is null or l."user_id" = (select auth.uid()))), '[]'::jsonb)) as j, jsonb_build_array((e."starts_at")::text, (e."id")::text) as k
from (
select * from "better_supabase"."calendar_events" e
where (not (v_filters -> 'calendarId' is not null and jsonb_typeof(v_filters -> 'calendarId') <> 'null' and not (jsonb_typeof(v_filters -> 'calendarId') = 'array' and jsonb_array_length(v_filters -> 'calendarId') = 0)) or e."calendar_id" = any((case when jsonb_typeof(v_filters -> 'calendarId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'calendarId')) else array[v_filters ->> 'calendarId'] end)::uuid[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (e."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (e."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (e."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or e."ends_at" > (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or e."starts_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or e."title" ilike v_search or e."location" ilike v_search)
and (v_after is null or (e."starts_at" <= (v_after ->> 0)::timestamptz and ((e."starts_at" < (v_after ->> 0)::timestamptz) or (e."starts_at" = (v_after ->> 0)::timestamptz and e."id" > (v_after ->> 1)::uuid))))
order by e."starts_at" desc, e."id" asc
limit v_limit + 1 offset v_offset
) e
) p;
elsif v_sort = 'startsAt' and coalesce(v_dir, 'asc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'CALENDAR_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', e."id", 'calendarId', e."calendar_id", 'uid', e."uid", 'title', e."title", 'startsAt', e."starts_at", 'endsAt', e."ends_at", 'allDay', e."all_day", 'tz', e."tz", 'status', e."status", 'rrule', e."rrule", 'exdates', to_jsonb(e."exdates"), 'recurrenceId', e."recurrence_id", 'originalStart', e."original_starts_at", 'description', e."description", 'location', e."location", 'color', e."color", 'subjectType', e."subject_type", 'subjectId', e."subject_id") || jsonb_build_object('updatedAt', e."updated_at", 'kind', e."kind", 'transparency', e."transparency", 'autoDecline', e."auto_decline", 'attendees', coalesce((select jsonb_agg(jsonb_build_object('email', a."email", 'name', a."name", 'userId', a."user_id", 'role', a."role", 'response', a."response") order by a."email") from "better_supabase"."calendar_attendees" a where a."event_id" = e."id"), '[]'::jsonb), 'alarms', coalesce((select jsonb_agg(jsonb_build_object('id', l."id", 'method', l."method", 'offsetMinutes', l."offset_minutes", 'related', l."related_to", 'at', l."trigger_at", 'repeat', l."repeat_count", 'repeatMinutes', l."repeat_minutes", 'description', l."description", 'summary', l."summary") order by l."offset_minutes" nulls last, l."trigger_at", l."id") from "better_supabase"."calendar_event_alarms" l where l."event_id" = e."id" and (l."user_id" is null or l."user_id" = (select auth.uid()))), '[]'::jsonb)) as j, jsonb_build_array((e."starts_at")::text, (e."id")::text) as k
from (
select * from "better_supabase"."calendar_events" e
where (not (v_filters -> 'calendarId' is not null and jsonb_typeof(v_filters -> 'calendarId') <> 'null' and not (jsonb_typeof(v_filters -> 'calendarId') = 'array' and jsonb_array_length(v_filters -> 'calendarId') = 0)) or e."calendar_id" = any((case when jsonb_typeof(v_filters -> 'calendarId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'calendarId')) else array[v_filters ->> 'calendarId'] end)::uuid[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (e."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (e."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (e."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or e."ends_at" > (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or e."starts_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or e."title" ilike v_search or e."location" ilike v_search)
and (v_after is null or (e."starts_at", e."id") > ((v_after ->> 0)::timestamptz, (v_after ->> 1)::uuid))
order by e."starts_at" asc, e."id" asc
limit v_limit + 1 offset v_offset
) e
) p;
elsif v_sort = 'startsAt' and coalesce(v_dir, 'asc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'CALENDAR_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', e."id", 'calendarId', e."calendar_id", 'uid', e."uid", 'title', e."title", 'startsAt', e."starts_at", 'endsAt', e."ends_at", 'allDay', e."all_day", 'tz', e."tz", 'status', e."status", 'rrule', e."rrule", 'exdates', to_jsonb(e."exdates"), 'recurrenceId', e."recurrence_id", 'originalStart', e."original_starts_at", 'description', e."description", 'location', e."location", 'color', e."color", 'subjectType', e."subject_type", 'subjectId', e."subject_id") || jsonb_build_object('updatedAt', e."updated_at", 'kind', e."kind", 'transparency', e."transparency", 'autoDecline', e."auto_decline", 'attendees', coalesce((select jsonb_agg(jsonb_build_object('email', a."email", 'name', a."name", 'userId', a."user_id", 'role', a."role", 'response', a."response") order by a."email") from "better_supabase"."calendar_attendees" a where a."event_id" = e."id"), '[]'::jsonb), 'alarms', coalesce((select jsonb_agg(jsonb_build_object('id', l."id", 'method', l."method", 'offsetMinutes', l."offset_minutes", 'related', l."related_to", 'at', l."trigger_at", 'repeat', l."repeat_count", 'repeatMinutes', l."repeat_minutes", 'description', l."description", 'summary', l."summary") order by l."offset_minutes" nulls last, l."trigger_at", l."id") from "better_supabase"."calendar_event_alarms" l where l."event_id" = e."id" and (l."user_id" is null or l."user_id" = (select auth.uid()))), '[]'::jsonb)) as j, jsonb_build_array((e."starts_at")::text, (e."id")::text) as k
from (
select * from "better_supabase"."calendar_events" e
where (not (v_filters -> 'calendarId' is not null and jsonb_typeof(v_filters -> 'calendarId') <> 'null' and not (jsonb_typeof(v_filters -> 'calendarId') = 'array' and jsonb_array_length(v_filters -> 'calendarId') = 0)) or e."calendar_id" = any((case when jsonb_typeof(v_filters -> 'calendarId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'calendarId')) else array[v_filters ->> 'calendarId'] end)::uuid[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (e."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (e."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (e."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or e."ends_at" > (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or e."starts_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or e."title" ilike v_search or e."location" ilike v_search)
and (v_after is null or (e."starts_at" <= (v_after ->> 0)::timestamptz and ((e."starts_at" < (v_after ->> 0)::timestamptz) or (e."starts_at" = (v_after ->> 0)::timestamptz and e."id" > (v_after ->> 1)::uuid))))
order by e."starts_at" desc, e."id" asc
limit v_limit + 1 offset v_offset
) e
) p;
elsif v_sort = 'title' and coalesce(v_dir, 'asc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'CALENDAR_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', e."id", 'calendarId', e."calendar_id", 'uid', e."uid", 'title', e."title", 'startsAt', e."starts_at", 'endsAt', e."ends_at", 'allDay', e."all_day", 'tz', e."tz", 'status', e."status", 'rrule', e."rrule", 'exdates', to_jsonb(e."exdates"), 'recurrenceId', e."recurrence_id", 'originalStart', e."original_starts_at", 'description', e."description", 'location', e."location", 'color', e."color", 'subjectType', e."subject_type", 'subjectId', e."subject_id") || jsonb_build_object('updatedAt', e."updated_at", 'kind', e."kind", 'transparency', e."transparency", 'autoDecline', e."auto_decline", 'attendees', coalesce((select jsonb_agg(jsonb_build_object('email', a."email", 'name', a."name", 'userId', a."user_id", 'role', a."role", 'response', a."response") order by a."email") from "better_supabase"."calendar_attendees" a where a."event_id" = e."id"), '[]'::jsonb), 'alarms', coalesce((select jsonb_agg(jsonb_build_object('id', l."id", 'method', l."method", 'offsetMinutes', l."offset_minutes", 'related', l."related_to", 'at', l."trigger_at", 'repeat', l."repeat_count", 'repeatMinutes', l."repeat_minutes", 'description', l."description", 'summary', l."summary") order by l."offset_minutes" nulls last, l."trigger_at", l."id") from "better_supabase"."calendar_event_alarms" l where l."event_id" = e."id" and (l."user_id" is null or l."user_id" = (select auth.uid()))), '[]'::jsonb)) as j, jsonb_build_array((lower(e."title"))::text, (e."id")::text) as k
from (
select * from "better_supabase"."calendar_events" e
where (not (v_filters -> 'calendarId' is not null and jsonb_typeof(v_filters -> 'calendarId') <> 'null' and not (jsonb_typeof(v_filters -> 'calendarId') = 'array' and jsonb_array_length(v_filters -> 'calendarId') = 0)) or e."calendar_id" = any((case when jsonb_typeof(v_filters -> 'calendarId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'calendarId')) else array[v_filters ->> 'calendarId'] end)::uuid[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (e."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (e."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (e."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or e."ends_at" > (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or e."starts_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or e."title" ilike v_search or e."location" ilike v_search)
and (v_after is null or (lower(e."title"), e."id") > ((v_after ->> 0)::text, (v_after ->> 1)::uuid))
order by lower(e."title") asc, e."id" asc
limit v_limit + 1 offset v_offset
) e
) p;
elsif v_sort = 'title' and coalesce(v_dir, 'asc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'CALENDAR_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', e."id", 'calendarId', e."calendar_id", 'uid', e."uid", 'title', e."title", 'startsAt', e."starts_at", 'endsAt', e."ends_at", 'allDay', e."all_day", 'tz', e."tz", 'status', e."status", 'rrule', e."rrule", 'exdates', to_jsonb(e."exdates"), 'recurrenceId', e."recurrence_id", 'originalStart', e."original_starts_at", 'description', e."description", 'location', e."location", 'color', e."color", 'subjectType', e."subject_type", 'subjectId', e."subject_id") || jsonb_build_object('updatedAt', e."updated_at", 'kind', e."kind", 'transparency', e."transparency", 'autoDecline', e."auto_decline", 'attendees', coalesce((select jsonb_agg(jsonb_build_object('email', a."email", 'name', a."name", 'userId', a."user_id", 'role', a."role", 'response', a."response") order by a."email") from "better_supabase"."calendar_attendees" a where a."event_id" = e."id"), '[]'::jsonb), 'alarms', coalesce((select jsonb_agg(jsonb_build_object('id', l."id", 'method', l."method", 'offsetMinutes', l."offset_minutes", 'related', l."related_to", 'at', l."trigger_at", 'repeat', l."repeat_count", 'repeatMinutes', l."repeat_minutes", 'description', l."description", 'summary', l."summary") order by l."offset_minutes" nulls last, l."trigger_at", l."id") from "better_supabase"."calendar_event_alarms" l where l."event_id" = e."id" and (l."user_id" is null or l."user_id" = (select auth.uid()))), '[]'::jsonb)) as j, jsonb_build_array((lower(e."title"))::text, (e."id")::text) as k
from (
select * from "better_supabase"."calendar_events" e
where (not (v_filters -> 'calendarId' is not null and jsonb_typeof(v_filters -> 'calendarId') <> 'null' and not (jsonb_typeof(v_filters -> 'calendarId') = 'array' and jsonb_array_length(v_filters -> 'calendarId') = 0)) or e."calendar_id" = any((case when jsonb_typeof(v_filters -> 'calendarId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'calendarId')) else array[v_filters ->> 'calendarId'] end)::uuid[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (e."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (e."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (e."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or e."ends_at" > (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or e."starts_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or e."title" ilike v_search or e."location" ilike v_search)
and (v_after is null or (lower(e."title") <= (v_after ->> 0)::text and ((lower(e."title") < (v_after ->> 0)::text) or (lower(e."title") = (v_after ->> 0)::text and e."id" > (v_after ->> 1)::uuid))))
order by lower(e."title") desc, e."id" asc
limit v_limit + 1 offset v_offset
) e
) p;
elsif v_sort = 'updatedAt' and coalesce(v_dir, 'desc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'CALENDAR_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', e."id", 'calendarId', e."calendar_id", 'uid', e."uid", 'title', e."title", 'startsAt', e."starts_at", 'endsAt', e."ends_at", 'allDay', e."all_day", 'tz', e."tz", 'status', e."status", 'rrule', e."rrule", 'exdates', to_jsonb(e."exdates"), 'recurrenceId', e."recurrence_id", 'originalStart', e."original_starts_at", 'description', e."description", 'location', e."location", 'color', e."color", 'subjectType', e."subject_type", 'subjectId', e."subject_id") || jsonb_build_object('updatedAt', e."updated_at", 'kind', e."kind", 'transparency', e."transparency", 'autoDecline', e."auto_decline", 'attendees', coalesce((select jsonb_agg(jsonb_build_object('email', a."email", 'name', a."name", 'userId', a."user_id", 'role', a."role", 'response', a."response") order by a."email") from "better_supabase"."calendar_attendees" a where a."event_id" = e."id"), '[]'::jsonb), 'alarms', coalesce((select jsonb_agg(jsonb_build_object('id', l."id", 'method', l."method", 'offsetMinutes', l."offset_minutes", 'related', l."related_to", 'at', l."trigger_at", 'repeat', l."repeat_count", 'repeatMinutes', l."repeat_minutes", 'description', l."description", 'summary', l."summary") order by l."offset_minutes" nulls last, l."trigger_at", l."id") from "better_supabase"."calendar_event_alarms" l where l."event_id" = e."id" and (l."user_id" is null or l."user_id" = (select auth.uid()))), '[]'::jsonb)) as j, jsonb_build_array((e."updated_at")::text, (e."id")::text) as k
from (
select * from "better_supabase"."calendar_events" e
where (not (v_filters -> 'calendarId' is not null and jsonb_typeof(v_filters -> 'calendarId') <> 'null' and not (jsonb_typeof(v_filters -> 'calendarId') = 'array' and jsonb_array_length(v_filters -> 'calendarId') = 0)) or e."calendar_id" = any((case when jsonb_typeof(v_filters -> 'calendarId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'calendarId')) else array[v_filters ->> 'calendarId'] end)::uuid[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (e."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (e."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (e."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or e."ends_at" > (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or e."starts_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or e."title" ilike v_search or e."location" ilike v_search)
and (v_after is null or (e."updated_at", e."id") > ((v_after ->> 0)::timestamptz, (v_after ->> 1)::uuid))
order by e."updated_at" asc, e."id" asc
limit v_limit + 1 offset v_offset
) e
) p;
elsif v_sort = 'updatedAt' and coalesce(v_dir, 'desc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'CALENDAR_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', e."id", 'calendarId', e."calendar_id", 'uid', e."uid", 'title', e."title", 'startsAt', e."starts_at", 'endsAt', e."ends_at", 'allDay', e."all_day", 'tz', e."tz", 'status', e."status", 'rrule', e."rrule", 'exdates', to_jsonb(e."exdates"), 'recurrenceId', e."recurrence_id", 'originalStart', e."original_starts_at", 'description', e."description", 'location', e."location", 'color', e."color", 'subjectType', e."subject_type", 'subjectId', e."subject_id") || jsonb_build_object('updatedAt', e."updated_at", 'kind', e."kind", 'transparency', e."transparency", 'autoDecline', e."auto_decline", 'attendees', coalesce((select jsonb_agg(jsonb_build_object('email', a."email", 'name', a."name", 'userId', a."user_id", 'role', a."role", 'response', a."response") order by a."email") from "better_supabase"."calendar_attendees" a where a."event_id" = e."id"), '[]'::jsonb), 'alarms', coalesce((select jsonb_agg(jsonb_build_object('id', l."id", 'method', l."method", 'offsetMinutes', l."offset_minutes", 'related', l."related_to", 'at', l."trigger_at", 'repeat', l."repeat_count", 'repeatMinutes', l."repeat_minutes", 'description', l."description", 'summary', l."summary") order by l."offset_minutes" nulls last, l."trigger_at", l."id") from "better_supabase"."calendar_event_alarms" l where l."event_id" = e."id" and (l."user_id" is null or l."user_id" = (select auth.uid()))), '[]'::jsonb)) as j, jsonb_build_array((e."updated_at")::text, (e."id")::text) as k
from (
select * from "better_supabase"."calendar_events" e
where (not (v_filters -> 'calendarId' is not null and jsonb_typeof(v_filters -> 'calendarId') <> 'null' and not (jsonb_typeof(v_filters -> 'calendarId') = 'array' and jsonb_array_length(v_filters -> 'calendarId') = 0)) or e."calendar_id" = any((case when jsonb_typeof(v_filters -> 'calendarId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'calendarId')) else array[v_filters ->> 'calendarId'] end)::uuid[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (e."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (e."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (e."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or e."ends_at" > (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or e."starts_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or e."title" ilike v_search or e."location" ilike v_search)
and (v_after is null or (e."updated_at" <= (v_after ->> 0)::timestamptz and ((e."updated_at" < (v_after ->> 0)::timestamptz) or (e."updated_at" = (v_after ->> 0)::timestamptz and e."id" > (v_after ->> 1)::uuid))))
order by e."updated_at" desc, e."id" asc
limit v_limit + 1 offset v_offset
) e
) p;
end if;
if not v_paged then
return v_items;
end if;
if coalesce((v_filters ->> 'total')::boolean, false) then
select count(*) into v_total from (
select 1 from "better_supabase"."calendar_events" e
where (not (v_filters -> 'calendarId' is not null and jsonb_typeof(v_filters -> 'calendarId') <> 'null' and not (jsonb_typeof(v_filters -> 'calendarId') = 'array' and jsonb_array_length(v_filters -> 'calendarId') = 0)) or e."calendar_id" = any((case when jsonb_typeof(v_filters -> 'calendarId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'calendarId')) else array[v_filters ->> 'calendarId'] end)::uuid[]))
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (e."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'subjectType' is not null and jsonb_typeof(v_filters -> 'subjectType') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectType') = 'array' and jsonb_array_length(v_filters -> 'subjectType') = 0)) or (e."subject_type")::text = any((case when jsonb_typeof(v_filters -> 'subjectType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectType')) else array[v_filters ->> 'subjectType'] end)::text[]))
and (not (v_filters -> 'subjectId' is not null and jsonb_typeof(v_filters -> 'subjectId') <> 'null' and not (jsonb_typeof(v_filters -> 'subjectId') = 'array' and jsonb_array_length(v_filters -> 'subjectId') = 0)) or (e."subject_id")::text = any((case when jsonb_typeof(v_filters -> 'subjectId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'subjectId')) else array[v_filters ->> 'subjectId'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or e."ends_at" > (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or e."starts_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or e."title" ilike v_search or e."location" ilike v_search)
limit v_total_cap + 1
) c;
end if;
return jsonb_build_object('items', v_items, 'hasMore', v_more, 'nextCursor', case when v_more then v_next end)
|| case when v_total is null then '{}'::jsonb
else jsonb_build_object('total', least(v_total, v_total_cap), 'totalCapped', v_total > v_total_cap) end;
end;
$$;
revoke execute on function "better_supabase"."page_calendar_events"(jsonb) from public, anon;
grant execute on function "better_supabase"."page_calendar_events"(jsonb) to authenticated, service_role;
-- One event the caller reads, or null.
create or replace function "better_supabase"."get_calendar_event"(event_id uuid)
returns jsonb
language sql
stable
security invoker
set search_path = ''
as $$
select jsonb_build_object('id', e."id", 'calendarId', e."calendar_id", 'uid', e."uid", 'title', e."title", 'startsAt', e."starts_at", 'endsAt', e."ends_at", 'allDay', e."all_day", 'tz', e."tz", 'status', e."status", 'rrule', e."rrule", 'exdates', to_jsonb(e."exdates"), 'recurrenceId', e."recurrence_id", 'originalStart', e."original_starts_at", 'description', e."description", 'location', e."location", 'color', e."color", 'subjectType', e."subject_type", 'subjectId', e."subject_id") || jsonb_build_object('updatedAt', e."updated_at", 'kind', e."kind", 'transparency', e."transparency", 'autoDecline', e."auto_decline", 'attendees', coalesce((select jsonb_agg(jsonb_build_object('email', a."email", 'name', a."name", 'userId', a."user_id", 'role', a."role", 'response', a."response") order by a."email") from "better_supabase"."calendar_attendees" a where a."event_id" = e."id"), '[]'::jsonb), 'alarms', coalesce((select jsonb_agg(jsonb_build_object('id', l."id", 'method', l."method", 'offsetMinutes', l."offset_minutes", 'related', l."related_to", 'at', l."trigger_at", 'repeat', l."repeat_count", 'repeatMinutes', l."repeat_minutes", 'description', l."description", 'summary', l."summary") order by l."offset_minutes" nulls last, l."trigger_at", l."id") from "better_supabase"."calendar_event_alarms" l where l."event_id" = e."id" and (l."user_id" is null or l."user_id" = (select auth.uid()))), '[]'::jsonb)) from "better_supabase"."calendar_events" e where e."id" = get_calendar_event.event_id
$$;
revoke execute on function "better_supabase"."get_calendar_event"(uuid) from public, anon;
grant execute on function "better_supabase"."get_calendar_event"(uuid) to authenticated, service_role;
-- The tenant's calendars as a keyset page by name.
create or replace function "better_supabase"."page_calendars"(tenant uuid, filters jsonb default '{}')
returns jsonb
language plpgsql
stable
security invoker
set search_path = ''
as $$
#variable_conflict use_variable
declare
v_filters jsonb := coalesce(page_calendars.filters, '{}'::jsonb);
v_search text := case when nullif(trim(v_filters ->> 'search'), '') is null then null
else '%' || replace(replace(replace(trim(v_filters ->> 'search'), '\', '\\'), '%', '\%'), '_', '\_') || '%' end;
v_limit integer := least(greatest(coalesce((v_filters ->> 'limit')::integer, 50), 1), 200);
v_offset integer := greatest(coalesce((v_filters ->> 'offset')::integer, 0), 0);
v_after jsonb := v_filters -> 'after';
v_paged boolean := coalesce((v_filters ->> 'paged')::boolean, false);
v_total_cap integer := least(greatest(coalesce((v_filters ->> 'totalCap')::integer, 10000), 1), 100000);
v_items jsonb;
v_more boolean;
v_next jsonb;
v_total integer;
v_sort text := nullif(v_filters ->> 'sort', '');
v_dir text := nullif(v_filters ->> 'direction', '');
v_facet text := nullif(v_filters ->> 'facet', '');
v_count boolean := coalesce((v_filters ->> 'count')::boolean, false);
v_result jsonb;
begin
if v_filters ? 'after' and v_filters ? 'offset' then
raise exception 'after and offset are two ways to page; pass one' using errcode = '22023', hint = 'CALENDAR_INVALID';
end if;
if v_facet is not null then
if v_facet not in ('ownerType') then
raise exception 'facet is one of ownerType' using errcode = '22023', hint = 'CALENDAR_INVALID';
end if;
with s as (
select (case v_facet when 'ownerType' then (k."owner_type")::text end) as v
from "better_supabase"."calendars" k
where k."tenant_id" = page_calendars.tenant
and (not (v_filters -> 'ownerType' is not null and jsonb_typeof(v_filters -> 'ownerType') <> 'null' and not (jsonb_typeof(v_filters -> 'ownerType') = 'array' and jsonb_array_length(v_filters -> 'ownerType') = 0)) or (k."owner_type")::text = any((case when jsonb_typeof(v_filters -> 'ownerType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'ownerType')) else array[v_filters ->> 'ownerType'] end)::text[]))
and (v_search is null or k."name" ilike v_search)
limit v_total_cap + 1
)
select jsonb_build_object('facet', v_facet, 'values', coalesce(jsonb_agg(jsonb_build_object('value', g.v, 'count', jsonb_build_object('value', g.n, 'exact', g.exact)) order by g.n desc, g.v), '[]'::jsonb))
into v_result
from (
select s.v, count(*) as n, (select count(*) from s) <= v_total_cap as exact
from s where s.v is not null group by s.v order by n desc, s.v limit 100
) g;
return v_result;
end if;
if v_count then
select count(*) into v_total from (
select 1 from "better_supabase"."calendars" k
where k."tenant_id" = page_calendars.tenant
and (not (v_filters -> 'ownerType' is not null and jsonb_typeof(v_filters -> 'ownerType') <> 'null' and not (jsonb_typeof(v_filters -> 'ownerType') = 'array' and jsonb_array_length(v_filters -> 'ownerType') = 0)) or (k."owner_type")::text = any((case when jsonb_typeof(v_filters -> 'ownerType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'ownerType')) else array[v_filters ->> 'ownerType'] end)::text[]))
and (v_search is null or k."name" ilike v_search)
limit v_total_cap + 1
) c;
return jsonb_build_object('value', least(v_total, v_total_cap), 'exact', v_total <= v_total_cap);
end if;
if v_sort is not null and v_sort not in ('name') then
raise exception 'sort is one of name' using errcode = '22023', hint = 'CALENDAR_INVALID';
end if;
if v_dir is not null and v_dir not in ('asc', 'desc') then
raise exception 'direction is asc or desc' using errcode = '22023', hint = 'CALENDAR_INVALID';
end if;
if v_sort is null and coalesce(v_dir, 'asc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'CALENDAR_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', k."id", 'name', k."name", 'tz', k."tz", 'color', k."color", 'ownerType', k."owner_type", 'ownerId', k."owner_id") as j, jsonb_build_array((lower(k."name"))::text, (k."id")::text) as k
from (
select * from "better_supabase"."calendars" k
where k."tenant_id" = page_calendars.tenant
and (not (v_filters -> 'ownerType' is not null and jsonb_typeof(v_filters -> 'ownerType') <> 'null' and not (jsonb_typeof(v_filters -> 'ownerType') = 'array' and jsonb_array_length(v_filters -> 'ownerType') = 0)) or (k."owner_type")::text = any((case when jsonb_typeof(v_filters -> 'ownerType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'ownerType')) else array[v_filters ->> 'ownerType'] end)::text[]))
and (v_search is null or k."name" ilike v_search)
and (v_after is null or (lower(k."name"), k."id") > ((v_after ->> 0)::text, (v_after ->> 1)::uuid))
order by lower(k."name") asc, k."id" asc
limit v_limit + 1 offset v_offset
) k
) p;
elsif v_sort is null and coalesce(v_dir, 'asc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'CALENDAR_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', k."id", 'name', k."name", 'tz', k."tz", 'color', k."color", 'ownerType', k."owner_type", 'ownerId', k."owner_id") as j, jsonb_build_array((lower(k."name"))::text, (k."id")::text) as k
from (
select * from "better_supabase"."calendars" k
where k."tenant_id" = page_calendars.tenant
and (not (v_filters -> 'ownerType' is not null and jsonb_typeof(v_filters -> 'ownerType') <> 'null' and not (jsonb_typeof(v_filters -> 'ownerType') = 'array' and jsonb_array_length(v_filters -> 'ownerType') = 0)) or (k."owner_type")::text = any((case when jsonb_typeof(v_filters -> 'ownerType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'ownerType')) else array[v_filters ->> 'ownerType'] end)::text[]))
and (v_search is null or k."name" ilike v_search)
and (v_after is null or (lower(k."name") <= (v_after ->> 0)::text and ((lower(k."name") < (v_after ->> 0)::text) or (lower(k."name") = (v_after ->> 0)::text and k."id" > (v_after ->> 1)::uuid))))
order by lower(k."name") desc, k."id" asc
limit v_limit + 1 offset v_offset
) k
) p;
elsif v_sort = 'name' and coalesce(v_dir, 'asc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'CALENDAR_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', k."id", 'name', k."name", 'tz', k."tz", 'color', k."color", 'ownerType', k."owner_type", 'ownerId', k."owner_id") as j, jsonb_build_array((lower(k."name"))::text, (k."id")::text) as k
from (
select * from "better_supabase"."calendars" k
where k."tenant_id" = page_calendars.tenant
and (not (v_filters -> 'ownerType' is not null and jsonb_typeof(v_filters -> 'ownerType') <> 'null' and not (jsonb_typeof(v_filters -> 'ownerType') = 'array' and jsonb_array_length(v_filters -> 'ownerType') = 0)) or (k."owner_type")::text = any((case when jsonb_typeof(v_filters -> 'ownerType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'ownerType')) else array[v_filters ->> 'ownerType'] end)::text[]))
and (v_search is null or k."name" ilike v_search)
and (v_after is null or (lower(k."name"), k."id") > ((v_after ->> 0)::text, (v_after ->> 1)::uuid))
order by lower(k."name") asc, k."id" asc
limit v_limit + 1 offset v_offset
) k
) p;
elsif v_sort = 'name' and coalesce(v_dir, 'asc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'CALENDAR_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object('id', k."id", 'name', k."name", 'tz', k."tz", 'color', k."color", 'ownerType', k."owner_type", 'ownerId', k."owner_id") as j, jsonb_build_array((lower(k."name"))::text, (k."id")::text) as k
from (
select * from "better_supabase"."calendars" k
where k."tenant_id" = page_calendars.tenant
and (not (v_filters -> 'ownerType' is not null and jsonb_typeof(v_filters -> 'ownerType') <> 'null' and not (jsonb_typeof(v_filters -> 'ownerType') = 'array' and jsonb_array_length(v_filters -> 'ownerType') = 0)) or (k."owner_type")::text = any((case when jsonb_typeof(v_filters -> 'ownerType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'ownerType')) else array[v_filters ->> 'ownerType'] end)::text[]))
and (v_search is null or k."name" ilike v_search)
and (v_after is null or (lower(k."name") <= (v_after ->> 0)::text and ((lower(k."name") < (v_after ->> 0)::text) or (lower(k."name") = (v_after ->> 0)::text and k."id" > (v_after ->> 1)::uuid))))
order by lower(k."name") desc, k."id" asc
limit v_limit + 1 offset v_offset
) k
) p;
end if;
if not v_paged then
return v_items;
end if;
if coalesce((v_filters ->> 'total')::boolean, false) then
select count(*) into v_total from (
select 1 from "better_supabase"."calendars" k
where k."tenant_id" = page_calendars.tenant
and (not (v_filters -> 'ownerType' is not null and jsonb_typeof(v_filters -> 'ownerType') <> 'null' and not (jsonb_typeof(v_filters -> 'ownerType') = 'array' and jsonb_array_length(v_filters -> 'ownerType') = 0)) or (k."owner_type")::text = any((case when jsonb_typeof(v_filters -> 'ownerType') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'ownerType')) else array[v_filters ->> 'ownerType'] end)::text[]))
and (v_search is null or k."name" ilike v_search)
limit v_total_cap + 1
) c;
end if;
return jsonb_build_object('items', v_items, 'hasMore', v_more, 'nextCursor', case when v_more then v_next end)
|| case when v_total is null then '{}'::jsonb
else jsonb_build_object('total', least(v_total, v_total_cap), 'totalCapped', v_total > v_total_cap) end;
end;
$$;
revoke execute on function "better_supabase"."page_calendars"(uuid, jsonb) from public, anon;
grant execute on function "better_supabase"."page_calendars"(uuid, jsonb) to authenticated, service_role;
-- One calendar the caller reads, or null.
create or replace function "better_supabase"."get_calendar"(calendar_id uuid)
returns jsonb
language sql
stable
security invoker
set search_path = ''
as $$
select jsonb_build_object('id', k."id", 'name', k."name", 'tz', k."tz", 'color', k."color", 'ownerType', k."owner_type", 'ownerId', k."owner_id") from "better_supabase"."calendars" k where k."id" = get_calendar.calendar_id
$$;
revoke execute on function "better_supabase"."get_calendar"(uuid) from public, anon;
grant execute on function "better_supabase"."get_calendar"(uuid) to authenticated, service_role;
-- The calendars of a tenant, the events that can fall in [range_start,
-- range_end), those of [upcoming_start, upcoming_end) when both are given, and
-- the caller's reminders on events: one call instead of one per read. Each part
-- is what list_calendars, list_calendar_events and list_my_reminders return, and
-- it runs as the caller, so the policies decide what it holds.
create or replace function "better_supabase"."calendar_bootstrap"(tenant uuid, range_start timestamptz, range_end timestamptz, upcoming_start timestamptz default null, upcoming_end timestamptz default null)
returns jsonb
language plpgsql
stable
security invoker
set search_path = ''
as $$
declare
v_calendars jsonb := "better_supabase"."list_calendars"(calendar_bootstrap.tenant);
v_ids uuid[];
v_events jsonb := '[]'::jsonb;
v_upcoming jsonb := '[]'::jsonb;
v_reminders jsonb := '[]'::jsonb;
begin
select coalesce(array_agg((c ->> 'id')::uuid), '{}'::uuid[]) into v_ids
from jsonb_array_elements(v_calendars) c;
if cardinality(v_ids) > 0 then
v_events := "better_supabase"."list_calendar_events"(v_ids, calendar_bootstrap.range_start, calendar_bootstrap.range_end);
if calendar_bootstrap.upcoming_start is not null and calendar_bootstrap.upcoming_end is not null then
v_upcoming := "better_supabase"."list_calendar_events"(v_ids, calendar_bootstrap.upcoming_start, calendar_bootstrap.upcoming_end);
end if;
end if;
return jsonb_build_object('calendarList', v_calendars, 'monthEvents', v_events, 'upcomingEvents', v_upcoming, 'myReminders', v_reminders);
end;
$$;
revoke execute on function "better_supabase"."calendar_bootstrap"(uuid, timestamptz, timestamptz, timestamptz, timestamptz) from public, anon;
grant execute on function "better_supabase"."calendar_bootstrap"(uuid, timestamptz, timestamptz, timestamptz, timestamptz) to authenticated, service_role;
create or replace function "better_supabase"."set_calendar_attendee"(event_id uuid, email text, name text, user_id uuid, role text)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_tenant uuid;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
if coalesce(set_calendar_attendee.role, 'required') not in ('chair', 'required', 'optional', 'non-participant') then
raise exception 'role must be chair, required, optional or non-participant' using errcode = '22023', hint = 'CALENDAR_ROLE_INVALID';
end if;
select "tenant_id" into v_tenant from "better_supabase"."calendar_events" where "id" = set_calendar_attendee.event_id;
if not found then
raise exception 'event not found' using errcode = 'P0002', hint = 'CALENDAR_EVENT_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_tenant, 'calendar.write'), false)) then
raise exception 'not allowed to change this calendar' using errcode = '42501', hint = 'CALENDAR_FORBIDDEN';
end if;
insert into "better_supabase"."calendar_attendees" ("tenant_id", "event_id", "email", "name", "user_id", "role")
values (v_tenant, set_calendar_attendee.event_id, lower(set_calendar_attendee.email), set_calendar_attendee.name, set_calendar_attendee.user_id, coalesce(set_calendar_attendee.role, 'required'))
on conflict ("event_id", "email") do update
set "name" = excluded."name", "user_id" = excluded."user_id", "role" = excluded."role";
return true;
end;
$$;
revoke execute on function "better_supabase"."set_calendar_attendee"(uuid, text, text, uuid, text) from public, anon;
grant execute on function "better_supabase"."set_calendar_attendee"(uuid, text, text, uuid, text) to authenticated, service_role;
create or replace function "better_supabase"."delete_calendar_attendee"(event_id uuid, email text)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_tenant uuid;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select "tenant_id" into v_tenant from "better_supabase"."calendar_events" where "id" = delete_calendar_attendee.event_id;
if not found then
raise exception 'event not found' using errcode = 'P0002', hint = 'CALENDAR_EVENT_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_tenant, 'calendar.write'), false)) then
raise exception 'not allowed to change this calendar' using errcode = '42501', hint = 'CALENDAR_FORBIDDEN';
end if;
delete from "better_supabase"."calendar_attendees" where "event_id" = delete_calendar_attendee.event_id and "email" = lower(delete_calendar_attendee.email);
return found;
end;
$$;
revoke execute on function "better_supabase"."delete_calendar_attendee"(uuid, text) from public, anon;
grant execute on function "better_supabase"."delete_calendar_attendee"(uuid, text) to authenticated, service_role;
-- The caller answers an invitation: the attendee row with their user id,
-- or their email when the row has none.
create or replace function "better_supabase"."respond_calendar_event"(event_id uuid, response text)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
if respond_calendar_event.response not in ('needs-action', 'accepted', 'declined', 'tentative') then
raise exception 'response must be needs-action, accepted, declined or tentative' using errcode = '22023', hint = 'CALENDAR_RESPONSE_INVALID';
end if;
update "better_supabase"."calendar_attendees" set "response" = respond_calendar_event.response,
"user_id" = coalesce("user_id", auth.uid())
where "event_id" = respond_calendar_event.event_id
and ("user_id" = auth.uid() or ("user_id" is null and "email" = lower(auth.email())));
if not found then
raise exception 'you are not invited to this event' using errcode = 'P0002', hint = 'CALENDAR_ATTENDEE_NOT_FOUND';
end if;
return true;
end;
$$;
revoke execute on function "better_supabase"."respond_calendar_event"(uuid, text) from public, anon;
grant execute on function "better_supabase"."respond_calendar_event"(uuid, text) to authenticated, service_role;
create or replace function "better_supabase"."calendar_rule_starts"(dtstart timestamptz, tz text, rrule text, range_start timestamptz, range_end timestamptz)
returns setof timestamptz
language plpgsql
stable
set search_path = ''
as $$
begin
if calendar_rule_starts.dtstart is null or calendar_rule_starts.range_start is null or calendar_rule_starts.range_end is null then
return;
end if;
return query select o from better_supabase.rrule_occurrences(calendar_rule_starts.rrule, calendar_rule_starts.dtstart, calendar_rule_starts.tz, tstzrange(calendar_rule_starts.range_start, calendar_rule_starts.range_end, '[)')) o;
end;
$$;
revoke execute on function "better_supabase"."calendar_rule_starts"(timestamptz, text, text, timestamptz, timestamptz) from public, anon;
grant execute on function "better_supabase"."calendar_rule_starts"(timestamptz, text, text, timestamptz, timestamptz) to authenticated, service_role;
create or replace function "better_supabase"."calendar_busy_blocks"(tenant uuid, user_ids uuid[], range_start timestamptz, range_end timestamptz)
returns table (user_id uuid, kind text, starts_at timestamptz, ends_at timestamptz, event_id uuid, declines boolean)
language plpgsql
stable
security definer
set search_path = ''
as $$
#variable_conflict use_column
begin
return query
with sources as (
select m.u as user_id, e."id" as event_id
from unnest(calendar_busy_blocks.user_ids) m (u)
join "better_supabase"."calendars" k on k."tenant_id" = calendar_busy_blocks.tenant and k."owner_type" = 'user' and k."owner_id" = m.u::text
join "better_supabase"."calendar_events" e on e."calendar_id" = k."id"
union
select a."user_id" as user_id, e."id" as event_id
from "better_supabase"."calendar_attendees" a
join "better_supabase"."calendar_events" e on (e."id" = a."event_id" or e."recurrence_id" = a."event_id")
where a."tenant_id" = calendar_busy_blocks.tenant
and a."user_id" = any (calendar_busy_blocks.user_ids)
and a."response" <> 'declined'
and not exists (
select 1 from "better_supabase"."calendar_attendees" d
where d."event_id" = e."id" and d."user_id" = a."user_id" and d."response" = 'declined'
)
),
candidates as (
select distinct s.user_id, e."id" as event_id, e."kind" as kind, e."starts_at" as starts_at, e."ends_at" as ends_at,
e."tz" as tz, e."rrule" as rrule, e."recurrence_id" as recurrence, e."exdates" as exdates, (e."kind" <> 'event' and e."auto_decline") as declines
from sources s
join "better_supabase"."calendar_events" e on e."id" = s.event_id
where e."tenant_id" = calendar_busy_blocks.tenant
and e."status" <> 'cancelled'
and e."transparency" <> 'transparent'
and e."starts_at" < calendar_busy_blocks.range_end
)
select c.user_id, c.kind, c.starts_at, c.ends_at, c.event_id, c.declines
from candidates c
where c.rrule is null and c.starts_at < calendar_busy_blocks.range_end and c.ends_at > calendar_busy_blocks.range_start
union all
select c.user_id, c.kind, s.at, s.at + (c.ends_at - c.starts_at), c.event_id, c.declines
from candidates c
cross join lateral "better_supabase"."calendar_rule_starts"(c.starts_at, c.tz, c.rrule, calendar_busy_blocks.range_start - (c.ends_at - c.starts_at), calendar_busy_blocks.range_end) s (at)
where c.rrule is not null and c.recurrence is null
and c.starts_at < calendar_busy_blocks.range_end
and s.at + (c.ends_at - c.starts_at) > calendar_busy_blocks.range_start
and not (s.at = any (c.exdates))
and not exists (
select 1 from "better_supabase"."calendar_events" o
where o."recurrence_id" = c.event_id and o."original_starts_at" = s.at
);
end;
$$;
revoke execute on function "better_supabase"."calendar_busy_blocks"(uuid, uuid[], timestamptz, timestamptz) from public, anon, authenticated;
grant execute on function "better_supabase"."calendar_busy_blocks"(uuid, uuid[], timestamptz, timestamptz) to service_role;
-- Replaces a member's weekly hours: hours is [{ weekday, start, end }] with
-- ISO weekdays (1 is Monday) and local times in tz. Members set their own;
-- calendar managers set anyone's. An empty list clears them.
create or replace function "better_supabase"."set_calendar_working_hours"(tenant uuid, user_id uuid, tz text, hours jsonb)
returns integer
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_user uuid := coalesce(set_calendar_working_hours.user_id, auth.uid());
v_count integer;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
if v_user is null then
raise exception 'sign in or pass a user id' using errcode = '42501', hint = 'CALENDAR_FORBIDDEN';
end if;
if not (
(auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', set_calendar_working_hours.tenant, 'calendar.manage'), false))
or (coalesce(v_user = auth.uid(), false) and coalesce(better_supabase.can('tenant', set_calendar_working_hours.tenant, 'calendar.read'), false))
) then
raise exception 'not allowed to set these working hours' using errcode = '42501', hint = 'CALENDAR_FORBIDDEN';
end if;
if not exists (select 1 from pg_catalog.pg_timezone_names where name = coalesce(set_calendar_working_hours.tz, 'UTC')) then
raise exception 'tz must be an IANA time zone' using errcode = '22023', hint = 'CALENDAR_TZ_INVALID';
end if;
if jsonb_typeof(coalesce(set_calendar_working_hours.hours, '[]'::jsonb)) <> 'array' or exists (
select 1 from jsonb_array_elements(coalesce(set_calendar_working_hours.hours, '[]'::jsonb)) x
where case
when jsonb_typeof(x) <> 'object'
or coalesce(x ->> 'weekday', '') !~ '^[1-7]$'
or coalesce(x ->> 'start', '') !~ '^([01][0-9]|2[0-3]):[0-5][0-9]$'
or coalesce(x ->> 'end', '') !~ '^(([01][0-9]|2[0-3]):[0-5][0-9]|24:00)$' then true
else x ->> 'end' <> '24:00' and (x ->> 'end')::time <= (x ->> 'start')::time
end
) then
raise exception 'hours must be [{ weekday 1-7, start HH:MM, end HH:MM }] with end after start' using errcode = '22023', hint = 'CALENDAR_HOURS_INVALID';
end if;
delete from "better_supabase"."calendar_working_hours" where "tenant_id" = set_calendar_working_hours.tenant and "user_id" = v_user;
insert into "better_supabase"."calendar_working_hours" ("tenant_id", "user_id", "weekday", "starts_at", "ends_at", "tz")
select distinct set_calendar_working_hours.tenant, v_user, (x ->> 'weekday')::smallint, (x ->> 'start')::time, (x ->> 'end')::time, coalesce(set_calendar_working_hours.tz, 'UTC')
from jsonb_array_elements(coalesce(set_calendar_working_hours.hours, '[]'::jsonb)) x;
get diagnostics v_count = row_count;
return v_count;
end;
$$;
revoke execute on function "better_supabase"."set_calendar_working_hours"(uuid, uuid, text, jsonb) from public, anon;
grant execute on function "better_supabase"."set_calendar_working_hours"(uuid, uuid, text, jsonb) to authenticated, service_role;
-- [{ userId, tz, hours: [{ weekday, start, end }] }] for the members that set
-- working hours. Runs as the caller, so the table's policy decides.
create or replace function "better_supabase"."list_calendar_working_hours"(tenant uuid, user_ids uuid[])
returns jsonb
language sql
stable
security invoker
set search_path = ''
as $$
select coalesce(jsonb_agg(jsonb_build_object('userId', w.user_id, 'tz', w.tz, 'hours', w.hours) order by w.user_id), '[]'::jsonb)
from (
select h."user_id" as user_id, min(h."tz") as tz,
jsonb_agg(jsonb_build_object('weekday', h."weekday", 'start', to_char(h."starts_at", 'HH24:MI'), 'end', case when h."ends_at" = time '24:00' then '24:00' else to_char(h."ends_at", 'HH24:MI') end) order by h."weekday", h."starts_at") as hours
from "better_supabase"."calendar_working_hours" h
where h."tenant_id" = list_calendar_working_hours.tenant
and (list_calendar_working_hours.user_ids is null or h."user_id" = any (list_calendar_working_hours.user_ids))
group by h."user_id"
) w
$$;
revoke execute on function "better_supabase"."list_calendar_working_hours"(uuid, uuid[]) from public, anon;
grant execute on function "better_supabase"."list_calendar_working_hours"(uuid, uuid[]) to authenticated, service_role;
-- Busy, out-of-office and focus-time ranges per member, merged and clipped to
-- [range_start, range_end), with their working hours in it. It answers when
-- people are busy, never why: it returns no titles.
create or replace function "better_supabase"."calendar_free_busy"(tenant uuid, user_ids uuid[], range_start timestamptz, range_end timestamptz)
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_window tstzrange;
begin
if calendar_free_busy.range_start is null or calendar_free_busy.range_end is null
or calendar_free_busy.range_end <= calendar_free_busy.range_start
or calendar_free_busy.range_end - calendar_free_busy.range_start > interval '62 days' then
raise exception 'the range must end after it starts and cover at most 62 days' using errcode = '22023', hint = 'CALENDAR_RANGE_INVALID';
end if;
if coalesce(cardinality(calendar_free_busy.user_ids), 0) not between 1 and 100 then
raise exception 'ask for 1 to 100 members' using errcode = '22023', hint = 'CALENDAR_RANGE_INVALID';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', calendar_free_busy.tenant, 'calendar.read'), false)) then
raise exception 'not allowed to read this calendar' using errcode = '42501', hint = 'CALENDAR_FORBIDDEN';
end if;
v_window := tstzrange(calendar_free_busy.range_start, calendar_free_busy.range_end);
return (
with blocks as (
select b.user_id, b.kind, tstzrange(b.starts_at, b.ends_at) * v_window as during
from "better_supabase"."calendar_busy_blocks"(calendar_free_busy.tenant, calendar_free_busy.user_ids, calendar_free_busy.range_start, calendar_free_busy.range_end) b
),
hours as (
select h."user_id" as user_id, h."tz" as tz,
tstzrange((d.day + h."starts_at") at time zone h."tz", (d.day + h."ends_at") at time zone h."tz") * v_window as during
from "better_supabase"."calendar_working_hours" h
cross join lateral generate_series(
((calendar_free_busy.range_start at time zone h."tz")::date - 1)::timestamp,
((calendar_free_busy.range_end at time zone h."tz")::date)::timestamp,
interval '1 day'
) d (day)
where h."tenant_id" = calendar_free_busy.tenant
and h."user_id" = any (calendar_free_busy.user_ids)
and extract(isodow from d.day) = h."weekday"
),
members as (
select m.user_id, min(m.ord) as ord
from unnest(calendar_free_busy.user_ids) with ordinality m (user_id, ord)
where m.user_id is not null
group by m.user_id
)
select coalesce(jsonb_agg(jsonb_build_object(
'userId', u.user_id,
'busy', coalesce((select jsonb_agg(jsonb_build_object('startsAt', lower(x), 'endsAt', upper(x)) order by lower(x)) from unnest((select range_agg(b.during) from blocks b where b.user_id = u.user_id)) x), '[]'::jsonb),
'outOfOffice', coalesce((select jsonb_agg(jsonb_build_object('startsAt', lower(x), 'endsAt', upper(x)) order by lower(x)) from unnest((select range_agg(b.during) from blocks b where b.user_id = u.user_id and b.kind = 'out_of_office')) x), '[]'::jsonb),
'focusTime', coalesce((select jsonb_agg(jsonb_build_object('startsAt', lower(x), 'endsAt', upper(x)) order by lower(x)) from unnest((select range_agg(b.during) from blocks b where b.user_id = u.user_id and b.kind = 'focus_time')) x), '[]'::jsonb),
'tz', (select min(w.tz) from hours w where w.user_id = u.user_id),
'workingHours', case when exists (select 1 from hours w where w.user_id = u.user_id)
then coalesce((select jsonb_agg(jsonb_build_object('startsAt', lower(x), 'endsAt', upper(x)) order by lower(x)) from unnest((select range_agg(w.during) from hours w where w.user_id = u.user_id)) x), '[]'::jsonb) end
) order by u.ord), '[]'::jsonb)
from members u
);
end;
$$;
revoke execute on function "better_supabase"."calendar_free_busy"(uuid, uuid[], timestamptz, timestamptz) from public, anon;
grant execute on function "better_supabase"."calendar_free_busy"(uuid, uuid[], timestamptz, timestamptz) to authenticated, service_role;
create or replace function "better_supabase"."calendar_attendee_auto_decline"()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
declare
v_event record;
begin
select x."tenant_id" as tenant, x."starts_at" as starts_at, x."ends_at" as ends_at
into v_event
from "better_supabase"."calendar_events" x
where x."id" = new."event_id" and x."rrule" is null and x."kind" = 'event' and x."status" <> 'cancelled';
if found and exists (
select 1 from "better_supabase"."calendar_busy_blocks"(v_event.tenant, array[new."user_id"], v_event.starts_at, v_event.ends_at) b
where b.declines and b.event_id is distinct from new."event_id"
) then
new."response" := 'declined';
end if;
return new;
end;
$$;
revoke execute on function "better_supabase"."calendar_attendee_auto_decline"() from public, anon, authenticated;
create trigger "bsb_calendar_attendees_auto_decline"
before insert or update of "user_id" on "better_supabase"."calendar_attendees"
for each row
when (new."user_id" is not null and new."response" = 'needs-action')
execute function "better_supabase"."calendar_attendee_auto_decline"();
create or replace function "better_supabase"."calendar_block_auto_decline"()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
declare
v_owner uuid;
begin
select case when k."owner_id" ~* '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$' then k."owner_id"::uuid end
into v_owner
from "better_supabase"."calendars" k
where k."id" = new."calendar_id" and k."owner_type" = 'user';
if v_owner is null then
return null;
end if;
update "better_supabase"."calendar_attendees" a set "response" = 'declined'
from "better_supabase"."calendar_events" x
where a."event_id" = x."id"
and a."user_id" = v_owner
and a."response" = 'needs-action'
and x."tenant_id" = new."tenant_id"
and x."id" <> new."id"
and x."rrule" is null
and x."kind" = 'event'
and x."status" <> 'cancelled'
and x."starts_at" < new."ends_at"
and x."ends_at" > new."starts_at";
return null;
end;
$$;
revoke execute on function "better_supabase"."calendar_block_auto_decline"() from public, anon, authenticated;
create trigger "bsb_calendar_events_auto_decline"
after insert or update of "starts_at", "ends_at", "kind", "auto_decline", "status" on "better_supabase"."calendar_events"
for each row
when (new."kind" <> 'event' and new."auto_decline" and new."status" <> 'cancelled' and new."rrule" is null)
execute function "better_supabase"."calendar_block_auto_decline"();
-- Seeds the rows every organization starts with for calendar. Safe to run
-- again: a row whose key exists is kept. It is not an entry point.
create or replace function "better_supabase"."seed_calendar_defaults"(p_tenant uuid, p_variant text default null, p_locale text default 'en')
returns void
language plpgsql
security definer
set search_path = ''
as $$
begin
if p_locale = any (array['nl']) then
insert into "better_supabase"."calendars" ("tenant_id", "name", "tz")
select p_tenant, 'Public holidays (NL)', 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" is not distinct from 'Public holidays (NL)');
end if;
if p_locale = any (array['nl']) then
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2026-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Nieuwjaarsdag', (timestamp '2026-01-01 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2026-01-02 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2026-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2026-04-03', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Goede Vrijdag', (timestamp '2026-04-03 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2026-04-04 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2026-04-03');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2026-04-05', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Eerste Paasdag', (timestamp '2026-04-05 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2026-04-06 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2026-04-05');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2026-04-06', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Tweede Paasdag', (timestamp '2026-04-06 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2026-04-07 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2026-04-06');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2026-04-27', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Koningsdag', (timestamp '2026-04-27 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2026-04-28 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2026-04-27');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2026-05-14', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Hemelvaartsdag', (timestamp '2026-05-14 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2026-05-15 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2026-05-14');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2026-05-24', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Eerste Pinksterdag', (timestamp '2026-05-24 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2026-05-25 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2026-05-24');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2026-05-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Tweede Pinksterdag', (timestamp '2026-05-25 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2026-05-26 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2026-05-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2026-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Eerste Kerstdag', (timestamp '2026-12-25 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2026-12-26 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2026-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2026-12-26', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Tweede Kerstdag', (timestamp '2026-12-26 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2026-12-27 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2026-12-26');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2027-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Nieuwjaarsdag', (timestamp '2027-01-01 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2027-01-02 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2027-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2027-03-26', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Goede Vrijdag', (timestamp '2027-03-26 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2027-03-27 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2027-03-26');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2027-03-28', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Eerste Paasdag', (timestamp '2027-03-28 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2027-03-29 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2027-03-28');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2027-03-29', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Tweede Paasdag', (timestamp '2027-03-29 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2027-03-30 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2027-03-29');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2027-04-27', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Koningsdag', (timestamp '2027-04-27 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2027-04-28 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2027-04-27');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2027-05-06', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Hemelvaartsdag', (timestamp '2027-05-06 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2027-05-07 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2027-05-06');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2027-05-16', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Eerste Pinksterdag', (timestamp '2027-05-16 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2027-05-17 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2027-05-16');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2027-05-17', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Tweede Pinksterdag', (timestamp '2027-05-17 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2027-05-18 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2027-05-17');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2027-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Eerste Kerstdag', (timestamp '2027-12-25 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2027-12-26 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2027-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2027-12-26', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Tweede Kerstdag', (timestamp '2027-12-26 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2027-12-27 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2027-12-26');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2028-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Nieuwjaarsdag', (timestamp '2028-01-01 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2028-01-02 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2028-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2028-04-14', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Goede Vrijdag', (timestamp '2028-04-14 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2028-04-15 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2028-04-14');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2028-04-16', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Eerste Paasdag', (timestamp '2028-04-16 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2028-04-17 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2028-04-16');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2028-04-17', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Tweede Paasdag', (timestamp '2028-04-17 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2028-04-18 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2028-04-17');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2028-04-27', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Koningsdag', (timestamp '2028-04-27 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2028-04-28 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2028-04-27');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2028-05-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Hemelvaartsdag', (timestamp '2028-05-25 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2028-05-26 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2028-05-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2028-06-04', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Eerste Pinksterdag', (timestamp '2028-06-04 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2028-06-05 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2028-06-04');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2028-06-05', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Tweede Pinksterdag', (timestamp '2028-06-05 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2028-06-06 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2028-06-05');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2028-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Eerste Kerstdag', (timestamp '2028-12-25 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2028-12-26 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2028-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2028-12-26', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Tweede Kerstdag', (timestamp '2028-12-26 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2028-12-27 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2028-12-26');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2029-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Nieuwjaarsdag', (timestamp '2029-01-01 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2029-01-02 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2029-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2029-03-30', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Goede Vrijdag', (timestamp '2029-03-30 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2029-03-31 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2029-03-30');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2029-04-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Eerste Paasdag', (timestamp '2029-04-01 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2029-04-02 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2029-04-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2029-04-02', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Tweede Paasdag', (timestamp '2029-04-02 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2029-04-03 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2029-04-02');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2029-04-27', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Koningsdag', (timestamp '2029-04-27 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2029-04-28 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2029-04-27');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2029-05-10', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Hemelvaartsdag', (timestamp '2029-05-10 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2029-05-11 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2029-05-10');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2029-05-20', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Eerste Pinksterdag', (timestamp '2029-05-20 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2029-05-21 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2029-05-20');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2029-05-21', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Tweede Pinksterdag', (timestamp '2029-05-21 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2029-05-22 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2029-05-21');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2029-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Eerste Kerstdag', (timestamp '2029-12-25 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2029-12-26 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2029-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2029-12-26', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Tweede Kerstdag', (timestamp '2029-12-26 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2029-12-27 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2029-12-26');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2030-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Nieuwjaarsdag', (timestamp '2030-01-01 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2030-01-02 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2030-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2030-04-19', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Goede Vrijdag', (timestamp '2030-04-19 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2030-04-20 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2030-04-19');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2030-04-21', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Eerste Paasdag', (timestamp '2030-04-21 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2030-04-22 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2030-04-21');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2030-04-22', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Tweede Paasdag', (timestamp '2030-04-22 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2030-04-23 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2030-04-22');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2030-04-27', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Koningsdag', (timestamp '2030-04-27 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2030-04-28 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2030-04-27');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2030-05-30', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Hemelvaartsdag', (timestamp '2030-05-30 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2030-05-31 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2030-05-30');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2030-06-09', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Eerste Pinksterdag', (timestamp '2030-06-09 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2030-06-10 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2030-06-09');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2030-06-10', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Tweede Pinksterdag', (timestamp '2030-06-10 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2030-06-11 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2030-06-10');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2030-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Eerste Kerstdag', (timestamp '2030-12-25 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2030-12-26 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2030-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-nl-2030-12-26', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (NL)' limit 1), 'Tweede Kerstdag', (timestamp '2030-12-26 00:00' at time zone 'Europe/Amsterdam'), (timestamp '2030-12-27 00:00' at time zone 'Europe/Amsterdam'), true, 'Europe/Amsterdam'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-nl-2030-12-26');
end if;
if p_locale = any (array['de']) then
insert into "better_supabase"."calendars" ("tenant_id", "name", "tz")
select p_tenant, 'Public holidays (DE)', 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" is not distinct from 'Public holidays (DE)');
end if;
if p_locale = any (array['de']) then
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2026-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Neujahr', (timestamp '2026-01-01 00:00' at time zone 'Europe/Berlin'), (timestamp '2026-01-02 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2026-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2026-04-03', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Karfreitag', (timestamp '2026-04-03 00:00' at time zone 'Europe/Berlin'), (timestamp '2026-04-04 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2026-04-03');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2026-04-06', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Ostermontag', (timestamp '2026-04-06 00:00' at time zone 'Europe/Berlin'), (timestamp '2026-04-07 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2026-04-06');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2026-05-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Tag der Arbeit', (timestamp '2026-05-01 00:00' at time zone 'Europe/Berlin'), (timestamp '2026-05-02 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2026-05-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2026-05-14', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Christi Himmelfahrt', (timestamp '2026-05-14 00:00' at time zone 'Europe/Berlin'), (timestamp '2026-05-15 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2026-05-14');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2026-05-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Pfingstmontag', (timestamp '2026-05-25 00:00' at time zone 'Europe/Berlin'), (timestamp '2026-05-26 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2026-05-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2026-10-03', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Tag der Deutschen Einheit', (timestamp '2026-10-03 00:00' at time zone 'Europe/Berlin'), (timestamp '2026-10-04 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2026-10-03');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2026-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), '1. Weihnachtstag', (timestamp '2026-12-25 00:00' at time zone 'Europe/Berlin'), (timestamp '2026-12-26 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2026-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2026-12-26', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), '2. Weihnachtstag', (timestamp '2026-12-26 00:00' at time zone 'Europe/Berlin'), (timestamp '2026-12-27 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2026-12-26');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2027-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Neujahr', (timestamp '2027-01-01 00:00' at time zone 'Europe/Berlin'), (timestamp '2027-01-02 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2027-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2027-03-26', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Karfreitag', (timestamp '2027-03-26 00:00' at time zone 'Europe/Berlin'), (timestamp '2027-03-27 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2027-03-26');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2027-03-29', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Ostermontag', (timestamp '2027-03-29 00:00' at time zone 'Europe/Berlin'), (timestamp '2027-03-30 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2027-03-29');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2027-05-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Tag der Arbeit', (timestamp '2027-05-01 00:00' at time zone 'Europe/Berlin'), (timestamp '2027-05-02 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2027-05-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2027-05-06', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Christi Himmelfahrt', (timestamp '2027-05-06 00:00' at time zone 'Europe/Berlin'), (timestamp '2027-05-07 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2027-05-06');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2027-05-17', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Pfingstmontag', (timestamp '2027-05-17 00:00' at time zone 'Europe/Berlin'), (timestamp '2027-05-18 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2027-05-17');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2027-10-03', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Tag der Deutschen Einheit', (timestamp '2027-10-03 00:00' at time zone 'Europe/Berlin'), (timestamp '2027-10-04 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2027-10-03');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2027-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), '1. Weihnachtstag', (timestamp '2027-12-25 00:00' at time zone 'Europe/Berlin'), (timestamp '2027-12-26 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2027-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2027-12-26', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), '2. Weihnachtstag', (timestamp '2027-12-26 00:00' at time zone 'Europe/Berlin'), (timestamp '2027-12-27 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2027-12-26');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2028-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Neujahr', (timestamp '2028-01-01 00:00' at time zone 'Europe/Berlin'), (timestamp '2028-01-02 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2028-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2028-04-14', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Karfreitag', (timestamp '2028-04-14 00:00' at time zone 'Europe/Berlin'), (timestamp '2028-04-15 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2028-04-14');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2028-04-17', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Ostermontag', (timestamp '2028-04-17 00:00' at time zone 'Europe/Berlin'), (timestamp '2028-04-18 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2028-04-17');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2028-05-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Tag der Arbeit', (timestamp '2028-05-01 00:00' at time zone 'Europe/Berlin'), (timestamp '2028-05-02 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2028-05-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2028-05-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Christi Himmelfahrt', (timestamp '2028-05-25 00:00' at time zone 'Europe/Berlin'), (timestamp '2028-05-26 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2028-05-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2028-06-05', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Pfingstmontag', (timestamp '2028-06-05 00:00' at time zone 'Europe/Berlin'), (timestamp '2028-06-06 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2028-06-05');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2028-10-03', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Tag der Deutschen Einheit', (timestamp '2028-10-03 00:00' at time zone 'Europe/Berlin'), (timestamp '2028-10-04 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2028-10-03');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2028-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), '1. Weihnachtstag', (timestamp '2028-12-25 00:00' at time zone 'Europe/Berlin'), (timestamp '2028-12-26 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2028-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2028-12-26', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), '2. Weihnachtstag', (timestamp '2028-12-26 00:00' at time zone 'Europe/Berlin'), (timestamp '2028-12-27 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2028-12-26');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2029-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Neujahr', (timestamp '2029-01-01 00:00' at time zone 'Europe/Berlin'), (timestamp '2029-01-02 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2029-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2029-03-30', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Karfreitag', (timestamp '2029-03-30 00:00' at time zone 'Europe/Berlin'), (timestamp '2029-03-31 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2029-03-30');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2029-04-02', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Ostermontag', (timestamp '2029-04-02 00:00' at time zone 'Europe/Berlin'), (timestamp '2029-04-03 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2029-04-02');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2029-05-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Tag der Arbeit', (timestamp '2029-05-01 00:00' at time zone 'Europe/Berlin'), (timestamp '2029-05-02 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2029-05-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2029-05-10', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Christi Himmelfahrt', (timestamp '2029-05-10 00:00' at time zone 'Europe/Berlin'), (timestamp '2029-05-11 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2029-05-10');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2029-05-21', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Pfingstmontag', (timestamp '2029-05-21 00:00' at time zone 'Europe/Berlin'), (timestamp '2029-05-22 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2029-05-21');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2029-10-03', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Tag der Deutschen Einheit', (timestamp '2029-10-03 00:00' at time zone 'Europe/Berlin'), (timestamp '2029-10-04 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2029-10-03');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2029-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), '1. Weihnachtstag', (timestamp '2029-12-25 00:00' at time zone 'Europe/Berlin'), (timestamp '2029-12-26 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2029-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2029-12-26', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), '2. Weihnachtstag', (timestamp '2029-12-26 00:00' at time zone 'Europe/Berlin'), (timestamp '2029-12-27 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2029-12-26');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2030-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Neujahr', (timestamp '2030-01-01 00:00' at time zone 'Europe/Berlin'), (timestamp '2030-01-02 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2030-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2030-04-19', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Karfreitag', (timestamp '2030-04-19 00:00' at time zone 'Europe/Berlin'), (timestamp '2030-04-20 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2030-04-19');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2030-04-22', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Ostermontag', (timestamp '2030-04-22 00:00' at time zone 'Europe/Berlin'), (timestamp '2030-04-23 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2030-04-22');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2030-05-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Tag der Arbeit', (timestamp '2030-05-01 00:00' at time zone 'Europe/Berlin'), (timestamp '2030-05-02 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2030-05-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2030-05-30', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Christi Himmelfahrt', (timestamp '2030-05-30 00:00' at time zone 'Europe/Berlin'), (timestamp '2030-05-31 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2030-05-30');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2030-06-10', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Pfingstmontag', (timestamp '2030-06-10 00:00' at time zone 'Europe/Berlin'), (timestamp '2030-06-11 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2030-06-10');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2030-10-03', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), 'Tag der Deutschen Einheit', (timestamp '2030-10-03 00:00' at time zone 'Europe/Berlin'), (timestamp '2030-10-04 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2030-10-03');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2030-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), '1. Weihnachtstag', (timestamp '2030-12-25 00:00' at time zone 'Europe/Berlin'), (timestamp '2030-12-26 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2030-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-de-2030-12-26', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (DE)' limit 1), '2. Weihnachtstag', (timestamp '2030-12-26 00:00' at time zone 'Europe/Berlin'), (timestamp '2030-12-27 00:00' at time zone 'Europe/Berlin'), true, 'Europe/Berlin'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-de-2030-12-26');
end if;
if p_locale = any (array['fr']) then
insert into "better_supabase"."calendars" ("tenant_id", "name", "tz")
select p_tenant, 'Public holidays (FR)', 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" is not distinct from 'Public holidays (FR)');
end if;
if p_locale = any (array['fr']) then
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2026-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Jour de l''An', (timestamp '2026-01-01 00:00' at time zone 'Europe/Paris'), (timestamp '2026-01-02 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2026-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2026-04-06', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Lundi de Pâques', (timestamp '2026-04-06 00:00' at time zone 'Europe/Paris'), (timestamp '2026-04-07 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2026-04-06');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2026-05-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Fête du Travail', (timestamp '2026-05-01 00:00' at time zone 'Europe/Paris'), (timestamp '2026-05-02 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2026-05-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2026-05-08', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Victoire 1945', (timestamp '2026-05-08 00:00' at time zone 'Europe/Paris'), (timestamp '2026-05-09 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2026-05-08');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2026-05-14', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Ascension', (timestamp '2026-05-14 00:00' at time zone 'Europe/Paris'), (timestamp '2026-05-15 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2026-05-14');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2026-05-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Lundi de Pentecôte', (timestamp '2026-05-25 00:00' at time zone 'Europe/Paris'), (timestamp '2026-05-26 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2026-05-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2026-07-14', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Fête nationale', (timestamp '2026-07-14 00:00' at time zone 'Europe/Paris'), (timestamp '2026-07-15 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2026-07-14');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2026-08-15', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Assomption', (timestamp '2026-08-15 00:00' at time zone 'Europe/Paris'), (timestamp '2026-08-16 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2026-08-15');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2026-11-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Toussaint', (timestamp '2026-11-01 00:00' at time zone 'Europe/Paris'), (timestamp '2026-11-02 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2026-11-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2026-11-11', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Armistice 1918', (timestamp '2026-11-11 00:00' at time zone 'Europe/Paris'), (timestamp '2026-11-12 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2026-11-11');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2026-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Noël', (timestamp '2026-12-25 00:00' at time zone 'Europe/Paris'), (timestamp '2026-12-26 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2026-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2027-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Jour de l''An', (timestamp '2027-01-01 00:00' at time zone 'Europe/Paris'), (timestamp '2027-01-02 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2027-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2027-03-29', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Lundi de Pâques', (timestamp '2027-03-29 00:00' at time zone 'Europe/Paris'), (timestamp '2027-03-30 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2027-03-29');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2027-05-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Fête du Travail', (timestamp '2027-05-01 00:00' at time zone 'Europe/Paris'), (timestamp '2027-05-02 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2027-05-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2027-05-08', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Victoire 1945', (timestamp '2027-05-08 00:00' at time zone 'Europe/Paris'), (timestamp '2027-05-09 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2027-05-08');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2027-05-06', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Ascension', (timestamp '2027-05-06 00:00' at time zone 'Europe/Paris'), (timestamp '2027-05-07 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2027-05-06');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2027-05-17', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Lundi de Pentecôte', (timestamp '2027-05-17 00:00' at time zone 'Europe/Paris'), (timestamp '2027-05-18 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2027-05-17');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2027-07-14', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Fête nationale', (timestamp '2027-07-14 00:00' at time zone 'Europe/Paris'), (timestamp '2027-07-15 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2027-07-14');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2027-08-15', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Assomption', (timestamp '2027-08-15 00:00' at time zone 'Europe/Paris'), (timestamp '2027-08-16 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2027-08-15');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2027-11-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Toussaint', (timestamp '2027-11-01 00:00' at time zone 'Europe/Paris'), (timestamp '2027-11-02 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2027-11-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2027-11-11', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Armistice 1918', (timestamp '2027-11-11 00:00' at time zone 'Europe/Paris'), (timestamp '2027-11-12 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2027-11-11');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2027-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Noël', (timestamp '2027-12-25 00:00' at time zone 'Europe/Paris'), (timestamp '2027-12-26 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2027-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2028-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Jour de l''An', (timestamp '2028-01-01 00:00' at time zone 'Europe/Paris'), (timestamp '2028-01-02 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2028-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2028-04-17', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Lundi de Pâques', (timestamp '2028-04-17 00:00' at time zone 'Europe/Paris'), (timestamp '2028-04-18 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2028-04-17');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2028-05-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Fête du Travail', (timestamp '2028-05-01 00:00' at time zone 'Europe/Paris'), (timestamp '2028-05-02 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2028-05-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2028-05-08', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Victoire 1945', (timestamp '2028-05-08 00:00' at time zone 'Europe/Paris'), (timestamp '2028-05-09 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2028-05-08');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2028-05-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Ascension', (timestamp '2028-05-25 00:00' at time zone 'Europe/Paris'), (timestamp '2028-05-26 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2028-05-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2028-06-05', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Lundi de Pentecôte', (timestamp '2028-06-05 00:00' at time zone 'Europe/Paris'), (timestamp '2028-06-06 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2028-06-05');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2028-07-14', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Fête nationale', (timestamp '2028-07-14 00:00' at time zone 'Europe/Paris'), (timestamp '2028-07-15 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2028-07-14');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2028-08-15', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Assomption', (timestamp '2028-08-15 00:00' at time zone 'Europe/Paris'), (timestamp '2028-08-16 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2028-08-15');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2028-11-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Toussaint', (timestamp '2028-11-01 00:00' at time zone 'Europe/Paris'), (timestamp '2028-11-02 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2028-11-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2028-11-11', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Armistice 1918', (timestamp '2028-11-11 00:00' at time zone 'Europe/Paris'), (timestamp '2028-11-12 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2028-11-11');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2028-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Noël', (timestamp '2028-12-25 00:00' at time zone 'Europe/Paris'), (timestamp '2028-12-26 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2028-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2029-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Jour de l''An', (timestamp '2029-01-01 00:00' at time zone 'Europe/Paris'), (timestamp '2029-01-02 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2029-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2029-04-02', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Lundi de Pâques', (timestamp '2029-04-02 00:00' at time zone 'Europe/Paris'), (timestamp '2029-04-03 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2029-04-02');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2029-05-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Fête du Travail', (timestamp '2029-05-01 00:00' at time zone 'Europe/Paris'), (timestamp '2029-05-02 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2029-05-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2029-05-08', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Victoire 1945', (timestamp '2029-05-08 00:00' at time zone 'Europe/Paris'), (timestamp '2029-05-09 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2029-05-08');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2029-05-10', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Ascension', (timestamp '2029-05-10 00:00' at time zone 'Europe/Paris'), (timestamp '2029-05-11 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2029-05-10');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2029-05-21', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Lundi de Pentecôte', (timestamp '2029-05-21 00:00' at time zone 'Europe/Paris'), (timestamp '2029-05-22 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2029-05-21');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2029-07-14', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Fête nationale', (timestamp '2029-07-14 00:00' at time zone 'Europe/Paris'), (timestamp '2029-07-15 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2029-07-14');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2029-08-15', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Assomption', (timestamp '2029-08-15 00:00' at time zone 'Europe/Paris'), (timestamp '2029-08-16 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2029-08-15');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2029-11-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Toussaint', (timestamp '2029-11-01 00:00' at time zone 'Europe/Paris'), (timestamp '2029-11-02 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2029-11-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2029-11-11', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Armistice 1918', (timestamp '2029-11-11 00:00' at time zone 'Europe/Paris'), (timestamp '2029-11-12 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2029-11-11');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2029-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Noël', (timestamp '2029-12-25 00:00' at time zone 'Europe/Paris'), (timestamp '2029-12-26 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2029-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2030-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Jour de l''An', (timestamp '2030-01-01 00:00' at time zone 'Europe/Paris'), (timestamp '2030-01-02 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2030-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2030-04-22', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Lundi de Pâques', (timestamp '2030-04-22 00:00' at time zone 'Europe/Paris'), (timestamp '2030-04-23 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2030-04-22');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2030-05-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Fête du Travail', (timestamp '2030-05-01 00:00' at time zone 'Europe/Paris'), (timestamp '2030-05-02 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2030-05-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2030-05-08', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Victoire 1945', (timestamp '2030-05-08 00:00' at time zone 'Europe/Paris'), (timestamp '2030-05-09 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2030-05-08');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2030-05-30', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Ascension', (timestamp '2030-05-30 00:00' at time zone 'Europe/Paris'), (timestamp '2030-05-31 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2030-05-30');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2030-06-10', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Lundi de Pentecôte', (timestamp '2030-06-10 00:00' at time zone 'Europe/Paris'), (timestamp '2030-06-11 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2030-06-10');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2030-07-14', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Fête nationale', (timestamp '2030-07-14 00:00' at time zone 'Europe/Paris'), (timestamp '2030-07-15 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2030-07-14');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2030-08-15', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Assomption', (timestamp '2030-08-15 00:00' at time zone 'Europe/Paris'), (timestamp '2030-08-16 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2030-08-15');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2030-11-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Toussaint', (timestamp '2030-11-01 00:00' at time zone 'Europe/Paris'), (timestamp '2030-11-02 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2030-11-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2030-11-11', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Armistice 1918', (timestamp '2030-11-11 00:00' at time zone 'Europe/Paris'), (timestamp '2030-11-12 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2030-11-11');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-fr-2030-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (FR)' limit 1), 'Noël', (timestamp '2030-12-25 00:00' at time zone 'Europe/Paris'), (timestamp '2030-12-26 00:00' at time zone 'Europe/Paris'), true, 'Europe/Paris'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-fr-2030-12-25');
end if;
if p_locale = any (array['en-gb']) then
insert into "better_supabase"."calendars" ("tenant_id", "name", "tz")
select p_tenant, 'Public holidays (GB)', 'Europe/London'
where not exists (select 1 from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" is not distinct from 'Public holidays (GB)');
end if;
if p_locale = any (array['en-gb']) then
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2026-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'New Year''s Day', (timestamp '2026-01-01 00:00' at time zone 'Europe/London'), (timestamp '2026-01-02 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2026-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2026-04-03', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Good Friday', (timestamp '2026-04-03 00:00' at time zone 'Europe/London'), (timestamp '2026-04-04 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2026-04-03');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2026-04-06', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Easter Monday', (timestamp '2026-04-06 00:00' at time zone 'Europe/London'), (timestamp '2026-04-07 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2026-04-06');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2026-05-04', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Early May bank holiday', (timestamp '2026-05-04 00:00' at time zone 'Europe/London'), (timestamp '2026-05-05 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2026-05-04');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2026-05-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Spring bank holiday', (timestamp '2026-05-25 00:00' at time zone 'Europe/London'), (timestamp '2026-05-26 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2026-05-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2026-08-31', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Summer bank holiday', (timestamp '2026-08-31 00:00' at time zone 'Europe/London'), (timestamp '2026-09-01 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2026-08-31');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2026-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Christmas Day', (timestamp '2026-12-25 00:00' at time zone 'Europe/London'), (timestamp '2026-12-26 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2026-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2026-12-28', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Boxing Day', (timestamp '2026-12-28 00:00' at time zone 'Europe/London'), (timestamp '2026-12-29 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2026-12-28');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2027-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'New Year''s Day', (timestamp '2027-01-01 00:00' at time zone 'Europe/London'), (timestamp '2027-01-02 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2027-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2027-03-26', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Good Friday', (timestamp '2027-03-26 00:00' at time zone 'Europe/London'), (timestamp '2027-03-27 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2027-03-26');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2027-03-29', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Easter Monday', (timestamp '2027-03-29 00:00' at time zone 'Europe/London'), (timestamp '2027-03-30 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2027-03-29');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2027-05-03', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Early May bank holiday', (timestamp '2027-05-03 00:00' at time zone 'Europe/London'), (timestamp '2027-05-04 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2027-05-03');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2027-05-31', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Spring bank holiday', (timestamp '2027-05-31 00:00' at time zone 'Europe/London'), (timestamp '2027-06-01 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2027-05-31');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2027-08-30', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Summer bank holiday', (timestamp '2027-08-30 00:00' at time zone 'Europe/London'), (timestamp '2027-08-31 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2027-08-30');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2027-12-27', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Christmas Day', (timestamp '2027-12-27 00:00' at time zone 'Europe/London'), (timestamp '2027-12-28 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2027-12-27');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2027-12-28', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Boxing Day', (timestamp '2027-12-28 00:00' at time zone 'Europe/London'), (timestamp '2027-12-29 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2027-12-28');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2028-01-03', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'New Year''s Day', (timestamp '2028-01-03 00:00' at time zone 'Europe/London'), (timestamp '2028-01-04 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2028-01-03');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2028-04-14', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Good Friday', (timestamp '2028-04-14 00:00' at time zone 'Europe/London'), (timestamp '2028-04-15 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2028-04-14');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2028-04-17', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Easter Monday', (timestamp '2028-04-17 00:00' at time zone 'Europe/London'), (timestamp '2028-04-18 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2028-04-17');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2028-05-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Early May bank holiday', (timestamp '2028-05-01 00:00' at time zone 'Europe/London'), (timestamp '2028-05-02 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2028-05-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2028-05-29', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Spring bank holiday', (timestamp '2028-05-29 00:00' at time zone 'Europe/London'), (timestamp '2028-05-30 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2028-05-29');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2028-08-28', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Summer bank holiday', (timestamp '2028-08-28 00:00' at time zone 'Europe/London'), (timestamp '2028-08-29 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2028-08-28');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2028-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Christmas Day', (timestamp '2028-12-25 00:00' at time zone 'Europe/London'), (timestamp '2028-12-26 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2028-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2028-12-26', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Boxing Day', (timestamp '2028-12-26 00:00' at time zone 'Europe/London'), (timestamp '2028-12-27 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2028-12-26');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2029-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'New Year''s Day', (timestamp '2029-01-01 00:00' at time zone 'Europe/London'), (timestamp '2029-01-02 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2029-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2029-03-30', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Good Friday', (timestamp '2029-03-30 00:00' at time zone 'Europe/London'), (timestamp '2029-03-31 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2029-03-30');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2029-04-02', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Easter Monday', (timestamp '2029-04-02 00:00' at time zone 'Europe/London'), (timestamp '2029-04-03 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2029-04-02');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2029-05-07', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Early May bank holiday', (timestamp '2029-05-07 00:00' at time zone 'Europe/London'), (timestamp '2029-05-08 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2029-05-07');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2029-05-28', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Spring bank holiday', (timestamp '2029-05-28 00:00' at time zone 'Europe/London'), (timestamp '2029-05-29 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2029-05-28');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2029-08-27', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Summer bank holiday', (timestamp '2029-08-27 00:00' at time zone 'Europe/London'), (timestamp '2029-08-28 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2029-08-27');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2029-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Christmas Day', (timestamp '2029-12-25 00:00' at time zone 'Europe/London'), (timestamp '2029-12-26 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2029-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2029-12-26', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Boxing Day', (timestamp '2029-12-26 00:00' at time zone 'Europe/London'), (timestamp '2029-12-27 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2029-12-26');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2030-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'New Year''s Day', (timestamp '2030-01-01 00:00' at time zone 'Europe/London'), (timestamp '2030-01-02 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2030-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2030-04-19', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Good Friday', (timestamp '2030-04-19 00:00' at time zone 'Europe/London'), (timestamp '2030-04-20 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2030-04-19');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2030-04-22', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Easter Monday', (timestamp '2030-04-22 00:00' at time zone 'Europe/London'), (timestamp '2030-04-23 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2030-04-22');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2030-05-06', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Early May bank holiday', (timestamp '2030-05-06 00:00' at time zone 'Europe/London'), (timestamp '2030-05-07 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2030-05-06');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2030-05-27', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Spring bank holiday', (timestamp '2030-05-27 00:00' at time zone 'Europe/London'), (timestamp '2030-05-28 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2030-05-27');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2030-08-26', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Summer bank holiday', (timestamp '2030-08-26 00:00' at time zone 'Europe/London'), (timestamp '2030-08-27 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2030-08-26');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2030-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Christmas Day', (timestamp '2030-12-25 00:00' at time zone 'Europe/London'), (timestamp '2030-12-26 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2030-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-gb-2030-12-26', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (GB)' limit 1), 'Boxing Day', (timestamp '2030-12-26 00:00' at time zone 'Europe/London'), (timestamp '2030-12-27 00:00' at time zone 'Europe/London'), true, 'Europe/London'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-gb-2030-12-26');
end if;
if p_locale = any (array['en-us']) then
insert into "better_supabase"."calendars" ("tenant_id", "name", "tz")
select p_tenant, 'Public holidays (US)', 'America/New_York'
where not exists (select 1 from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" is not distinct from 'Public holidays (US)');
end if;
if p_locale = any (array['en-us']) then
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2026-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'New Year''s Day', (timestamp '2026-01-01 00:00' at time zone 'America/New_York'), (timestamp '2026-01-02 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2026-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2026-01-19', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Martin Luther King Jr. Day', (timestamp '2026-01-19 00:00' at time zone 'America/New_York'), (timestamp '2026-01-20 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2026-01-19');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2026-02-16', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Presidents'' Day', (timestamp '2026-02-16 00:00' at time zone 'America/New_York'), (timestamp '2026-02-17 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2026-02-16');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2026-05-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Memorial Day', (timestamp '2026-05-25 00:00' at time zone 'America/New_York'), (timestamp '2026-05-26 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2026-05-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2026-06-19', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Juneteenth', (timestamp '2026-06-19 00:00' at time zone 'America/New_York'), (timestamp '2026-06-20 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2026-06-19');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2026-07-04', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Independence Day', (timestamp '2026-07-04 00:00' at time zone 'America/New_York'), (timestamp '2026-07-05 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2026-07-04');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2026-09-07', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Labor Day', (timestamp '2026-09-07 00:00' at time zone 'America/New_York'), (timestamp '2026-09-08 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2026-09-07');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2026-10-12', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Columbus Day', (timestamp '2026-10-12 00:00' at time zone 'America/New_York'), (timestamp '2026-10-13 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2026-10-12');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2026-11-11', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Veterans Day', (timestamp '2026-11-11 00:00' at time zone 'America/New_York'), (timestamp '2026-11-12 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2026-11-11');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2026-11-26', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Thanksgiving Day', (timestamp '2026-11-26 00:00' at time zone 'America/New_York'), (timestamp '2026-11-27 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2026-11-26');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2026-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Christmas Day', (timestamp '2026-12-25 00:00' at time zone 'America/New_York'), (timestamp '2026-12-26 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2026-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2027-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'New Year''s Day', (timestamp '2027-01-01 00:00' at time zone 'America/New_York'), (timestamp '2027-01-02 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2027-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2027-01-18', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Martin Luther King Jr. Day', (timestamp '2027-01-18 00:00' at time zone 'America/New_York'), (timestamp '2027-01-19 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2027-01-18');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2027-02-15', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Presidents'' Day', (timestamp '2027-02-15 00:00' at time zone 'America/New_York'), (timestamp '2027-02-16 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2027-02-15');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2027-05-31', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Memorial Day', (timestamp '2027-05-31 00:00' at time zone 'America/New_York'), (timestamp '2027-06-01 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2027-05-31');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2027-06-19', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Juneteenth', (timestamp '2027-06-19 00:00' at time zone 'America/New_York'), (timestamp '2027-06-20 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2027-06-19');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2027-07-04', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Independence Day', (timestamp '2027-07-04 00:00' at time zone 'America/New_York'), (timestamp '2027-07-05 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2027-07-04');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2027-09-06', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Labor Day', (timestamp '2027-09-06 00:00' at time zone 'America/New_York'), (timestamp '2027-09-07 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2027-09-06');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2027-10-11', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Columbus Day', (timestamp '2027-10-11 00:00' at time zone 'America/New_York'), (timestamp '2027-10-12 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2027-10-11');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2027-11-11', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Veterans Day', (timestamp '2027-11-11 00:00' at time zone 'America/New_York'), (timestamp '2027-11-12 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2027-11-11');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2027-11-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Thanksgiving Day', (timestamp '2027-11-25 00:00' at time zone 'America/New_York'), (timestamp '2027-11-26 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2027-11-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2027-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Christmas Day', (timestamp '2027-12-25 00:00' at time zone 'America/New_York'), (timestamp '2027-12-26 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2027-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2028-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'New Year''s Day', (timestamp '2028-01-01 00:00' at time zone 'America/New_York'), (timestamp '2028-01-02 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2028-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2028-01-17', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Martin Luther King Jr. Day', (timestamp '2028-01-17 00:00' at time zone 'America/New_York'), (timestamp '2028-01-18 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2028-01-17');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2028-02-21', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Presidents'' Day', (timestamp '2028-02-21 00:00' at time zone 'America/New_York'), (timestamp '2028-02-22 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2028-02-21');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2028-05-29', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Memorial Day', (timestamp '2028-05-29 00:00' at time zone 'America/New_York'), (timestamp '2028-05-30 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2028-05-29');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2028-06-19', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Juneteenth', (timestamp '2028-06-19 00:00' at time zone 'America/New_York'), (timestamp '2028-06-20 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2028-06-19');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2028-07-04', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Independence Day', (timestamp '2028-07-04 00:00' at time zone 'America/New_York'), (timestamp '2028-07-05 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2028-07-04');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2028-09-04', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Labor Day', (timestamp '2028-09-04 00:00' at time zone 'America/New_York'), (timestamp '2028-09-05 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2028-09-04');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2028-10-09', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Columbus Day', (timestamp '2028-10-09 00:00' at time zone 'America/New_York'), (timestamp '2028-10-10 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2028-10-09');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2028-11-11', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Veterans Day', (timestamp '2028-11-11 00:00' at time zone 'America/New_York'), (timestamp '2028-11-12 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2028-11-11');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2028-11-23', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Thanksgiving Day', (timestamp '2028-11-23 00:00' at time zone 'America/New_York'), (timestamp '2028-11-24 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2028-11-23');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2028-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Christmas Day', (timestamp '2028-12-25 00:00' at time zone 'America/New_York'), (timestamp '2028-12-26 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2028-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2029-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'New Year''s Day', (timestamp '2029-01-01 00:00' at time zone 'America/New_York'), (timestamp '2029-01-02 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2029-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2029-01-15', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Martin Luther King Jr. Day', (timestamp '2029-01-15 00:00' at time zone 'America/New_York'), (timestamp '2029-01-16 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2029-01-15');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2029-02-19', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Presidents'' Day', (timestamp '2029-02-19 00:00' at time zone 'America/New_York'), (timestamp '2029-02-20 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2029-02-19');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2029-05-28', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Memorial Day', (timestamp '2029-05-28 00:00' at time zone 'America/New_York'), (timestamp '2029-05-29 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2029-05-28');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2029-06-19', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Juneteenth', (timestamp '2029-06-19 00:00' at time zone 'America/New_York'), (timestamp '2029-06-20 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2029-06-19');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2029-07-04', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Independence Day', (timestamp '2029-07-04 00:00' at time zone 'America/New_York'), (timestamp '2029-07-05 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2029-07-04');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2029-09-03', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Labor Day', (timestamp '2029-09-03 00:00' at time zone 'America/New_York'), (timestamp '2029-09-04 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2029-09-03');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2029-10-08', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Columbus Day', (timestamp '2029-10-08 00:00' at time zone 'America/New_York'), (timestamp '2029-10-09 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2029-10-08');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2029-11-11', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Veterans Day', (timestamp '2029-11-11 00:00' at time zone 'America/New_York'), (timestamp '2029-11-12 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2029-11-11');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2029-11-22', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Thanksgiving Day', (timestamp '2029-11-22 00:00' at time zone 'America/New_York'), (timestamp '2029-11-23 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2029-11-22');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2029-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Christmas Day', (timestamp '2029-12-25 00:00' at time zone 'America/New_York'), (timestamp '2029-12-26 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2029-12-25');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2030-01-01', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'New Year''s Day', (timestamp '2030-01-01 00:00' at time zone 'America/New_York'), (timestamp '2030-01-02 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2030-01-01');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2030-01-21', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Martin Luther King Jr. Day', (timestamp '2030-01-21 00:00' at time zone 'America/New_York'), (timestamp '2030-01-22 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2030-01-21');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2030-02-18', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Presidents'' Day', (timestamp '2030-02-18 00:00' at time zone 'America/New_York'), (timestamp '2030-02-19 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2030-02-18');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2030-05-27', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Memorial Day', (timestamp '2030-05-27 00:00' at time zone 'America/New_York'), (timestamp '2030-05-28 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2030-05-27');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2030-06-19', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Juneteenth', (timestamp '2030-06-19 00:00' at time zone 'America/New_York'), (timestamp '2030-06-20 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2030-06-19');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2030-07-04', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Independence Day', (timestamp '2030-07-04 00:00' at time zone 'America/New_York'), (timestamp '2030-07-05 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2030-07-04');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2030-09-02', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Labor Day', (timestamp '2030-09-02 00:00' at time zone 'America/New_York'), (timestamp '2030-09-03 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2030-09-02');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2030-10-14', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Columbus Day', (timestamp '2030-10-14 00:00' at time zone 'America/New_York'), (timestamp '2030-10-15 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2030-10-14');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2030-11-11', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Veterans Day', (timestamp '2030-11-11 00:00' at time zone 'America/New_York'), (timestamp '2030-11-12 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2030-11-11');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2030-11-28', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Thanksgiving Day', (timestamp '2030-11-28 00:00' at time zone 'America/New_York'), (timestamp '2030-11-29 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2030-11-28');
insert into "better_supabase"."calendar_events" ("tenant_id", "uid", "calendar_id", "title", "starts_at", "ends_at", "all_day", "tz")
select p_tenant, 'holiday-en-us-2030-12-25', (select x."id" from "better_supabase"."calendars" x where x."tenant_id" = p_tenant and x."name" = 'Public holidays (US)' limit 1), 'Christmas Day', (timestamp '2030-12-25 00:00' at time zone 'America/New_York'), (timestamp '2030-12-26 00:00' at time zone 'America/New_York'), true, 'America/New_York'
where not exists (select 1 from "better_supabase"."calendar_events" x where x."tenant_id" = p_tenant and x."uid" is not distinct from 'holiday-en-us-2030-12-25');
end if;
end;
$$;
revoke execute on function "better_supabase"."seed_calendar_defaults"(uuid, text, text) from public, anon, authenticated;
grant execute on function "better_supabase"."seed_calendar_defaults"(uuid, text, text) to service_role;
-- Tenant isolation (ADR 0099): permissive policies combine with or, so one
-- the app adds would widen the block's. These restrictive policies keep each
-- table to the caller's organizations and what the block's policies grant.
create policy bsb_tenant_scope_select on "better_supabase"."calendars" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("tenant_id" in (select better_supabase.tenant_ids_with('calendar.read')))
);
create policy bsb_tenant_scope_insert on "better_supabase"."calendars" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."calendars" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."calendars" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_select on "better_supabase"."calendar_events" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("tenant_id" in (select better_supabase.tenant_ids_with('calendar.read')))
);
create policy bsb_tenant_scope_insert on "better_supabase"."calendar_events" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."calendar_events" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."calendar_events" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_select on "better_supabase"."calendar_attendees" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("tenant_id" in (select better_supabase.tenant_ids_with('calendar.read')) or "user_id" = (select auth.uid()))
);
create policy bsb_tenant_scope_insert on "better_supabase"."calendar_attendees" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."calendar_attendees" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."calendar_attendees" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_select on "better_supabase"."calendar_event_alarms" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("tenant_id" in (select better_supabase.tenant_ids_with('calendar.read')) and ("user_id" is null or "user_id" = (select auth.uid())))
);
create policy bsb_tenant_scope_insert on "better_supabase"."calendar_event_alarms" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."calendar_event_alarms" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."calendar_event_alarms" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_select on "better_supabase"."calendar_working_hours" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("tenant_id" in (select better_supabase.tenant_ids_with('calendar.read')) or "user_id" = (select auth.uid()))
);
create policy bsb_tenant_scope_insert on "better_supabase"."calendar_working_hours" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."calendar_working_hours" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."calendar_working_hours" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
Client
The React hooks are the registry item @better-supabase/use-calendar, which shadcn copies into your app. They return data and actions and render no UI, so you build the interface from your own components.
Events
With the outbox installed, the module writes these in the same transaction as the change.
- calendar.created
- calendar_event.created
- calendar_event.purged
- calendar_event.updated
Dependencies
- Required: the install command adds it too
- Works with: optional
Recipes
More in Scheduling
Remindersreminders Recurrence rules and reminders. Expansion follows RFC 5545 in TypeScript. Delivery is a job that records reminder.sent once per occurrence. Bookingsbookings Bookable resources with weekly hours and exceptions, services with durations and buffers, and bookings that a gist exclusion constraint (btree_gist) keeps from overlapping per seat. Holds expire; recurring series book every occurrence; a share link opens public booking; confirmed bookings mirror into the calendar and reminders.React hooks Passespasses Admission passes on QR codes and NFC tags, signed with the signing-keys block: a ticket for a booking or a pass with a number of uses for a resource, a log of every scan, class credits that pay for bookings and come back on an early cancellation, and drop-ins counted against the resource's capacity with its bookings. Recurrencerecurrence An RFC 5545 expander in SQL: rrule_occurrences lists the instants a rule starts in a range and rrule_next gives the first after an instant, matching the TypeScript expander in better-supabase/recurrence.