# Phase 1: Database Schema Migration Scripts

**Created:** 2025-11-06
**Status:** Ready for DEV testing
**Estimated Time:** 15-20 minutes

---

## Overview

This directory contains Phase 1 migration scripts for consolidating the `pc` table into the `machines` table. These scripts create new infrastructure tables and extend existing tables to support both PCs and machines in a unified schema.

---

## Scripts Included

### Migration Scripts (Run in Order)

1. **01_create_communications_infrastructure.sql** (2-3 min)
   - Creates `comstypes` table (8 communication types)
   - Creates `communications` table (generic address field)

2. **02_extend_machines_table.sql** (2-3 min)
   - Adds 11 new columns to machines table
   - Adds indexes for new columns

3. **03_create_pc_machine_types.sql** (1 min)
   - Creates 5 PC machine types (Standard, Shopfloor, Engineer, Server, Laptop)

4. **04_create_warranty_infrastructure.sql** (1-2 min)
   - Creates `warranties` table
   - Creates warranty status views

5. **05_create_compliance_infrastructure.sql** (2-3 min)
   - Creates `compliance` table (15 columns)
   - Creates `compliancescans` table
   - Creates compliance views

6. **06_extend_businessunits_table.sql** (1 min)
   - Adds liaison fields to businessunits

7. **07_rename_pcstatus_to_machinestatus.sql** (1 min)
   - Renames pcstatus table and columns
   - Adds FK constraint to machines

8. **08_create_machine_relationships_infrastructure.sql** (2 min)
   - Creates `relationshiptypes` table
   - Creates `machinerelationships` table
   - Creates relationship views

### Rollback Scripts (Reverse Order)

- **ROLLBACK_08_machine_relationships_infrastructure.sql**
- **ROLLBACK_07_machinestatus_rename.sql**
- **ROLLBACK_06_businessunits_extensions.sql**
- **ROLLBACK_05_compliance_infrastructure.sql**
- **ROLLBACK_04_warranty_infrastructure.sql**
- **ROLLBACK_03_pc_machine_types.sql**
- **ROLLBACK_02_machines_table_extensions.sql**
- **ROLLBACK_01_communications_infrastructure.sql**

---

## Quick Start

### Option 1: Run All Scripts (Recommended for DEV)

```bash
# Run master script (executes all 8 in order)
docker exec -i dev-mysql mysql -u 570005354 -p570005354 shopdb < RUN_ALL_PHASE1_SCRIPTS.sql
```

### Option 2: Run Individual Scripts

```bash
# Run each script one at a time
docker exec -i dev-mysql mysql -u 570005354 -p570005354 shopdb < 01_create_communications_infrastructure.sql
docker exec -i dev-mysql mysql -u 570005354 -p570005354 shopdb < 02_extend_machines_table.sql
# ... and so on
```

### Option 3: Use MySQL Workbench

1. Connect to database
2. Open script file
3. Execute
4. Review output
5. Repeat for each script

---

## Pre-Execution Checklist

- [ ] **Backup created** - Full database backup exists
- [ ] **Dev environment** - Running on DEV, not production
- [ ] **Dependencies** - All required tables exist (machines, pctype, operatingsystems, etc.)
- [ ] **Disk space** - At least 100MB free
- [ ] **Permissions** - User has CREATE, ALTER, DROP privileges

---

## Execution Steps

### 1. Create Backup

```bash
# Backup current database
docker exec dev-mysql mysqldump -u 570005354 -p570005354 shopdb > /tmp/shopdb-before-phase1-$(date +%Y%m%d).sql
```

### 2. Run Migration Scripts

```bash
cd /home/camp/projects/windows/shopdb/sql/migration_phase1

# Option A: Run all at once
docker exec -i dev-mysql mysql -u 570005354 -p570005354 shopdb < RUN_ALL_PHASE1_SCRIPTS.sql

# Option B: Run individually for better control
for i in {01..08}; do
    echo "Running script $i..."
    docker exec -i dev-mysql mysql -u 570005354 -p570005354 shopdb < ${i}_*.sql
    if [ $? -ne 0 ]; then
        echo "ERROR in script $i! Stopping."
        exit 1
    fi
done
```

