# Production Deployment Instructions - Infrastructure Migration

**Date:** 2025-10-23
**Estimated Time:** 5-10 minutes
**Estimated Downtime:** 2-5 minutes (during script execution)

---

## Overview

This migration adds infrastructure device tracking (servers, switches, cameras, access points, IDFs) to the ShopDB application. All scripts are **safe to re-run** and use IF NOT EXISTS checks.

---

## Pre-Deployment Checklist

### 1. Create Database Backup

```bash
# Navigate to backup location
cd /path/to/backups

# Create timestamped backup
mysqldump -h <hostname> -u <username> -p<password> shopdb > shopdb_backup_$(date +%Y%m%d_%H%M%S).sql

# Verify backup was created
ls -lh shopdb_backup_*.sql
```

### 2. Verify Database Connection

```bash
mysql -h <hostname> -u <username> -p<password> shopdb -e "SELECT 'Connection successful' as status;"
```

### 3. Check Current State

```bash
mysql -h <hostname> -u <username> -p<password> shopdb -e "
SELECT COUNT(*) as active_printers FROM printers WHERE isactive = 1;
SELECT COUNT(*) as total_machines FROM machines WHERE isactive = 1;
"
```

**Expected Results:**
- Should see printer count (likely 37-40)
- Should see machine count

---

## Migration Execution

Run these scripts **in order**. Each script is idempotent (safe to re-run).

### Script 1: Create Infrastructure Tables

```bash
mysql -h <hostname> -u <username> -p<password> shopdb < sql/PRODUCTION_READY_infrastructure_migration_v2.sql
```

**Expected Output:**
```
Step 1 Complete: Added maptop and mapleft columns to printers table
Step 2 Complete: Created IDFs table
Step 3 Complete: Created switches table
Step 4 Complete: Created accesspoints table
Step 5 Complete: Created servers table
Step 6 Complete: Created cameras table
Step 7 Complete: Cleaned up duplicate -PRINTER machines
Step 8 Complete: Migrated printer coordinates from machines
Step 9 Complete: Created infrastructure views
MIGRATION COMPLETED SUCCESSFULLY!
```

**What it does:**
- Creates 5 new tables: idfs, servers, switches, cameras, accesspoints
- Adds maptop/mapleft to printers table
- Deactivates machines with names like "-PRINTER" (duplicates)
- Migrates printer coordinates from machines to printers

---

### Script 2: Add Vendor/Model Support

```bash
mysql -h <hostname> -u <username> -p<password> shopdb < sql/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!
```

**What it does:**
- Adds modelid column to all infrastructure tables
- Links infrastructure devices to existing vendors/models tables
- Creates vw_network_devices view (unified view of all devices)

---

### Script 3: Add Device Name Fields

```bash
mysql -h <hostname> -u <username> -p<password> shopdb < sql/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!
```

**What it does:**
- Adds name fields to all infrastructure tables
- Updates vw_network_devices view with device names
- Includes camera-specific columns (idfid, idfname, macaddress)
- **Includes printers in unified view**

---

## Post-Migration Verification

### 1. Verify Tables Created

```bash
mysql -h <hostname> -u <username> -p<password> shopdb -e "
SHOW TABLES LIKE '%servers%';
SHOW TABLES LIKE '%switches%';
SHOW TABLES LIKE '%cameras%';
SHOW TABLES LIKE '%accesspoints%';
SHOW TABLES LIKE '%idfs%';
"
```

**Expected:** All 5 tables should exist

---

### 2. Verify View Includes All Device Types

```bash
mysql -h <hostname> -u <username> -p<password> shopdb -e "
SELECT device_type, COUNT(*) as count
FROM vw_network_devices
GROUP BY device_type
ORDER BY device_type;
"
```

**Expected Output:**
```
device_type      count
Printer          37-40 (your printer count)
```

*Note: Other device types will show 0 until you add infrastructure devices*

---

### 3. Verify Printer Coordinates Migrated

```bash
mysql -h <hostname> -u <username> -p<password> shopdb -e "
SELECT COUNT(*) as printers_with_coordinates
FROM printers
WHERE maptop IS NOT NULL AND mapleft IS NOT NULL;
"
```

**Expected:** Should show count of printers that had coordinates

---

### 4. Test Application Pages

Navigate to these pages and verify no errors:

1. **http://your-server/network_devices.asp**
   - Should load with tabs: All, IDF, Server, Switch, Camera, Access Point, Printer
   - Click "Printer" tab - should show all printers

2. **http://your-server/network_devices.asp?filter=Printer**
   - Should display all printers with vendor/model info

3. **http://your-server/network_devices.asp?filter=Camera**
   - Should show "No cameras found" (not a 500 error!)

4. **http://your-server/displayprinters.asp**
   - Existing printer page should still work

5. **http://your-server/network_map.asp**
   - Map should still work with existing data

---

## If Something Goes Wrong

### Rollback Option 1: Restore from Backup

```bash
# Stop application if needed
# Restore database
mysql -h <hostname> -u <username> -p<password> shopdb < shopdb_backup_YYYYMMDD_HHMMSS.sql

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

### Rollback Option 2: Run Rollback Script

```bash
mysql -h <hostname> -u <username> -p<password> shopdb < sql/ROLLBACK_infrastructure_migration.sql
```

**Warning:** This will drop all infrastructure tables and columns added by the migration.

---

## Post-Deployment Notes

### Immediate Impact
- ✅ All existing functionality continues to work
- ✅ New "Network Devices" menu item appears in navigation
- ✅ Printers now visible in unified network devices view
- ✅ No data loss - all existing data preserved

### What's New
- New menu: "Network Devices" (left sidebar)
- Unified device management page at `/network_devices.asp`
- Ready to add servers, switches, cameras, access points, IDFs
- Printer coordinates now on printers table (migrated from machines)

### No Breaking Changes
- All existing pages work unchanged
- Existing queries continue to function
- All new columns are nullable
- Foreign keys allow NULL values

---

## Support Contacts

- **DBA:** [Your DBA Contact]
- **Developer:** [Your Developer Contact]
- **After-hours:** [Emergency Contact]

---

## Migration History Log

Record your deployment here:

| Date | Time | Executed By | Status | Notes |
|------|------|-------------|--------|-------|
| YYYY-MM-DD | HH:MM | [Name] | [ ] Success / [ ] Rollback | |

---

## Quick Command Reference

**Full migration (all 3 scripts):**

```bash
# Set your database credentials
DB_HOST="your-host"
DB_USER="your-user"
DB_PASS="your-password"
DB_NAME="shopdb"

# Navigate to sql directory
cd /path/to/shopdb/sql

# Run all 3 scripts in sequence
mysql -h $DB_HOST -u $DB_USER -p$DB_PASS $DB_NAME < PRODUCTION_READY_infrastructure_migration_v2.sql
mysql -h $DB_HOST -u $DB_USER -p$DB_PASS $DB_NAME < add_infrastructure_vendor_model_support.sql
mysql -h $DB_HOST -u $DB_USER -p$DB_PASS $DB_NAME < add_device_name_fields.sql

# Verify
mysql -h $DB_HOST -u $DB_USER -p$DB_PASS $DB_NAME -e "SELECT device_type, COUNT(*) FROM vw_network_devices GROUP BY device_type;"
```

---

**Status:** READY FOR PRODUCTION ✅
**Last Tested:** 2025-10-23 (on fresh production data backup)
**Test Results:** All scripts completed successfully, all device types displaying correctly
