-- Renames job_alerts.client_id to alert_id.
--
-- Run this ONLY if the database already exists and still has `client_id`. A
-- database built fresh from schema.sql already has `alert_id` and must not be
-- run through this. Equivalent to the migration
-- 2026_08_01_000005_rename_client_id_to_alert_id_on_job_alerts_table.php · use
-- whichever suits: `php artisan migrate --force` if you have CLI access, or this
-- file in phpMyAdmin if you do not. Do not run both.
--
-- Check first. This should return exactly one row, named client_id:
--
--   SELECT column_name, column_type FROM information_schema.columns
--   WHERE table_schema = DATABASE() AND table_name = 'job_alerts'
--     AND column_name IN ('client_id', 'alert_id');
--
-- Back the table up before running it:
--
--   CREATE TABLE job_alerts_backup_20260801 AS SELECT * FROM job_alerts;

-- 1. The unique key names the old column, so it goes first.
ALTER TABLE `job_alerts`
  DROP INDEX `job_alerts_device_id_client_id_unique`;

-- 2. CHANGE, not ADD plus DROP · this keeps every value already in the column.
--    VARCHAR(64) rather than CHAR(36) because the app may send an id that is not
--    a UUID, and CHAR would pad anything shorter with spaces.
ALTER TABLE `job_alerts`
  CHANGE `client_id` `alert_id` VARCHAR(64) NULL DEFAULT NULL;

-- 3. Still unique per device, so a retried create updates the same row. Two
--    different devices are free to reuse the same alert id.
ALTER TABLE `job_alerts`
  ADD UNIQUE `job_alerts_device_id_alert_id_unique` (`device_id`, `alert_id`);

-- Verify:
--   SHOW CREATE TABLE `job_alerts`;
--   SELECT id, device_id, alert_id, name FROM job_alerts LIMIT 10;
--
-- Rolling back narrows the column to CHAR(36) and will truncate anything longer.
-- Check before you do it:
--   SELECT COUNT(*) FROM job_alerts WHERE CHAR_LENGTH(alert_id) > 36;
