# Infrastructure Support - Production Migration Checklist

**Date Created:** 2025-10-23
**Status:** TESTED ON DEV - READY FOR PRODUCTION
**Estimated Downtime:** 2-5 minutes

---

## Pre-Migration Checklist

### 1. Backup Database
```bash
# Create backup before migration
docker exec dev-mysql mysqldump -u 570005354 -p570005354 shopdb > shopdb_backup_$(date +%Y%m%d_%H%M%S).sql

# Verify backup file exists and has content
ls -lh shopdb_backup_*.sql
```

### 2. Verify Current State
```sql
-- Check if tables exist
SELECT table_name FROM information_schema.tables
WHERE table_schema='shopdb'
AND table_name IN ('servers', 'switches', 'cameras', 'idfs');

-- Check current row counts
SELECT 'servers' as table_name, COUNT(*) as count FROM servers
UNION ALL
SELECT 'switches', COUNT(*) FROM switches
UNION ALL
SELECT 'cameras', COUNT(*) FROM cameras
UNION ALL
SELECT 'idfs', COUNT(*) FROM idfs;
```

### 3. Test Database Connection
```bash
# Ensure you can connect
mysql -h <host> -u <user> -p<password> shopdb -e "SELECT 'Connection successful' as status;"
```

---

## Migration Scripts (Run in Order)

### Script 1: add_infrastructure_vendor_model_support.sql

**Purpose:** Adds modelid column to infrastructure tables and creates vw_network_devices view

**What it does:**
- Adds `isserver`, `isswitch`, `iscamera` flags to vendors table (if not exist)
- Adds `modelid` to servers table (if not exist)
- Adds `modelid` to switches table (if not exist)
- Adds `modelid` to cameras table (if not exist)
- Adds `modelid` to accesspoints table (if not exist)
- Creates foreign keys to models table
- Creates `vw_network_devices` view

**Safe to re-run:** YES - Uses IF NOT EXISTS checks

**How to run:**
```bash
mysql -h <host> -u <user> -p<password> shopdb < add_infrastructure_vendor_model_support.sql
```

**Expected output:**
```
Step 1 Complete: Added vendor flags for infrastructure devices
Step 2 Complete: Added modelid to servers table
Step 3 Complete: Added modelid to switches table
Step 4 Complete: Added modelid to cameras table
Step 5 Complete: Added modelid to accesspoints table
Step 6 Complete: Updated vw_network_devices with vendor/model info
VENDOR/MODEL SUPPORT ADDED SUCCESSFULLY!
```

**Verification:**
```sql
-- Verify modelid columns exist
DESCRIBE servers;
DESCRIBE switches;
DESCRIBE cameras;

-- Verify view exists
SELECT * FROM vw_network_devices LIMIT 1;

-- Verify foreign keys
SELECT TABLE_NAME, CONSTRAINT_NAME, REFERENCED_TABLE_NAME
FROM information_schema.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = 'shopdb'
  AND TABLE_NAME IN ('servers', 'switches', 'cameras')
  AND REFERENCED_TABLE_NAME = 'models';
```

### Script 2: add_device_name_fields.sql

**Purpose:** Adds name fields to infrastructure tables

**What it does:**
- Adds `servername` to servers table (if not exist)
- Adds `switchname` to switches table (if not exist)
- Adds `cameraname` to cameras table (if not exist)
- Adds `apname` to accesspoints table (if not exist)
- Updates `vw_network_devices` view to include name fields and camera-specific columns (idfid, idfname, macaddress)
- Includes Access Points and Printers in the view

**Safe to re-run:** YES - Uses IF NOT EXISTS checks

**How to run:**
```bash
mysql -h <host> -u <user> -p<password> shopdb < add_device_name_fields.sql
```

**Expected output:**
```
Step 1 Complete: Added servername to servers table
Step 2 Complete: Added switchname to switches table
Step 3 Complete: Added cameraname to cameras table
Step 4 Complete: Added apname to accesspoints table
Step 5 Complete: Updated vw_network_devices view with name fields, camera columns, and printers
NAME FIELDS ADDED SUCCESSFULLY!
```

**Verification:**
```sql
-- Verify name columns exist
SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'shopdb'
  AND TABLE_NAME IN ('servers', 'switches', 'cameras', 'idfs', 'accesspoints')
  AND COLUMN_NAME LIKE '%name%'
ORDER BY TABLE_NAME;

-- Should show:
-- accesspoints | apname      | varchar
-- cameras      | cameraname  | varchar
-- idfs         | idfname     | varchar
-- servers      | servername  | varchar
-- switches     | switchname  | varchar
```

---

## Post-Migration Verification

