-- Add gad_source and gad_campaignid columns to call_clicks.
-- These fields are sent by tracking.js for Google Ads traffic
-- (gad_source = Google Ads traffic source indicator, gad_campaignid = campaign ID).
-- Compatible with older MySQL/MariaDB that do not support ADD COLUMN IF NOT EXISTS.

SET @db := DATABASE();

SET @sql := IF(
    (SELECT COUNT(*) FROM information_schema.COLUMNS
     WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'call_clicks' AND COLUMN_NAME = 'gad_source') = 0,
    'ALTER TABLE call_clicks ADD COLUMN gad_source VARCHAR(64) NULL AFTER wbraid',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql := IF(
    (SELECT COUNT(*) FROM information_schema.COLUMNS
     WHERE TABLE_SCHEMA = @db AND TABLE_NAME = 'call_clicks' AND COLUMN_NAME = 'gad_campaignid') = 0,
    'ALTER TABLE call_clicks ADD COLUMN gad_campaignid VARCHAR(64) NULL AFTER gad_source',
    'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
