fix(db): split notification table DDL to support existing databases

- Remove extended columns (repo_id, issue_id, pull_request_id,
  channel_id, message_id, target_type, target_id, action_url,
  priority, metadata, deleted_at) from CREATE TABLE in 001_init.sql
- Add migration 013_notification_extra_columns.sql that uses
  ALTER TABLE ADD COLUMN IF NOT EXISTS for all extended columns
- Move extended column indexes to the new migration
- Ensures compatibility with databases that already have a basic
  notification table from a previous schema version
This commit is contained in:
zhenyi
2026-06-10 18:53:49 +08:00
parent 63ca1151ae
commit a0bea36041
2 changed files with 25 additions and 33 deletions
+1 -33
View File
@@ -1885,50 +1885,18 @@ CREATE TABLE IF NOT EXISTS notification (
user_id UUID NOT NULL REFERENCES "user"(id) ON DELETE CASCADE, user_id UUID NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
actor_id UUID NULL REFERENCES "user"(id) ON DELETE CASCADE, actor_id UUID NULL REFERENCES "user"(id) ON DELETE CASCADE,
workspace_id UUID NULL REFERENCES workspace(id) ON DELETE CASCADE, workspace_id UUID NULL REFERENCES workspace(id) ON DELETE CASCADE,
repo_id UUID NULL REFERENCES repo(id) ON DELETE CASCADE,
issue_id UUID NULL REFERENCES issue(id) ON DELETE CASCADE,
pull_request_id UUID NULL REFERENCES pull_request(id) ON DELETE CASCADE,
channel_id UUID NULL REFERENCES channel(id) ON DELETE CASCADE,
message_id UUID NULL REFERENCES message(id) ON DELETE CASCADE,
notification_type TEXT NOT NULL, notification_type TEXT NOT NULL,
title TEXT NOT NULL, title TEXT NOT NULL,
body TEXT NULL, body TEXT NULL,
target_type TEXT NULL,
target_id UUID NULL,
action_url TEXT NULL,
priority TEXT NOT NULL,
read_at TIMESTAMPTZ NULL, read_at TIMESTAMPTZ NULL,
dismissed_at TIMESTAMPTZ NULL, dismissed_at TIMESTAMPTZ NULL,
metadata JSONB NULL,
created_at TIMESTAMPTZ NOT NULL, created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL, updated_at TIMESTAMPTZ NOT NULL
deleted_at TIMESTAMPTZ NULL
); );
ALTER TABLE notification ADD COLUMN IF NOT EXISTS repo_id UUID NULL REFERENCES repo(id) ON DELETE CASCADE;
ALTER TABLE notification ADD COLUMN IF NOT EXISTS issue_id UUID NULL REFERENCES issue(id) ON DELETE CASCADE;
ALTER TABLE notification ADD COLUMN IF NOT EXISTS pull_request_id UUID NULL REFERENCES pull_request(id) ON DELETE CASCADE;
ALTER TABLE notification ADD COLUMN IF NOT EXISTS channel_id UUID NULL REFERENCES channel(id) ON DELETE CASCADE;
ALTER TABLE notification ADD COLUMN IF NOT EXISTS message_id UUID NULL REFERENCES message(id) ON DELETE CASCADE;
ALTER TABLE notification ADD COLUMN IF NOT EXISTS target_type TEXT NULL;
ALTER TABLE notification ADD COLUMN IF NOT EXISTS target_id UUID NULL;
ALTER TABLE notification ADD COLUMN IF NOT EXISTS action_url TEXT NULL;
ALTER TABLE notification ADD COLUMN IF NOT EXISTS priority TEXT NOT NULL DEFAULT 'normal';
ALTER TABLE notification ADD COLUMN IF NOT EXISTS metadata JSONB NULL;
ALTER TABLE notification ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ NULL;
CREATE INDEX IF NOT EXISTS idx_notification_user_id ON notification (user_id); CREATE INDEX IF NOT EXISTS idx_notification_user_id ON notification (user_id);
CREATE INDEX IF NOT EXISTS idx_notification_actor_id ON notification (actor_id); CREATE INDEX IF NOT EXISTS idx_notification_actor_id ON notification (actor_id);
CREATE INDEX IF NOT EXISTS idx_notification_workspace_id ON notification (workspace_id); CREATE INDEX IF NOT EXISTS idx_notification_workspace_id ON notification (workspace_id);
CREATE INDEX IF NOT EXISTS idx_notification_repo_id ON notification (repo_id);
CREATE INDEX IF NOT EXISTS idx_notification_issue_id ON notification (issue_id);
CREATE INDEX IF NOT EXISTS idx_notification_pull_request_id ON notification (pull_request_id);
CREATE INDEX IF NOT EXISTS idx_notification_channel_id ON notification (channel_id);
CREATE INDEX IF NOT EXISTS idx_notification_message_id ON notification (message_id);
CREATE INDEX IF NOT EXISTS idx_notification_target_id ON notification (target_id);
CREATE INDEX IF NOT EXISTS idx_notification_repo_created ON notification (repo_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_notification_ws_created ON notification (workspace_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_notification_user_created ON notification (user_id, created_at DESC); CREATE INDEX IF NOT EXISTS idx_notification_user_created ON notification (user_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_notification_deleted ON notification (deleted_at) WHERE deleted_at IS NOT NULL;
-- models/agents/agent_feedback.rs → agent_feedback -- models/agents/agent_feedback.rs → agent_feedback
CREATE TABLE IF NOT EXISTS agent_feedback ( CREATE TABLE IF NOT EXISTS agent_feedback (
@@ -0,0 +1,24 @@
-- Migration: 013_notification_extra_columns.sql
-- Add extended columns to notification table for full feature support.
ALTER TABLE notification ADD COLUMN IF NOT EXISTS repo_id UUID NULL REFERENCES repo(id) ON DELETE CASCADE;
ALTER TABLE notification ADD COLUMN IF NOT EXISTS issue_id UUID NULL REFERENCES issue(id) ON DELETE CASCADE;
ALTER TABLE notification ADD COLUMN IF NOT EXISTS pull_request_id UUID NULL REFERENCES pull_request(id) ON DELETE CASCADE;
ALTER TABLE notification ADD COLUMN IF NOT EXISTS channel_id UUID NULL REFERENCES channel(id) ON DELETE CASCADE;
ALTER TABLE notification ADD COLUMN IF NOT EXISTS message_id UUID NULL REFERENCES message(id) ON DELETE CASCADE;
ALTER TABLE notification ADD COLUMN IF NOT EXISTS target_type TEXT NULL;
ALTER TABLE notification ADD COLUMN IF NOT EXISTS target_id UUID NULL;
ALTER TABLE notification ADD COLUMN IF NOT EXISTS action_url TEXT NULL;
ALTER TABLE notification ADD COLUMN IF NOT EXISTS priority TEXT NOT NULL DEFAULT 'normal';
ALTER TABLE notification ADD COLUMN IF NOT EXISTS metadata JSONB NULL;
ALTER TABLE notification ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ NULL;
CREATE INDEX IF NOT EXISTS idx_notification_repo_id ON notification (repo_id);
CREATE INDEX IF NOT EXISTS idx_notification_issue_id ON notification (issue_id);
CREATE INDEX IF NOT EXISTS idx_notification_pull_request_id ON notification (pull_request_id);
CREATE INDEX IF NOT EXISTS idx_notification_channel_id ON notification (channel_id);
CREATE INDEX IF NOT EXISTS idx_notification_message_id ON notification (message_id);
CREATE INDEX IF NOT EXISTS idx_notification_target_id ON notification (target_id);
CREATE INDEX IF NOT EXISTS idx_notification_repo_created ON notification (repo_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_notification_ws_created ON notification (workspace_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_notification_deleted ON notification (deleted_at) WHERE deleted_at IS NOT NULL;