-- Deploy camper:page to pg
-- requires: roles
-- requires: schema_camper
-- requires: company
-- requires: user_profile

begin;

set search_path to camper, public;

create table page (
	page_id serial primary key,
	company_id integer not null references company,
	slug uuid not null unique default gen_random_uuid(),
	title text not null constraint title_not_empty check(length(trim(title)) > 0),
	content xml not null default ''
);

grant select on table page to guest;
grant select on table page to employee;
grant select, insert, update, delete on table page to admin;

grant usage on sequence page_page_id_seq to admin;

alter table page enable row level security;

create policy guest_ok
on page
for select
using (true)
;

create policy insert_to_company
on page
for insert
with check (
	company_id in (select company_id from user_profile)
)
;

create policy update_company
on page
for update
using (
	company_id in (select company_id from user_profile)
)
;

create policy delete_from_company
on page
for delete
using (
	company_id in (select company_id from user_profile)
)
;

commit;
