-- Adds an optional "last contacted" date to contacts. Idempotent: safe to re-run.
-- If the table is busy, this gives up after 15 seconds (and can simply be run again) instead of waiting and holding up the app.
SET SESSION lock_wait_timeout = 15;
SET @col_exists = (
    SELECT COUNT(*) FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'dim_contacts' AND COLUMN_NAME = 'last_contacted_at'
);
SET @sql = IF(@col_exists = 0,
    'ALTER TABLE dim_contacts ADD COLUMN last_contacted_at DATE NULL',
    'SELECT 1');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
