visionA/visionA-backend/migrations/0005_create_agents.down.sql
jim800121chen 8369cab85c feat(db): migration 0005 agents 模型(個人設備管理 A' 走向第二階段地基)
- 新增 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>
2026-07-16 04:05:53 +08:00

23 lines
1.2 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 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隨欄位存在先於欄位 DROPIF 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;