-- Script de limpeza após falha parcial da migration 20260722180000_restructure_schedule_items.
-- Execute NO banco admsis_protik ANTES de rodar novamente: npx prisma migrate deploy
--
-- Uso:
--   mysql --ssl-mode=DISABLED -u USER -p admsis_protik < migrations/fix_failed_schedule_restructure.sql
--   npx prisma migrate resolve --rolled-back 20260722180000_restructure_schedule_items
--   npx prisma migrate deploy

-- Remove tabela nova se criada na tentativa anterior (dados ainda estão em ptk_project_schedule)


-- Se item_id foi adicionado sem concluir o remap, remove a coluna
SET @drop_user_item := (
  SELECT IF(
    EXISTS(
      SELECT 1 FROM information_schema.columns
      WHERE table_schema = DATABASE()
        AND table_name = 'ptk_project_schedule_user'
        AND column_name = 'item_id'
    )
    AND EXISTS(
      SELECT 1 FROM information_schema.columns
      WHERE table_schema = DATABASE()
        AND table_name = 'ptk_project_schedule_user'
        AND column_name = 'schedule_id'
    ),
    'ALTER TABLE `ptk_project_schedule_user` DROP COLUMN `item_id`',
    'SELECT 1'
  )
);
PREPARE s1 FROM @drop_user_item; EXECUTE s1; DEALLOCATE PREPARE s1;

SET @drop_ticket_item := (
  SELECT IF(
    EXISTS(
      SELECT 1 FROM information_schema.columns
      WHERE table_schema = DATABASE()
        AND table_name = 'ptk_schedule_ticket'
        AND column_name = 'item_id'
    )
    AND EXISTS(
      SELECT 1 FROM information_schema.columns
      WHERE table_schema = DATABASE()
        AND table_name = 'ptk_schedule_ticket'
        AND column_name = 'schedule_id'
    ),
    'ALTER TABLE `ptk_schedule_ticket` DROP COLUMN `item_id`',
    'SELECT 1'
  )
);
PREPARE s2 FROM @drop_ticket_item; EXECUTE s2; DEALLOCATE PREPARE s2;

