-- Metryki konwersji i ROAS dla historii Facebook Ads

SET @sql = IF(
  EXISTS (
    SELECT 1
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'facebook_campaigns_history'
      AND COLUMN_NAME = 'conversion_value'
  ),
  'DO 1',
  'ALTER TABLE `facebook_campaigns_history` ADD COLUMN `conversion_value` DECIMAL(20,6) NOT NULL DEFAULT 0.000000 AFTER `cpc`'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
  EXISTS (
    SELECT 1
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'facebook_campaigns_history'
      AND COLUMN_NAME = 'roas'
  ),
  'DO 1',
  'ALTER TABLE `facebook_campaigns_history` ADD COLUMN `roas` DECIMAL(20,6) NOT NULL DEFAULT 0.000000 AFTER `conversion_value`'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
  EXISTS (
    SELECT 1
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'facebook_ad_sets_history'
      AND COLUMN_NAME = 'conversion_value'
  ),
  'DO 1',
  'ALTER TABLE `facebook_ad_sets_history` ADD COLUMN `conversion_value` DECIMAL(20,6) NOT NULL DEFAULT 0.000000 AFTER `cpc`'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
  EXISTS (
    SELECT 1
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'facebook_ad_sets_history'
      AND COLUMN_NAME = 'roas'
  ),
  'DO 1',
  'ALTER TABLE `facebook_ad_sets_history` ADD COLUMN `roas` DECIMAL(20,6) NOT NULL DEFAULT 0.000000 AFTER `conversion_value`'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
  EXISTS (
    SELECT 1
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'facebook_ads_history'
      AND COLUMN_NAME = 'conversion_value'
  ),
  'DO 1',
  'ALTER TABLE `facebook_ads_history` ADD COLUMN `conversion_value` DECIMAL(20,6) NOT NULL DEFAULT 0.000000 AFTER `cpc`'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
  EXISTS (
    SELECT 1
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = 'facebook_ads_history'
      AND COLUMN_NAME = 'roas'
  ),
  'DO 1',
  'ALTER TABLE `facebook_ads_history` ADD COLUMN `roas` DECIMAL(20,6) NOT NULL DEFAULT 0.000000 AFTER `conversion_value`'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
