-- =====================================================
-- ROLLBACK 07: Rename machinestatus back to pcstatus
-- =====================================================
-- Date: 2025-11-06
-- Purpose: Rollback script 07_rename_pcstatus_to_machinestatus.sql
-- =====================================================

USE shopdb;
SET SQL_SAFE_UPDATES = 0;

-- =====================================================
-- STEP 1: Drop FK constraint from machines table
-- =====================================================

SET @stmt = (SELECT IF(
    (SELECT COUNT(*) FROM information_schema.TABLE_CONSTRAINTS
     WHERE CONSTRAINT_SCHEMA = 'shopdb'
       AND TABLE_NAME = 'machines'
       AND CONSTRAINT_NAME = 'fk_machines_machinestatusid') > 0,
    'ALTER TABLE machines DROP FOREIGN KEY fk_machines_machinestatusid',
    'SELECT "FK fk_machines_machinestatusid does not exist"'
));
PREPARE stmt FROM @stmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- =====================================================
-- STEP 2: Rename status column back
-- =====================================================

ALTER TABLE machinestatus
CHANGE COLUMN machinestatus pcstatus VARCHAR(50);

-- =====================================================
-- STEP 3: Rename primary key column back
-- =====================================================

ALTER TABLE machinestatus
CHANGE COLUMN machinestatusid pcstatusid INT(11) NOT NULL AUTO_INCREMENT;

-- =====================================================
-- STEP 4: Rename table back
-- =====================================================

RENAME TABLE machinestatus TO pcstatus;

-- =====================================================
-- VERIFICATION
-- =====================================================

SELECT '✓ Table renamed back to pcstatus' AS status;
DESCRIBE pcstatus;

SELECT '✓ Status values:' AS info;
SELECT * FROM pcstatus ORDER BY pcstatusid;

SELECT '✓ ROLLBACK 07 completed successfully' AS status;

SET SQL_SAFE_UPDATES = 1;

-- =====================================================
-- NOTES
-- =====================================================
-- Table renamed: machinestatus → pcstatus
-- Column renamed: machinestatusid → pcstatusid
-- Column renamed: machinestatus → pcstatus
-- FK constraint removed from machines table
--
-- To restore: Run 07_rename_pcstatus_to_machinestatus.sql
-- =====================================================
