-- Deploy numerus:tag to pg
-- requires: schema_numerus
-- requires: tag_name

begin;

set search_path to numerus, public;

create table tag (
	tag_id serial primary key,
	company_id integer not null references company,
	name tag_name not null,
	unique (company_id, name)
);

grant select, insert, update, delete on table tag to invoicer;
grant select, insert, update, delete on table tag to admin;

grant usage on sequence tag_tag_id_seq to invoicer;
grant usage on sequence tag_tag_id_seq to admin;

alter table tag enable row level security;

create policy company_policy
on tag
using (
	exists(
		select 1
		from company_user
		join user_profile using (user_id)
		where company_user.company_id = tag.company_id
	)
);

commit;
