-- Deploy numerus:contact_email to pg
-- requires: roles
-- requires: schema_numerus
-- requires: email
-- requires: contact

begin;

set search_path to numerus, public;

create table contact_email (
	contact_id integer primary key references contact,
	email email not null
);

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

alter table contact_email enable row level security;

create policy company_policy
on contact_email
using (
	exists(
		select 1
		from contact
		where contact.contact_id = contact_email.contact_id
	)
);

insert into contact_email
select contact_id, email
from contact;

alter table contact
  drop column email
;

commit;