### 3. Verify Results

```bash
# Check new tables were created
docker exec dev-mysql mysql -u 570005354 -p570005354 shopdb -e "SHOW TABLES LIKE '%com%'; SHOW TABLES LIKE '%warrant%'; SHOW TABLES LIKE '%compliance%'; SHOW TABLES LIKE '%relationship%';"

# Check machines table structure
docker exec dev-mysql mysql -u 570005354 -p570005354 shopdb -e "DESCRIBE machines;"

# Check machine types
docker exec dev-mysql mysql -u 570005354 -p570005354 shopdb -e "SELECT * FROM machinetypes WHERE machinetype LIKE 'PC -%';"
```

### 4. If Rollback Needed

```bash
# Run rollback scripts in REVERSE order (08 down to 01)
for i in {08..01}; do
    echo "Rolling back script $i..."
    docker exec -i dev-mysql mysql -u 570005354 -p570005354 shopdb < ROLLBACK_${i}_*.sql
done
```

---

## Tables Created

### New Tables (7)

1. **comstypes** - Communication types (IP, Serial, etc.)
2. **communications** - Generic communications (replaces pc_comm_config + pc_network_interfaces)
3. **warranties** - Warranty tracking
4. **compliance** - Compliance and security tracking
5. **compliancescans** - Historical scan records
6. **relationshiptypes** - Types of machine relationships
7. **machinerelationships** - Machine-to-machine relationships

### Modified Tables (3)

1. **machines** - Added 11 columns (hostname, serialnumber, osid, etc.)
2. **businessunits** - Added 2 columns (liaisonname, liaisonsso)
3. **machinestatus** - Renamed from pcstatus

---

## New Columns in machines Table

1. hostname (VARCHAR 100)
2. loggedinuser (VARCHAR 100)
3. serialnumber (VARCHAR 100)
4. osid (INT 11) - FK to operatingsystems
5. machinestatusid (INT 11) - FK to machinestatus
6. pctypeid (INT 11) - FK to pctype
7. controllertypeid (INT 11) - FK to controllertypes
8. controllerosid (INT 11) - FK to operatingsystems
9. requires_manual_machine_config (TINYINT 1)
10. lastupdated (DATETIME)
11. dateadded (DATETIME)

---

## Views Created

- vw_warranty_status
- vw_warranties_expiring
- vw_compliance_summary
- vw_machine_relationships
- vw_dualpath_machines

---

## Testing Checklist

After running scripts, verify:

- [ ] All 7 new tables exist
- [ ] machines table has 11 new columns
- [ ] 5 PC machine types created
- [ ] machinestatus table exists (pcstatus renamed)
- [ ] All views created successfully
- [ ] No errors in execution
- [ ] Foreign keys in place
- [ ] Indexes created

---

## Common Issues

### Issue: FK constraint fails
**Cause:** Referenced table doesn't exist yet
**Fix:** Run scripts in order (01 through 08)

### Issue: Column already exists
**Cause:** Script already ran
**Fix:** Safe to ignore or run rollback first

### Issue: Permission denied
**Cause:** User lacks privileges
**Fix:** Grant CREATE, ALTER, DROP privileges

---

## Next Steps After Phase 1

1. **Test queries** - Verify new tables work
2. **Phase 2** - Data migration scripts (PC → machines)
3. **Phase 3** - Update views that reference pc table
4. **Phase 4** - Update ASP files

---

## Support

**Documentation:**
- Main design doc: `PC_MACHINES_CONSOLIDATION_PLAN.md`
- Status summary: `MIGRATION_STATUS_SUMMARY.md`
- Quick reference: `MIGRATION_QUICK_REFERENCE.md`

**Questions?**
- Review design documents
- Check CRITICAL_RULES.md
- Test on DEV first!

---

## Notes

- **REVERSIBLE:** All scripts have rollback scripts
- **SAFE:** No data deletion (only schema changes)
- **TESTED:** Design complete, ready for dev testing
- **ESTIMATED TIME:** 15-20 minutes total

**Created:** 2025-11-06
**Last Updated:** 2025-11-06
**Status:** ✅ Ready for DEV testing
