RLS not applied to the view after migration
I have a RLS on the root table from which a view is created, recently I updated the view via a migration by doing a
The issue is that after that, the view seems skipping the RLS rules while still working on the table.
The migration I ran:
While the RLS applied to
drop view ...drop view ... and creating it again.The issue is that after that, the view seems skipping the RLS rules while still working on the table.
The migration I ran:
-- 1) Add `updated_at` to shops (backfilled with default for existing rows)
alter table public.shops
add column if not exists updated_at timestamp with time zone
not null default (now() at time zone 'gmt-2'::text);
-- 2) (Re)create the updated_at trigger for shops
drop trigger if exists zz_handle_updated_at on public.shops;
create trigger zz_handle_updated_at
before update on public.shops
for each row
execute function moddatetime('updated_at');
-- 3) Drop the old view before recreating
drop view if exists public.shops_view;
-- 4) Extend the view with phone, computed address, created_at, updated_at
create or replace view public.shops_view as
select
s.id as shop_id,
s.code as shop_code,
s.name as shop_name,
-- new: expose phone
s.phone as shop_phone,
-- new: computed, gracefully joins address parts that exist
nullif(
trim(both ', ' from concat_ws(
', ',
nullif(s.address, ''),
nullif(concat_ws(' ', nullif(s.postal_code, ''), nullif(s.city, '')), ''),
nullif(s.province, '')
)),
''
) as shop_address,
c.id as company_id,
c.name as company_name,
c.created_by as company_created_by,
-- new: timestamps so you can sort by recency
s.created_at,
s.updated_at
from
public.shops s
join public.companies c on s.company = c.id;-- 1) Add `updated_at` to shops (backfilled with default for existing rows)
alter table public.shops
add column if not exists updated_at timestamp with time zone
not null default (now() at time zone 'gmt-2'::text);
-- 2) (Re)create the updated_at trigger for shops
drop trigger if exists zz_handle_updated_at on public.shops;
create trigger zz_handle_updated_at
before update on public.shops
for each row
execute function moddatetime('updated_at');
-- 3) Drop the old view before recreating
drop view if exists public.shops_view;
-- 4) Extend the view with phone, computed address, created_at, updated_at
create or replace view public.shops_view as
select
s.id as shop_id,
s.code as shop_code,
s.name as shop_name,
-- new: expose phone
s.phone as shop_phone,
-- new: computed, gracefully joins address parts that exist
nullif(
trim(both ', ' from concat_ws(
', ',
nullif(s.address, ''),
nullif(concat_ws(' ', nullif(s.postal_code, ''), nullif(s.city, '')), ''),
nullif(s.province, '')
)),
''
) as shop_address,
c.id as company_id,
c.name as company_name,
c.created_by as company_created_by,
-- new: timestamps so you can sort by recency
s.created_at,
s.updated_at
from
public.shops s
join public.companies c on s.company = c.id;While the RLS applied to
shopsshops is working as I tested it properly.