-- =====================================================
-- ROLLBACK 01: Remove appversions table
-- =====================================================
-- Date: 2025-11-25
-- Purpose: Rollback script for 01_create_appversions_table.sql
-- WARNING: This will delete all version data!
-- =====================================================

USE shopdb;
SET SQL_SAFE_UPDATES = 0;

-- Must remove FK from installedapps first (if it exists)
SET @fk_exists = (
    SELECT COUNT(*)
    FROM information_schema.TABLE_CONSTRAINTS
    WHERE TABLE_SCHEMA = 'shopdb'
    AND TABLE_NAME = 'installedapps'
    AND CONSTRAINT_NAME = 'fk_installedapps_appversionid'
);

SET @sql = IF(@fk_exists > 0,
    'ALTER TABLE installedapps DROP FOREIGN KEY fk_installedapps_appversionid',
    'SELECT "FK does not exist" AS status'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Drop appversions table
DROP TABLE IF EXISTS appversions;

SELECT '✓ appversions table dropped' AS status;

SET SQL_SAFE_UPDATES = 1;
