-- =====================================================
-- SCRIPT 07: Cleanup Phase 2 Migration Tables
-- =====================================================
-- Date: 2025-11-13
-- Purpose: Drop unused backup and migration tables after Phase 2 completion
-- Status: READY FOR TESTING
-- Estimated Time: < 1 minute
-- Dependencies:
--   - Phase 2 migration must be complete
--   - All ASP files must be updated to use new schema
--   - Verification that old tables are no longer in use
-- =====================================================

USE shopdb;
SET SQL_SAFE_UPDATES = 0;

-- =====================================================
-- STEP 1: Pre-cleanup verification
-- =====================================================

SELECT '========================================' AS '';
SELECT 'PHASE 2 MIGRATION CLEANUP' AS '';
SELECT '========================================' AS '';
SELECT '' AS '';

SELECT 'Tables to be dropped:' AS '';
SELECT '' AS '';

-- Show backup tables
SELECT 'Backup Tables:' AS '';
SELECT TABLE_NAME, TABLE_ROWS, ROUND(((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024), 2) AS 'Size_MB'
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'shopdb'
  AND (TABLE_NAME LIKE '%backup%phase2%' OR TABLE_NAME = 'pc_model_backup')
ORDER BY TABLE_NAME;

SELECT '' AS '';

-- Show mapping tables
SELECT 'Mapping Tables:' AS '';
SELECT TABLE_NAME, TABLE_ROWS, ROUND(((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024), 2) AS 'Size_MB'
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'shopdb'
  AND TABLE_NAME IN ('pc_to_machine_id_mapping', 'machine_pc_relationships')
ORDER BY TABLE_NAME;

SELECT '' AS '';

-- =====================================================
-- STEP 2: Confirm Phase 2 migration completion
-- =====================================================

SELECT 'Verifying Phase 2 migration status...' AS '';
SELECT '' AS '';

-- Check machines table has PCs
SELECT CONCAT('✓ PCs in machines table: ', COUNT(*)) AS status
FROM machines
WHERE pctypeid IS NOT NULL;

-- Check communications table has data
SELECT CONCAT('✓ Network interfaces in communications: ', COUNT(*)) AS status
FROM communications;

-- Check machinerelationships has data
SELECT CONCAT('✓ Relationships in machinerelationships: ', COUNT(*)) AS status
FROM machinerelationships;

SELECT '' AS '';

-- =====================================================
-- STEP 3: Drop backup tables
-- =====================================================

SELECT 'Dropping backup tables...' AS '';

-- Backup tables created during Phase 2 migration
DROP TABLE IF EXISTS pc_backup_phase2;
SELECT '✓ Dropped pc_backup_phase2' AS status;

DROP TABLE IF EXISTS pc_network_interfaces_backup_phase2;
SELECT '✓ Dropped pc_network_interfaces_backup_phase2' AS status;

DROP TABLE IF EXISTS pc_comm_config_backup_phase2;
SELECT '✓ Dropped pc_comm_config_backup_phase2' AS status;

DROP TABLE IF EXISTS pc_dualpath_assignments_backup_phase2;
SELECT '✓ Dropped pc_dualpath_assignments_backup_phase2' AS status;

DROP TABLE IF EXISTS pc_model_backup;
SELECT '✓ Dropped pc_model_backup' AS status;

SELECT '' AS '';

-- =====================================================
-- STEP 4: Drop mapping tables
-- =====================================================

SELECT 'Dropping mapping/helper tables...' AS '';

-- Mapping table used during migration (no longer needed)
DROP TABLE IF EXISTS pc_to_machine_id_mapping;
SELECT '✓ Dropped pc_to_machine_id_mapping' AS status;

-- Unused relationship table (replaced by machinerelationships)
DROP TABLE IF EXISTS machine_pc_relationships;
SELECT '✓ Dropped machine_pc_relationships' AS status;

SELECT '' AS '';

-- =====================================================
-- STEP 5: Verification
-- =====================================================

SELECT 'Verifying cleanup...' AS '';
SELECT '' AS '';

-- Check that essential tables still exist
SELECT CONCAT('✓ machines table exists: ', COUNT(*), ' records') AS status
FROM machines;

SELECT CONCAT('✓ communications table exists: ', COUNT(*), ' records') AS status
FROM communications;

SELECT CONCAT('✓ machinerelationships table exists: ', COUNT(*), ' records') AS status
FROM machinerelationships;

SELECT '' AS '';

-- Show remaining tables
SELECT 'Remaining migration-related tables:' AS '';
SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'shopdb'
  AND (TABLE_NAME LIKE 'pc%' OR TABLE_NAME LIKE '%backup%' OR TABLE_NAME LIKE '%mapping%')
ORDER BY TABLE_NAME;

SELECT '' AS '';
SELECT '========================================' AS '';
SELECT 'CLEANUP COMPLETE' AS '';
SELECT '========================================' AS '';

SET SQL_SAFE_UPDATES = 1;

-- =====================================================
-- NOTES
-- =====================================================
--
-- Tables dropped in this script:
--   1. pc_backup_phase2 (276 rows) - Backup of old pc table
--   2. pc_network_interfaces_backup_phase2 (705 rows) - Backup of network interfaces
--   3. pc_comm_config_backup_phase2 (502 rows) - Backup of comm config
--   4. pc_dualpath_assignments_backup_phase2 (33 rows) - Backup of dualpath assignments
--   5. pc_model_backup (206 rows) - Backup of pc models
--   6. pc_to_machine_id_mapping (221 rows) - Migration mapping table
--   7. machine_pc_relationships (0 rows) - Unused relationship table
--
-- Old tables NOT dropped (still may be in use):
--   - pc (old PC table - may still be referenced for historical data)
--   - pc_network_interfaces (old network interfaces)
--   - pc_comm_config (old communication config)
--   - pc_dualpath_assignments (old dualpath assignments)
--   - pc_dnc_config (DNC configuration)
--
-- ROLLBACK: If you need to restore, you must restore from database backup
--           taken BEFORE running this script. These tables cannot be recovered
--           after deletion.
--
-- =====================================================
