-- Migration: Cleanup license_sessions hardware_id column
-- Date: 2025-01-03
-- Description: Migrate hardware_id data to hardware_fingerprint and remove old column

BEGIN;

-- Copy data from hardware_id to hardware_fingerprint where it's null
UPDATE license_sessions 
SET hardware_fingerprint = hardware_id 
WHERE hardware_fingerprint IS NULL AND hardware_id IS NOT NULL;

-- Make hardware_fingerprint NOT NULL (it should have data now)
ALTER TABLE license_sessions 
ALTER COLUMN hardware_fingerprint SET NOT NULL;

-- Drop the old hardware_id column
ALTER TABLE license_sessions 
DROP COLUMN hardware_id CASCADE;

-- Update the index to use hardware_fingerprint
DROP INDEX IF EXISTS idx_license_sessions_license_hardware;
CREATE INDEX idx_license_sessions_license_hardware ON license_sessions(license_id, hardware_fingerprint);

-- Add comment
COMMENT ON COLUMN license_sessions.hardware_fingerprint IS 'Unique hardware identifier for the session';

COMMIT;