-- 0005_create_agents.down.sql -- -- 反向 0005:移除 devices 新欄位與 index、DROP agents 表。 -- 對稱性:agents 表整個 DROP,故 (3) data migration 建的 agent row 無殘留、無需個別還原。 -- devices 的 agent_id 欄 DROP 後,data migration 寫進去的值一併消失,回到 0004 後狀態。 -- -- 順序:先 DROP devices 上參照 agents 的 FK 欄(agent_id),才能 DROP agents 表 -- (否則 agents 被 devices.agent_id FK 參照、DROP TABLE agents 會失敗)。 -- devices 新增 index(隨欄位存在,先於欄位 DROP;IF EXISTS 保冪等)。 DROP INDEX IF EXISTS idx_devices_registered; DROP INDEX IF EXISTS idx_devices_agent_active; -- devices 新增欄位(agent_id 內含 FK→agents,須先於 DROP TABLE agents 移除)。 ALTER TABLE devices DROP COLUMN IF EXISTS is_representative; ALTER TABLE devices DROP COLUMN IF EXISTS registered_at; ALTER TABLE devices DROP COLUMN IF EXISTS agent_local_device_id; ALTER TABLE devices DROP COLUMN IF EXISTS agent_id; -- agents 表(index idx_agents_owner_active 隨 table DROP 自動移除;顯式 DROP INDEX 保冪等)。 DROP INDEX IF EXISTS idx_agents_owner_active; DROP TABLE IF EXISTS agents;