-- ============================================================================
-- eDNC Special Character Fix - Database Migration
-- Migrates ednclogs from hostname to machineid (FK to machines table)
-- Run on PRODUCTION after initial setup
-- Updated: 2025-12-12
-- ============================================================================

-- Step 1: Add machineid column to ednclogs
ALTER TABLE ednclogs
ADD COLUMN machineid INT NOT NULL AFTER logid,
ADD INDEX idx_machineid (machineid);

-- Step 2: Populate machineid from hostname (match existing PCs)
UPDATE ednclogs l
INNER JOIN machines m ON UPPER(m.hostname) = UPPER(l.hostname)
SET l.machineid = m.machineid
WHERE m.pctypeid IS NOT NULL;

-- Step 3: Remove hostname column and index
ALTER TABLE ednclogs DROP INDEX idx_hostname;
ALTER TABLE ednclogs DROP COLUMN hostname;

-- Verify migration
SELECT 'Migration complete' AS status;
DESCRIBE ednclogs;
