-- Additional encrypted customer fields for organization schemas.

alter table {schema}.customers
    add column if not exists details_ciphertext bytea,
    add column if not exists details_nonce bytea,
    add column if not exists details_key_id text;

alter table {schema}.customers
    drop constraint if exists customers_details_encryption_complete;

alter table {schema}.customers
    add constraint customers_details_encryption_complete check (
        (
            details_ciphertext is null
            and details_nonce is null
            and details_key_id is null
        )
        or (
            details_ciphertext is not null
            and details_nonce is not null
            and details_key_id is not null
        )
    );

alter table {schema}.suppliers
    add column if not exists details_ciphertext bytea,
    add column if not exists details_nonce bytea,
    add column if not exists details_key_id text;

alter table {schema}.suppliers
    drop constraint if exists suppliers_details_encryption_complete;

alter table {schema}.suppliers
    add constraint suppliers_details_encryption_complete check (
        (
            details_ciphertext is null
            and details_nonce is null
            and details_key_id is null
        )
        or (
            details_ciphertext is not null
            and details_nonce is not null
            and details_key_id is not null
        )
    );
