-- Powiazanie alertow z konkretnym produktem

SET @sql = IF(
  EXISTS (
    SELECT 1
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'campaign_alerts'
      AND COLUMN_NAME = 'product_id'
  ),
  'DO 1',
  'ALTER TABLE `campaign_alerts` ADD COLUMN `product_id` INT(11) NULL DEFAULT NULL AFTER `ad_group_external_id`'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
  EXISTS (
    SELECT 1
    FROM INFORMATION_SCHEMA.STATISTICS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'campaign_alerts'
      AND INDEX_NAME = 'idx_alert_product'
  ),
  'DO 1',
  'CREATE INDEX `idx_alert_product` ON `campaign_alerts` (`product_id`)'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
