🗃️ db: add notifications migration with idempotent clauses

This commit is contained in:
YuTengjing
2026-03-24 17:07:44 +08:00
parent acb5c0498d
commit 728659e3fb
4 changed files with 13218 additions and 0 deletions

View File

@@ -907,6 +907,43 @@ table nextauth_verificationtokens {
}
}
table notification_deliveries {
id text [pk, not null]
notification_id text [not null]
channel text [not null]
status text [not null]
provider_message_id text
failed_reason text
sent_at "timestamp with time zone"
created_at "timestamp with time zone" [not null, default: `now()`]
indexes {
notification_id [name: 'idx_deliveries_notification']
}
}
table notifications {
id text [pk, not null]
user_id text [not null]
category text [not null]
type text [not null]
content_key text [not null]
content_params jsonb
content text
dedupe_key text
action_url text
is_read boolean [not null, default: false]
is_archived boolean [not null, default: false]
created_at "timestamp with time zone" [not null, default: `now()`]
updated_at "timestamp with time zone" [not null, default: `now()`]
indexes {
(user_id, created_at) [name: 'idx_notifications_user_active']
user_id [name: 'idx_notifications_user_unread']
(user_id, dedupe_key) [name: 'idx_notifications_dedupe', unique]
}
}
table oauth_handoffs {
id text [pk, not null]
client varchar(50) [not null]
@@ -1463,6 +1500,7 @@ table user_settings {
memory jsonb
tool jsonb
image jsonb
notification jsonb
}
table users {

View File

@@ -0,0 +1,36 @@
CREATE TABLE IF NOT EXISTS "notification_deliveries" (
"id" text PRIMARY KEY NOT NULL,
"notification_id" text NOT NULL,
"channel" text NOT NULL,
"status" text NOT NULL,
"provider_message_id" text,
"failed_reason" text,
"sent_at" timestamp with time zone,
"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "notifications" (
"id" text PRIMARY KEY NOT NULL,
"user_id" text NOT NULL,
"category" text NOT NULL,
"type" text NOT NULL,
"content_key" text NOT NULL,
"content_params" jsonb,
"content" text,
"dedupe_key" text,
"action_url" text,
"is_read" boolean DEFAULT false NOT NULL,
"is_archived" boolean DEFAULT false NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "user_settings" ADD COLUMN IF NOT EXISTS "notification" jsonb;--> statement-breakpoint
ALTER TABLE "notification_deliveries" DROP CONSTRAINT IF EXISTS "notification_deliveries_notification_id_notifications_id_fk";
ALTER TABLE "notification_deliveries" ADD CONSTRAINT "notification_deliveries_notification_id_notifications_id_fk" FOREIGN KEY ("notification_id") REFERENCES "public"."notifications"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "notifications" DROP CONSTRAINT IF EXISTS "notifications_user_id_users_id_fk";
ALTER TABLE "notifications" ADD CONSTRAINT "notifications_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "idx_deliveries_notification" ON "notification_deliveries" USING btree ("notification_id");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "idx_notifications_user_active" ON "notifications" USING btree ("user_id","created_at") WHERE "notifications"."is_archived" = false;--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "idx_notifications_user_unread" ON "notifications" USING btree ("user_id") WHERE "notifications"."is_read" = false AND "notifications"."is_archived" = false;--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "idx_notifications_dedupe" ON "notifications" USING btree ("user_id","dedupe_key");

File diff suppressed because it is too large Load Diff

View File

@@ -665,6 +665,13 @@
"when": 1773764776073,
"tag": "0094_agent_bot_providers_add_settings",
"breakpoints": true
},
{
"idx": 95,
"version": "7",
"when": 1774343178827,
"tag": "0095_add_notifications",
"breakpoints": true
}
],
"version": "6"