-- Alembic Revision: 2f2346b8b7cb

/*
  ***** SPECIAL ROLLBACK INSTRUCTIONS *****
  1. Re-add old column and index.
  2. Update old column.
  3. Deploy code.
  4. Re-add old foreign key.
  5. Drop new table.
 */

alter table menu_combo_items
 add column menu_combo_item_group_id varchar(64) collate utf8_bin not null after id,
 add key idx_menu_combo_item_menu_combo_item_group_id (_cache_id, menu_combo_item_group_id),
 algorithm=inplace, lock=none;

-- Perform the update and add the constraint inside a lock to ensure the table state doesn't violate the constraint when it is applied.
lock tables menu_combo_items write,  menu_combo_item_groups_menu_combo_items write;

-- Could match more than one row if a combo has multiple items with the same id. The item will end up on only one of the groups.
update menu_combo_items
  join menu_combo_item_groups_menu_combo_items
    on menu_combo_items._cache_id = menu_combo_item_groups_menu_combo_items._cache_id
   and menu_combo_items.id = menu_combo_item_groups_menu_combo_items.menu_combo_item_id
   set menu_combo_items.menu_combo_item_group_id = menu_combo_item_groups_menu_combo_items.menu_combo_item_group_id;
   
alter table menu_combo_items
 add constraint `fk_menu_combo_items_menu_combo_item_groups` foreign key (`_cache_id`, `menu_combo_item_group_id`)
     references `menu_combo_item_groups` (`_cache_id`, `id`) on delete cascade on update cascade;
 
 unlock tables;

drop table menu_combo_item_groups_menu_combo_items;