-- =====================================================
-- RUN ALL: Phase 3 Migration Scripts
-- =====================================================
-- Date: 2025-11-10
-- Purpose: Execute all Phase 3 migration scripts in sequence
-- Part of: Phase 3 Migration (Network Devices → machines table)
-- Estimated Time: 5-10 minutes total
-- =====================================================
--
-- IMPORTANT: READ BEFORE RUNNING
-- =====================================================
-- 1. BACKUP YOUR DATABASE FIRST!
--    mysqldump -u root -p shopdb > shopdb_backup_before_phase3.sql
--
-- 2. Test on a backup database first!
--
-- 3. Schedule during maintenance window
--
-- 4. Monitor console output for errors
--
-- 5. If any script fails, STOP and review
--
-- 6. Keep old tables for 30 days (rollback safety)
-- =====================================================

USE shopdb;

-- Enable detailed error reporting
SET SQL_MODE='TRADITIONAL';
SET SQL_SAFE_UPDATES = 0;

SELECT '============================================================' AS '';
SELECT 'PHASE 3 MIGRATION - NETWORK DEVICES TO MACHINES TABLE' AS '';
SELECT '============================================================' AS '';
SELECT 'Start Time:' AS '', NOW() AS timestamp;
SELECT '' AS '';

-- =====================================================
-- SCRIPT 01: Create Network Machine Types
-- =====================================================

SELECT '------------------------------------------------------------' AS '';
SELECT 'Running Script 01: Create Network Machine Types' AS '';
SELECT '------------------------------------------------------------' AS '';

SOURCE 01_create_network_machinetypes.sql;

SELECT '' AS '';
SELECT '✓ Script 01 complete' AS status;
SELECT '' AS '';

-- Pause to allow review (in manual execution)
-- UNCOMMENT IF RUNNING MANUALLY: SELECT 'Press Enter to continue...' AS ''; PROMPT;

-- =====================================================
-- SCRIPT 02: Migrate Servers
-- =====================================================

SELECT '------------------------------------------------------------' AS '';
SELECT 'Running Script 02: Migrate Servers to machines Table' AS '';
SELECT '------------------------------------------------------------' AS '';

SOURCE 02_migrate_servers_to_machines.sql;

SELECT '' AS '';
SELECT '✓ Script 02 complete' AS status;
SELECT '' AS '';

-- =====================================================
-- SCRIPT 03: Migrate Switches
-- =====================================================

SELECT '------------------------------------------------------------' AS '';
SELECT 'Running Script 03: Migrate Switches to machines Table' AS '';
SELECT '------------------------------------------------------------' AS '';

SOURCE 03_migrate_switches_to_machines.sql;

SELECT '' AS '';
SELECT '✓ Script 03 complete' AS status;
SELECT '' AS '';

-- =====================================================
-- SCRIPT 04: Migrate Cameras
-- =====================================================

SELECT '------------------------------------------------------------' AS '';
SELECT 'Running Script 04: Migrate Cameras to machines Table' AS '';
SELECT '------------------------------------------------------------' AS '';

SOURCE 04_migrate_cameras_to_machines.sql;

SELECT '' AS '';
SELECT '✓ Script 04 complete' AS status;
SELECT '' AS '';

-- =====================================================
-- SCRIPT 07: Migrate Network Communications
-- =====================================================

SELECT '------------------------------------------------------------' AS '';
SELECT 'Running Script 07: Migrate Network Communications' AS '';
SELECT '------------------------------------------------------------' AS '';

SOURCE 07_migrate_network_communications.sql;

SELECT '' AS '';
SELECT '✓ Script 07 complete' AS status;
SELECT '' AS '';

-- =====================================================
-- SCRIPT 08: Create Network Relationship Types
-- =====================================================

SELECT '------------------------------------------------------------' AS '';
SELECT 'Running Script 08: Create Network Relationship Types' AS '';
SELECT '------------------------------------------------------------' AS '';

SOURCE 08_create_network_relationship_types.sql;

SELECT '' AS '';
SELECT '✓ Script 08 complete' AS status;
SELECT '' AS '';

-- =====================================================
-- SCRIPT 09: Update Views
-- =====================================================

SELECT '------------------------------------------------------------' AS '';
SELECT 'Running Script 09: Update Views for Network Devices' AS '';
SELECT '------------------------------------------------------------' AS '';

SOURCE 09_update_views_for_network_devices.sql;

SELECT '' AS '';
SELECT '✓ Script 09 complete' AS status;
SELECT '' AS '';

-- =====================================================
-- VERIFICATION
-- =====================================================

SELECT '============================================================' AS '';
SELECT 'RUNNING VERIFICATION' AS '';
SELECT '============================================================' AS '';

SOURCE VERIFY_PHASE3_MIGRATION.sql;

-- =====================================================
-- COMPLETION SUMMARY
-- =====================================================

SELECT '' AS '';
SELECT '============================================================' AS '';
SELECT 'PHASE 3 MIGRATION COMPLETE' AS '';
SELECT '============================================================' AS '';
SELECT 'End Time:' AS '', NOW() AS timestamp;
SELECT '' AS '';

-- Summary of migrated devices
SELECT 'Migration Summary:' AS status;
SELECT
    mt.machinetype AS device_type,
    COUNT(*) AS migrated_count
FROM machines m
JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
WHERE mt.machinetypeid BETWEEN 30 AND 36
GROUP BY mt.machinetype
ORDER BY mt.machinetypeid;

SELECT '' AS '';
SELECT '============================================================' AS '';
SELECT 'NEXT STEPS:' AS '';
SELECT '============================================================' AS '';
SELECT '1. Review verification results above for any FAIL messages' AS step;
SELECT '2. Test application pages (displaymachines.asp, etc.)' AS step;
SELECT '3. Update application code to use new schema' AS step;
SELECT '4. Monitor for 30 days' AS step;
SELECT '5. Drop old tables only after confirming stability:' AS step;
SELECT '   DROP TABLE servers;' AS '';
SELECT '   DROP TABLE switches;' AS '';
SELECT '   DROP TABLE cameras;' AS '';
SELECT '============================================================' AS '';

SET SQL_SAFE_UPDATES = 1;

-- =====================================================
-- ROLLBACK INSTRUCTIONS (if needed)
-- =====================================================
-- If migration fails or issues found:
-- SOURCE ROLLBACK_PHASE3.sql;
-- =====================================================
