-- Revert numerus:contact_tax_details from pg

begin;

set search_path to numerus, public;

alter table contact
  drop constraint name_not_empty
, add column country_code country_code
, add column postal_code text
, add column province text
, add column city text
, add column address text
, add column vatin vatin
, add column business_name text constraint business_name_not_empty check(length(trim(business_name)) > 1)
;

alter table contact
  rename column name to trade_name
;

update contact
set business_name = tax.business_name
  , vatin = tax.vatin
  , address = tax.address
  , city = tax.city
  , province = tax.province
  , postal_code = tax.postal_code
  , country_code = tax.country_code
from contact_tax_details as tax
where tax.contact_id = contact.contact_id
;

alter table contact
  alter column business_name set not null
, alter column vatin set not null
, alter column address set not null
, alter column city set not null
, alter column province set not null
, alter column postal_code set not null
, alter column country_code set not null
;

update contact
set trade_name = ''
where trade_name = business_name
;

drop table if exists contact_tax_details;

commit;
