Files
goclaw/migrations/000054_agent_hooks_name.up.sql
viettranx ee328d4266 refactor: consolidate hooks table migration and drop deprecated agent_id column
- Consolidated PG migration 054: agent_hooks rename + junction table + deprecated column drop
- Updated SQLite schema v19 with final consolidated migration
- Removed obsolete schema rebuild files (v21, v23)
- Updated Go store layer (pg/hooks.go, sqlitestore/hooks.go)
- Updated integration tests to use new table names (hooks, hook_agents)
- Updated TypeScript protocol, UI components, and i18n strings
- Updated gateway methods to reflect schema changes

Tables: agent_hooks → hooks, agent_hook_agents → hook_agents
2026-04-16 14:17:48 +07:00

42 lines
1.5 KiB
SQL

-- 000054 — Add `name` column, N:M junction, rename tables, drop agent_id.
--
-- 1) name: user-facing label. Nullable so existing rows don't break.
-- 2) Junction table replaces the 1:N agent_id FK with many-to-many.
-- 3) Data migration: copy existing agent_id values into junction.
-- 4) Rename agent_hooks → hooks, agent_hook_agents → hook_agents.
-- 5) Drop deprecated agent_id column from hooks.
-- 6) Recreate indexes with new table names.
-- Step 1: add name column
ALTER TABLE agent_hooks ADD COLUMN IF NOT EXISTS name VARCHAR(255);
-- Step 2: create junction table
CREATE TABLE IF NOT EXISTS agent_hook_agents (
hook_id UUID NOT NULL REFERENCES agent_hooks(id) ON DELETE CASCADE,
agent_id UUID NOT NULL REFERENCES agents(id) ON DELETE CASCADE,
PRIMARY KEY (hook_id, agent_id)
);
CREATE INDEX IF NOT EXISTS idx_hook_agents_agent
ON agent_hook_agents (agent_id);
-- Step 3: copy existing agent_id values into junction
INSERT INTO agent_hook_agents (hook_id, agent_id)
SELECT id, agent_id FROM agent_hooks
WHERE agent_id IS NOT NULL
ON CONFLICT DO NOTHING;
-- Step 4: rename tables
ALTER TABLE agent_hooks RENAME TO hooks;
ALTER TABLE agent_hook_agents RENAME TO hook_agents;
-- Step 5: drop deprecated column
ALTER TABLE hooks DROP COLUMN IF EXISTS agent_id;
-- Step 6: recreate indexes with new table/column names
DROP INDEX IF EXISTS idx_hooks_lookup;
CREATE INDEX idx_hooks_lookup ON hooks (tenant_id, event) WHERE enabled = TRUE;
DROP INDEX IF EXISTS idx_hook_agents_agent;
CREATE INDEX idx_hook_agents_agent ON hook_agents (agent_id);