### 1. Check Final Schema

**IDFs Table:**
```sql
DESCRIBE idfs;
-- Expected: idfid, idfname, description, maptop, mapleft, isactive
```

**Servers Table:**
```sql
DESCRIBE servers;
-- Expected: serverid, servername, modelid, serialnumber, ipaddress,
--           description, maptop, mapleft, isactive
```

**Switches Table:**
```sql
DESCRIBE switches;
-- Expected: switchid, switchname, modelid, serialnumber, ipaddress,
--           description, maptop, mapleft, isactive
```

**Cameras Table:**
```sql
DESCRIBE cameras;
-- Expected: cameraid, cameraname, modelid, idfid, serialnumber, macaddress,
--           ipaddress, description, maptop, mapleft, isactive
```

### 2. Test View

```sql
-- View should include device_name column
SELECT device_type, device_name, vendor, modelnumber
FROM vw_network_devices
LIMIT 5;
```

### 3. Verify Data Integrity

```sql
-- Check that existing data is intact
SELECT COUNT(*) as total_devices FROM (
    SELECT idfid as id FROM idfs WHERE isactive = 1
    UNION ALL
    SELECT serverid FROM servers WHERE isactive = 1
    UNION ALL
    SELECT switchid FROM switches WHERE isactive = 1
    UNION ALL
    SELECT cameraid FROM cameras WHERE isactive = 1
) as all_devices;

-- Compare this count to pre-migration counts
```

---

## Rollback Plan (If Needed)

### If Script 1 Fails or Needs Rollback

```sql
-- Drop foreign keys
ALTER TABLE servers DROP FOREIGN KEY servers_ibfk_1;
ALTER TABLE switches DROP FOREIGN KEY switches_ibfk_1;
ALTER TABLE cameras DROP FOREIGN KEY fk_cameras_model;

-- Drop modelid columns
ALTER TABLE servers DROP COLUMN modelid;
ALTER TABLE switches DROP COLUMN modelid;
ALTER TABLE cameras DROP COLUMN modelid;

-- Drop vendor flags (optional - these don't hurt anything)
ALTER TABLE vendors DROP COLUMN isserver;
ALTER TABLE vendors DROP COLUMN isswitch;
ALTER TABLE vendors DROP COLUMN iscamera;

-- Drop view
DROP VIEW IF EXISTS vw_network_devices;
```

### If Script 2 Fails or Needs Rollback

```sql
-- Drop name columns
ALTER TABLE servers DROP COLUMN servername;
ALTER TABLE switches DROP COLUMN switchname;
ALTER TABLE cameras DROP COLUMN cameraname;

-- Recreate old view (without name fields)
DROP VIEW IF EXISTS vw_network_devices;

CREATE VIEW vw_network_devices AS
SELECT
  'IDF' AS device_type,
  i.idfid AS device_id,
  NULL AS modelid,
  NULL AS modelnumber,
  NULL AS vendor,
  NULL AS serialnumber,
  NULL AS ipaddress,
  i.description,
  i.maptop,
  i.mapleft,
  i.isactive
FROM idfs i
UNION ALL
SELECT
  'Server' AS device_type,
  s.serverid AS device_id,
  s.modelid,
  m.modelnumber,
  v.vendor,
  s.serialnumber,
  s.ipaddress,
  s.description,
  s.maptop,
  s.mapleft,
  s.isactive
FROM servers s
LEFT JOIN models m ON s.modelid = m.modelnumberid
LEFT JOIN vendors v ON m.vendorid = v.vendorid
UNION ALL
SELECT
  'Switch' AS device_type,
  sw.switchid AS device_id,
  sw.modelid,
  m.modelnumber,
  v.vendor,
  sw.serialnumber,
  sw.ipaddress,
  sw.description,
  sw.maptop,
  sw.mapleft,
  sw.isactive
FROM switches sw
LEFT JOIN models m ON sw.modelid = m.modelnumberid
LEFT JOIN vendors v ON m.vendorid = v.vendorid
UNION ALL
SELECT
  'Camera' AS device_type,
  c.cameraid AS device_id,
  c.modelid,
  m.modelnumber,
  v.vendor,
  c.serialnumber,
  c.ipaddress,
  c.description,
  c.maptop,
  c.mapleft,
  c.isactive
FROM cameras c
LEFT JOIN models m ON c.modelid = m.modelnumberid
LEFT JOIN vendors v ON m.vendorid = v.vendorid;
```

### Restore from Backup (Complete Rollback)

```bash
# Stop application (if applicable)
# Restore database
mysql -h <host> -u <user> -p<password> shopdb < shopdb_backup_YYYYMMDD_HHMMSS.sql

# Verify restoration
mysql -h <host> -u <user> -p<password> shopdb -e "SHOW TABLES; SELECT COUNT(*) FROM servers;"
```

