-- Deploy numerus:contact_phone to pg
-- requires: roles
-- requires: schema_numerus
-- requires: extension_pg_libphonenumber

begin;

set search_path to numerus, public;

create table contact_phone (
	contact_id integer primary key references contact,
	phone packed_phone_number not null
);

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

alter table contact_phone enable row level security;

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

insert into contact_phone
select contact_id, phone
from contact;

alter table contact
  drop column phone
;

commit;
