-- P5-6245: add market id to product blacklist table

alter table product_blacklist
    add column market_id integer;

alter table product_blacklist
    add constraint market_fkey
    foreign key (market_id)
    references markets(id) match simple;

update product_blacklist SET market_id = (
    select markets.id from markets
    join locales on markets.locale_id = locales.id
    where locales.value = product_blacklist.language_id || '_' || product_blacklist.territory_id
);

alter table product_blacklist
    alter column market_id set not null;

create unique index on product_blacklist (product_id, group_id, market_id);
