-- ==============================================================================
-- Smart-alert columns on notifications.
--
-- smart_alert_key + the (user_id, smart_alert_key) unique index are what make
-- the alert generators idempotent: re-running app:alerts:generate on the same
-- day inserts nothing, so a cron that fires twice cannot flood a doctor with
-- duplicate reminders. NULL for ordinary application notifications, and MySQL
-- treats every NULL in a unique index as distinct, so existing rows are
-- unaffected and never deduplicated against each other.
-- ==============================================================================

ALTER TABLE `notifications`
    ADD COLUMN `smart_alert_key` VARCHAR(100) DEFAULT NULL,
    ADD COLUMN `payload` JSON DEFAULT NULL;

ALTER TABLE `notifications`
    ADD UNIQUE INDEX `uniq_notification_user_smart_alert_key` (`user_id`, `smart_alert_key`);
