From a0bea3604187ff627277f2a54c01cf63a490cfac Mon Sep 17 00:00:00 2001 From: zhenyi <434836402@qq.com> Date: Wed, 10 Jun 2026 18:53:49 +0800 Subject: [PATCH] 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 --- migrate/001_init.sql | 34 +--------------------- migrate/013_notification_extra_columns.sql | 24 +++++++++++++++ 2 files changed, 25 insertions(+), 33 deletions(-) create mode 100644 migrate/013_notification_extra_columns.sql diff --git a/migrate/001_init.sql b/migrate/001_init.sql index 8ba295a..904424e 100644 --- a/migrate/001_init.sql +++ b/migrate/001_init.sql @@ -1885,50 +1885,18 @@ CREATE TABLE IF NOT EXISTS notification ( user_id UUID NOT 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, - 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, title TEXT NOT NULL, body TEXT NULL, - target_type TEXT NULL, - target_id UUID NULL, - action_url TEXT NULL, - priority TEXT NOT NULL, read_at TIMESTAMPTZ NULL, dismissed_at TIMESTAMPTZ NULL, - metadata JSONB NULL, created_at TIMESTAMPTZ NOT NULL, - updated_at TIMESTAMPTZ NOT NULL, - deleted_at TIMESTAMPTZ NULL - + updated_at TIMESTAMPTZ NOT 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_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_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_deleted ON notification (deleted_at) WHERE deleted_at IS NOT NULL; -- models/agents/agent_feedback.rs → agent_feedback CREATE TABLE IF NOT EXISTS agent_feedback ( diff --git a/migrate/013_notification_extra_columns.sql b/migrate/013_notification_extra_columns.sql new file mode 100644 index 0000000..c7f8b10 --- /dev/null +++ b/migrate/013_notification_extra_columns.sql @@ -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;