- 新增 agents 表 + devices 加 4 欄(agent_id/agent_local_device_id/ registered_at/is_representative)+ 2 index + 純 SQL data migration - data migration 採 R-A:agent.id := device.id 決定性推導、冪等、 soft-deleted device 排除(WHERE deleted_at IS NULL) - 守住 ADR-018 走向 A':不碰 session_tokens FK、不改現有 partial unique index uq_devices_owner_serial_active、對 0001-0003 零破壞 - migrate_0005_db_test.go 6 個 dbtest case(apply/data migration/ rollback 對稱/re-apply 冪等/session_tokens 零影響/既有 device 讀寫回歸) - 修 TestMigrate_UpDownUp 編號 gap 假設(0001/2/3/5 無 0004): assert.Equal(topVer-1) → assert.Less(downVer, topVer) - .gitignore 加 .logs/ + **/.logs/(本機執行 log per-branch 不進 git) Reviewer 通過(0 Critical/0 Major/3 Minor/4 Sug)。全 db package 41 dbtest 130 綠、build/vet/test 綠、gitleaks 0。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
23 lines
1.2 KiB
SQL
23 lines
1.2 KiB
SQL
-- 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;
|