-- Migracja: rozbicie statystyk produktow per kampania / grupa reklam
-- Data: 2026-02-18
-- Uwaga: products_data pozostaje globalne per product_id (bez podzialu na scope)

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

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

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

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

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

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

-- products_history: indeksy scope + dzien
SET @sql = IF(
  EXISTS (
    SELECT 1
    FROM INFORMATION_SCHEMA.STATISTICS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'products_history'
      AND INDEX_NAME = 'idx_products_history_campaign_id'
  ),
  'DO 1',
  'ALTER TABLE `products_history` ADD INDEX `idx_products_history_campaign_id` (`campaign_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 = 'products_history'
      AND INDEX_NAME = 'idx_products_history_ad_group_id'
  ),
  'DO 1',
  'ALTER TABLE `products_history` ADD INDEX `idx_products_history_ad_group_id` (`ad_group_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 = 'products_history'
      AND INDEX_NAME = 'uk_products_history_scope_day'
  ),
  'DO 1',
  'ALTER TABLE `products_history` ADD UNIQUE INDEX `uk_products_history_scope_day` (`product_id`, `campaign_id`, `ad_group_id`, `date_add`)'
);
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 = 'products_history_30'
      AND INDEX_NAME = 'idx_products_history_30_campaign_id'
  ),
  'DO 1',
  'ALTER TABLE `products_history_30` ADD INDEX `idx_products_history_30_campaign_id` (`campaign_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 = 'products_history_30'
      AND INDEX_NAME = 'idx_products_history_30_ad_group_id'
  ),
  'DO 1',
  'ALTER TABLE `products_history_30` ADD INDEX `idx_products_history_30_ad_group_id` (`ad_group_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 = 'products_history_30'
      AND INDEX_NAME = 'uk_products_history_30_scope_day'
  ),
  'DO 1',
  'ALTER TABLE `products_history_30` ADD UNIQUE INDEX `uk_products_history_30_scope_day` (`product_id`, `campaign_id`, `ad_group_id`, `date_add`)'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- products_temp: indeksy scope
SET @sql = IF(
  EXISTS (
    SELECT 1
    FROM INFORMATION_SCHEMA.STATISTICS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'products_temp'
      AND INDEX_NAME = 'idx_products_temp_campaign_id'
  ),
  'DO 1',
  'ALTER TABLE `products_temp` ADD INDEX `idx_products_temp_campaign_id` (`campaign_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 = 'products_temp'
      AND INDEX_NAME = 'idx_products_temp_ad_group_id'
  ),
  'DO 1',
  'ALTER TABLE `products_temp` ADD INDEX `idx_products_temp_ad_group_id` (`ad_group_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 = 'products_temp'
      AND INDEX_NAME = 'uk_products_temp_scope'
  ),
  'DO 1',
  'ALTER TABLE `products_temp` ADD UNIQUE INDEX `uk_products_temp_scope` (`product_id`, `campaign_id`, `ad_group_id`)'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
