-- =============================================================================
-- Migration: Remove Legacy IP/Network Columns from machines Table
-- Date: 2025-11-14
-- Purpose: Remove unused network columns from machines table
--          All network data now stored in communications table
-- =============================================================================

-- PREREQUISITE: Printer IP addresses migrated to communications table
-- Run: migrate_printer_ips_to_communications.sql first

-- STEP 1: Drop legacy IP address columns
-- Note: ipaddress1 was used by printers (now in communications)
-- ipaddress2, ipaddress3 were never used (0 records)

ALTER TABLE machines DROP COLUMN ipaddress1;
ALTER TABLE machines DROP COLUMN ipaddress2;
ALTER TABLE machines DROP COLUMN ipaddress3;

-- STEP 2: Drop legacy MAC address columns
-- Note: macaddress1/2/3 were never used (0 records, no ASP references)

ALTER TABLE machines DROP COLUMN macaddress1;
ALTER TABLE machines DROP COLUMN macaddress2;
ALTER TABLE machines DROP COLUMN macaddress3;

-- STEP 3: Drop VLAN column
-- Note: Used in subnet management but never populated in machines table

ALTER TABLE machines DROP COLUMN vlan;

-- =============================================================================
-- Verification Queries
-- =============================================================================

-- Check machines table columns count
-- SELECT COUNT(*) as machines_columns
-- FROM information_schema.COLUMNS
-- WHERE TABLE_SCHEMA='shopdb' AND TABLE_NAME='machines';

-- Verify no IP columns remain
-- SELECT COLUMN_NAME
-- FROM information_schema.COLUMNS
-- WHERE TABLE_SCHEMA='shopdb' AND TABLE_NAME='machines'
--   AND COLUMN_NAME LIKE '%ipaddress%' OR COLUMN_NAME LIKE '%macaddress%' OR COLUMN_NAME = 'vlan';

-- Verify printer IPs are in communications table
-- SELECT COUNT(*) as printer_comms
-- FROM communications c
-- INNER JOIN machines m ON c.machineid = m.machineid
-- WHERE m.machinetypeid = 15 AND c.comstypeid = 1;

-- =============================================================================
-- Status: Ready to execute
-- Impact: Removes 7 unused/migrated columns from machines table
-- Tested: Pages updated to use communications table
-- =============================================================================
