Utopia-Canteen-System/scripts/mysql_migrate_remove_device...

33 lines
1.2 KiB
SQL

-- Migration: remove device_local_row_id, rename columns, and update unique index.
-- Run this on hrms (production) before deploying the app that uses new column names.
-- Production still has: unique index (device_id, device_local_row_id) and column device_local_row_id.
--
-- Order: 1) Drop unique index 2) Drop column 3) Rename scan_time_utc 4) Rename received_at_utc 5) Add new unique key
USE hrms;
-- 1) Drop the unique index on (device_id, device_local_row_id)
ALTER TABLE lunch_order_transactions
DROP INDEX ux_lunch_order_tx_device_device_local_row_id;
-- 2) Drop the column
ALTER TABLE lunch_order_transactions
DROP COLUMN device_local_row_id;
-- 3) Rename scan_time_utc → scan_date
ALTER TABLE lunch_order_transactions
CHANGE COLUMN scan_time_utc scan_date DATETIME(3) NOT NULL;
-- 4) Rename received_at_utc → received_date
ALTER TABLE lunch_order_transactions
CHANGE COLUMN received_at_utc received_date DATETIME(3) NOT NULL DEFAULT (UTC_TIMESTAMP(3));
-- 5) Add new unique key so INSERT IGNORE still deduplicates (same site/device/card/time = one row)
ALTER TABLE lunch_order_transactions
ADD UNIQUE KEY ux_lunch_order_tx_site_device_card_scan_date (
site_id,
device_id,
card_id,
scan_date
);