-- Alembic Revision: None

-- no fks

alter table `daily_cache_logs` modify `id` bigint(11) not null auto_increment;

alter table `scheduled_tasks` modify `id` bigint(11) not null auto_increment;

alter table `ticket_close_candidates` modify `id` bigint(11) not null auto_increment;

alter table `price_levels` modify `key` bigint(11) not null auto_increment;

alter table `option_sets` modify `key` bigint(11) not null auto_increment;


-- pay_rates

lock tables
    `pay_rate_levels` write,
    `pay_rates` write;

     alter table `pay_rate_levels`
drop foreign key `fk_pay_rate_levels_pay_rates`,
          modify `key` bigint(11) not null auto_increment,
          modify `pay_rate_key` bigint(11) not null;

alter table `pay_rates` modify `key` bigint(11) not null auto_increment;

   alter table `pay_rate_levels`
add constraint `fk_pay_rate_levels_pay_rates` foreign key (`pay_rate_key`, `_cache_id`)
    references `pay_rates` (`key`, `_cache_id`) on delete cascade on update cascade;

unlock tables;


-- ticket_items

lock tables
    `ticket_item_discounts` write,
    `ticket_item_modifier_closures` write,
    `ticket_items` write,
    `ticket_item_modifiers` write;

     alter table `ticket_item_discounts`
drop foreign key `fk_ticket_item_discounts_ticket_items`,
          modify `ticket_item_key` bigint(11) not null;

     alter table `ticket_item_modifier_closures`
drop foreign key `fk_ticket_item_modifier_closures_item_modifiers`,
drop foreign key `fk_ticket_item_modifier_closures_item_modifier_targets`,
drop foreign key `fk_ticket_item_modifier_closures_item_targets`,
          modify `modifier_key` bigint(11) not null,
          modify `target_modifier_key` bigint(11) default null,
          modify `target_item_key` bigint(11) default null;

alter table `ticket_items` modify `key` bigint(11) not null auto_increment;

alter table `ticket_item_modifiers` modify `key` bigint(11) not null auto_increment;

   alter table `ticket_item_discounts`
add CONSTRAINT `fk_ticket_item_discounts_ticket_items` FOREIGN KEY (`ticket_item_key`, `_cache_id`) REFERENCES `ticket_items` (`key`, `_cache_id`) ON DELETE CASCADE ON UPDATE CASCADE;

   alter table `ticket_item_modifier_closures`
add CONSTRAINT `fk_ticket_item_modifier_closures_item_modifier_targets` FOREIGN KEY (`target_modifier_key`, `_cache_id`) REFERENCES `ticket_item_modifiers` (`key`, `_cache_id`) ON DELETE CASCADE ON UPDATE CASCADE,
add CONSTRAINT `fk_ticket_item_modifier_closures_item_modifiers` FOREIGN KEY (`modifier_key`, `_cache_id`) REFERENCES `ticket_item_modifiers` (`key`, `_cache_id`) ON DELETE CASCADE ON UPDATE CASCADE,
add CONSTRAINT `fk_ticket_item_modifier_closures_item_targets` FOREIGN KEY (`target_item_key`, `_cache_id`) REFERENCES `ticket_items` (`key`, `_cache_id`) ON DELETE CASCADE ON UPDATE CASCADE;

unlock tables;


-- held_ticket_items

lock tables
    `held_ticket_item_discounts` write,
    `held_ticket_item_modifier_closures` write,
    `held_ticket_items` write,
    `held_ticket_item_modifiers` write;

     alter table `held_ticket_item_discounts`
drop foreign key `fk_held_ticket_item_discounts_ticket_items`,
          modify `ticket_item_key` bigint(11) not null;

     alter table `held_ticket_item_modifier_closures`
drop foreign key `fk_held_ticket_item_modifier_closures_item_modifiers`,
drop foreign key `fk_held_ticket_item_modifier_closures_item_modifier_targets`,
drop foreign key `fk_held_ticket_item_modifier_closures_item_targets`,
          modify `modifier_key` bigint(11) not null,
          modify `target_modifier_key` bigint(11) default null,
          modify `target_item_key` bigint(11) default null;

alter table `held_ticket_items` modify `key` bigint(11) not null auto_increment;

alter table `held_ticket_item_modifiers` modify `key` bigint(11) not null auto_increment;

   alter table `held_ticket_item_discounts`
add CONSTRAINT `fk_held_ticket_item_discounts_ticket_items` FOREIGN KEY (`ticket_item_key`, `_cache_id`) REFERENCES `held_ticket_items` (`key`, `_cache_id`) ON DELETE CASCADE ON UPDATE CASCADE;

   alter table `held_ticket_item_modifier_closures`
add CONSTRAINT `fk_held_ticket_item_modifier_closures_item_modifiers` FOREIGN KEY (`target_modifier_key`, `_cache_id`) REFERENCES `held_ticket_item_modifiers` (`key`, `_cache_id`) ON DELETE CASCADE ON UPDATE CASCADE,
add CONSTRAINT `fk_held_ticket_item_modifier_closures_item_modifier_targets` FOREIGN KEY (`modifier_key`, `_cache_id`) REFERENCES `held_ticket_item_modifiers` (`key`, `_cache_id`) ON DELETE CASCADE ON UPDATE CASCADE,
add CONSTRAINT `fk_held_ticket_item_modifier_closures_item_targets` FOREIGN KEY (`target_item_key`, `_cache_id`) REFERENCES `held_ticket_items` (`key`, `_cache_id`) ON DELETE CASCADE ON UPDATE CASCADE;

unlock tables;

