-- Drop unused BOM storage introduced earlier but never adopted in runtime code.

SET @fk_cfg_bom_exists := (
    SELECT COUNT(*)
    FROM information_schema.REFERENTIAL_CONSTRAINTS
    WHERE CONSTRAINT_SCHEMA = DATABASE()
      AND CONSTRAINT_NAME = 'fk_qt_configurations_bom'
);
SET @fk_cfg_bom_sql := IF(
    @fk_cfg_bom_exists > 0,
    'ALTER TABLE qt_configurations DROP FOREIGN KEY fk_qt_configurations_bom',
    'SELECT 1'
);
PREPARE stmt_drop_cfg_bom_fk FROM @fk_cfg_bom_sql;
EXECUTE stmt_drop_cfg_bom_fk;
DEALLOCATE PREPARE stmt_drop_cfg_bom_fk;

SET @idx_cfg_bom_exists := (
    SELECT COUNT(*)
    FROM information_schema.STATISTICS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'qt_configurations'
      AND INDEX_NAME = 'idx_qt_configurations_bom_id'
);
SET @idx_cfg_bom_sql := IF(
    @idx_cfg_bom_exists > 0,
    'ALTER TABLE qt_configurations DROP INDEX idx_qt_configurations_bom_id',
    'SELECT 1'
);
PREPARE stmt_drop_cfg_bom_idx FROM @idx_cfg_bom_sql;
EXECUTE stmt_drop_cfg_bom_idx;
DEALLOCATE PREPARE stmt_drop_cfg_bom_idx;

SET @col_cfg_bom_exists := (
    SELECT COUNT(*)
    FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'qt_configurations'
      AND COLUMN_NAME = 'bom_id'
);
SET @col_cfg_bom_sql := IF(
    @col_cfg_bom_exists > 0,
    'ALTER TABLE qt_configurations DROP COLUMN bom_id',
    'SELECT 1'
);
PREPARE stmt_drop_cfg_bom_col FROM @col_cfg_bom_sql;
EXECUTE stmt_drop_cfg_bom_col;
DEALLOCATE PREPARE stmt_drop_cfg_bom_col;

DROP TABLE IF EXISTS qt_bom;
