alter table if exists keyword_process
  add column if not exists task_id text;

alter table if exists best_match_images
  add column if not exists task_id text;

alter table if exists extended_best_matches
  add column if not exists task_id text;

alter table if exists image_search_results
  add column if not exists task_id text;

alter table if exists keyword_selections
  add column if not exists task_id text;

alter table if exists extended_best_match_records
  add column if not exists task_id text;

alter table if exists search_rounds
  alter column task_id set default '';

alter table if exists final_results
  alter column task_id set default '';

alter table if exists final_result_products
  alter column task_id set default '';

update keyword_process set task_id = '' where task_id is null;
update search_rounds set task_id = '' where task_id is null;
update best_match_images set task_id = '' where task_id is null;
update extended_best_matches set task_id = '' where task_id is null;
update image_search_results set task_id = '' where task_id is null;
update keyword_selections set task_id = '' where task_id is null;
update final_results set task_id = '' where task_id is null;
update final_result_products set task_id = '' where task_id is null;
update extended_best_match_records set task_id = '' where task_id is null;

alter table if exists keyword_process
  alter column task_id set default '',
  alter column task_id set not null;

alter table if exists search_rounds
  alter column task_id set not null;

alter table if exists best_match_images
  alter column task_id set default '',
  alter column task_id set not null;

alter table if exists extended_best_matches
  alter column task_id set default '',
  alter column task_id set not null;

alter table if exists image_search_results
  alter column task_id set default '',
  alter column task_id set not null;

alter table if exists keyword_selections
  alter column task_id set default '',
  alter column task_id set not null;

alter table if exists final_results
  alter column task_id set not null;

alter table if exists final_result_products
  alter column task_id set not null;

alter table if exists extended_best_match_records
  alter column task_id set default '',
  alter column task_id set not null;

alter table if exists keyword_process
  drop constraint if exists keyword_process_batch_id_material_id_key,
  drop constraint if exists keyword_process_task_batch_material_key,
  add constraint keyword_process_task_batch_material_key unique (task_id, batch_id, material_id);

alter table if exists search_rounds
  drop constraint if exists search_rounds_batch_id_material_id_search_round_run_type_key,
  drop constraint if exists search_rounds_task_batch_material_round_type_key,
  add constraint search_rounds_task_batch_material_round_type_key unique (task_id, batch_id, material_id, search_round, run_type);

alter table if exists best_match_images
  drop constraint if exists best_match_images_batch_id_material_id_key,
  drop constraint if exists best_match_images_task_batch_material_key,
  add constraint best_match_images_task_batch_material_key unique (task_id, batch_id, material_id);

alter table if exists extended_best_matches
  drop constraint if exists extended_best_matches_batch_id_material_id_key,
  drop constraint if exists extended_best_matches_task_batch_material_key,
  add constraint extended_best_matches_task_batch_material_key unique (task_id, batch_id, material_id);

alter table if exists image_search_results
  drop constraint if exists image_search_results_batch_id_material_id_key,
  drop constraint if exists image_search_results_task_batch_material_key,
  add constraint image_search_results_task_batch_material_key unique (task_id, batch_id, material_id);

alter table if exists keyword_selections
  drop constraint if exists keyword_selections_batch_id_material_id_key,
  drop constraint if exists keyword_selections_task_batch_material_key,
  add constraint keyword_selections_task_batch_material_key unique (task_id, batch_id, material_id);

alter table if exists final_results
  drop constraint if exists final_results_batch_id_material_id_key,
  drop constraint if exists final_results_task_batch_material_key,
  add constraint final_results_task_batch_material_key unique (task_id, batch_id, material_id);

create index if not exists idx_keyword_process_task_id on keyword_process(task_id);
create index if not exists idx_best_match_images_task_id on best_match_images(task_id);
create index if not exists idx_extended_best_matches_task_id on extended_best_matches(task_id);
create index if not exists idx_image_search_results_task_id on image_search_results(task_id);
create index if not exists idx_keyword_selections_task_id on keyword_selections(task_id);
create index if not exists idx_extended_best_match_records_task_id on extended_best_match_records(task_id);
create index if not exists idx_final_result_products_task_batch_material on final_result_products(task_id, batch_id, material_id);
create index if not exists idx_extended_best_match_records_task_batch_material on extended_best_match_records(task_id, batch_id, material_id);