---

## Impact Assessment

### Changes to Existing Data
**NONE** - These migrations are purely additive:
- Add new columns (all nullable, won't break existing queries)
- Add foreign keys (won't break existing data)
- Create/update views (won't affect base tables)

### Impact on Existing Application
**MINIMAL** - Existing pages will continue to work:
- All new columns are nullable
- Existing SELECT queries still work (just ignore new columns)
- Existing INSERT queries still work (new columns default to NULL)
- View creation doesn't affect existing queries

### Required Application Updates
**NEW PAGES ONLY** - Existing application needs no changes:
- Need to create new network device management pages
- Need to update navigation menu
- Optionally update network_map.asp to show infrastructure

---

## Testing Steps (After Migration)

### 1. Verify Schema
```sql
-- Run all verification queries above
-- Ensure all columns exist
-- Ensure foreign keys are in place
```

### 2. Test Inserts
```sql
-- Test adding a server
INSERT INTO servers (servername, serialnumber, ipaddress, description, isactive)
VALUES ('TEST-SERVER-01', 'SN12345', '192.168.1.100', 'Test server', 1);

-- Verify
SELECT * FROM servers WHERE servername = 'TEST-SERVER-01';

-- Test view
SELECT * FROM vw_network_devices WHERE device_type = 'Server' AND device_name = 'TEST-SERVER-01';

-- Cleanup
DELETE FROM servers WHERE servername = 'TEST-SERVER-01';
```

### 3. Test Existing Functionality
- Navigate to existing pages (displayprinters.asp, displaymachines.asp, etc.)
- Verify no errors
- Verify data displays correctly

---

## Production Deployment Schedule

### Recommended Timing
- **Maintenance window:** After hours or weekend
- **Duration:** 15-30 minutes (includes verification)
- **Downtime:** 2-5 minutes (only during migration)

### Deployment Steps

1. **Announce maintenance** (1 day before)
2. **Backup database** (immediately before)
3. **Run migration script 1** (2 minutes)
4. **Verify script 1** (1 minute)
5. **Run migration script 2** (2 minutes)
6. **Verify script 2** (1 minute)
7. **Test inserts/queries** (5 minutes)
8. **Deploy new ASP pages** (when ready)
9. **Update navigation menu** (when ready)
10. **Announce completion**

---

## Support Contacts

- **DBA:** [Name/Contact]
- **Developer:** [Name/Contact]
- **After-hours:** [Emergency Contact]

---

## Migration History

| Date | Environment | Status | Notes |
|------|-------------|--------|-------|
| 2025-10-23 | Dev | ✅ Success | Both scripts ran successfully, all verifications passed |
| TBD | Production | Pending | |

---

## Final Schema Reference

```
idfs:
  ✅ idfid (PK)
  ✅ idfname
  ✅ description
  ✅ maptop, mapleft
  ✅ isactive

servers:
  ✅ serverid (PK)
  ➕ servername (NEW)
  ➕ modelid → models (NEW FK)
  ✅ serialnumber
  ✅ ipaddress
  ✅ description
  ✅ maptop, mapleft
  ✅ isactive

switches:
  ✅ switchid (PK)
  ➕ switchname (NEW)
  ➕ modelid → models (NEW FK)
  ✅ serialnumber
  ✅ ipaddress
  ✅ description
  ✅ maptop, mapleft
  ✅ isactive

cameras:
  ✅ cameraid (PK)
  ➕ cameraname (NEW)
  ➕ modelid → models (NEW FK)
  ✅ idfid → idfs (EXISTING)
  ✅ serialnumber
  ✅ macaddress
  ✅ ipaddress
  ✅ description
  ✅ maptop, mapleft
  ✅ isactive

accesspoints:
  ✅ apid (PK)
  ➕ apname (NEW)
  ➕ modelid → models (NEW FK)
  ✅ serialnumber
  ✅ ipaddress
  ✅ description
  ✅ maptop, mapleft
  ✅ isactive

vw_network_devices:
  ➕ device_name (NEW)
  ✅ device_type, device_id (Includes: IDF, Server, Switch, Camera, Access Point, Printer)
  ➕ modelid, modelnumber, vendor (NEW)
  ✅ serialnumber, ipaddress, description
  ✅ maptop, mapleft, isactive
  ➕ idfid, idfname (Camera-specific - NEW)
  ➕ macaddress (Camera-specific - NEW)
```

---

**Status:** READY FOR PRODUCTION ✅
**Last Updated:** 2025-10-23

