Fixed migrations

This commit is contained in:
2026-03-01 19:12:13 +08:00
parent 830c99a3ff
commit b9153a070f
3 changed files with 46 additions and 42 deletions
@@ -1,19 +1,22 @@
-- add device_id column to fcm_tokens and a unique constraint on (user_id, device_id)
-- Add device_id column to fcm_tokens
alter table if exists public.fcm_tokens
add column if not exists device_id text;
-- create a unique index so upsert can update the row for the same device
-- Create a unique index so upsert can update the row for the same device
create unique index if not exists fcm_tokens_user_device_idx
on public.fcm_tokens(user_id, device_id);
-- ensure device_id is protected by RLS policies: allow users to insert/update/delete their device rows
-- (these policies assume RLS is already enabled on the table)
create policy if not exists "Allow users insert their device tokens" on public.fcm_tokens
-- Drop policies if they exist to prevent duplication errors
drop policy if exists "Allow users insert their device tokens" on public.fcm_tokens;
drop policy if exists "Allow users delete their device tokens" on public.fcm_tokens;
drop policy if exists "Allow users update their device tokens" on public.fcm_tokens;
-- Recreate the RLS policies
create policy "Allow users insert their device tokens" on public.fcm_tokens
for insert with check (auth.uid() = user_id);
create policy if not exists "Allow users delete their device tokens" on public.fcm_tokens
create policy "Allow users delete their device tokens" on public.fcm_tokens
for delete using (auth.uid() = user_id);
create policy if not exists "Allow users update their device tokens" on public.fcm_tokens
for update using (auth.uid() = user_id) with check (auth.uid() = user_id);
create policy "Allow users update their device tokens" on public.fcm_tokens
for update using (auth.uid() = user_id) with check (auth.uid() = user_id);