35 lines
3.3 KiB
Go
35 lines
3.3 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"pageswiki/internal/db"
|
|
)
|
|
|
|
// Preserve keys, subscriptions and ambiguous deliveries for full import, but
|
|
// do not install an enqueue trigger or enable external Push delivery yet.
|
|
func (s *Store) ensurePostgreSQLPushStorage(ctx context.Context) error {
|
|
return db.ApplyPostgreSQLMigrations(ctx, s.DB, "store-notification-push-storage", []db.PostgreSQLMigration{{Version: 1, Name: "retained push state with delivery disabled", Statements: []string{
|
|
`CREATE TABLE notification_push_keys(id BIGINT PRIMARY KEY CHECK(id=1),private_key TEXT NOT NULL,public_key TEXT NOT NULL)`,
|
|
`CREATE TABLE notification_push_subscriptions(id TEXT COLLATE "C" PRIMARY KEY,domain_uuid TEXT COLLATE "C" NOT NULL,owner_uuid TEXT COLLATE "C" NOT NULL,session_hash TEXT NOT NULL,subscription TEXT NOT NULL,expires_at_ms BIGINT NOT NULL,foreground_until_ms BIGINT NOT NULL DEFAULT 0)`,
|
|
`CREATE INDEX notification_push_owner ON notification_push_subscriptions(domain_uuid,owner_uuid)`,
|
|
`CREATE INDEX notification_push_expiry ON notification_push_subscriptions(expires_at_ms)`,
|
|
`CREATE TABLE notification_push_queue(subscription_id TEXT COLLATE "C" NOT NULL,notification_uuid TEXT COLLATE "C" NOT NULL,created_at_ms BIGINT NOT NULL,next_at_ms BIGINT NOT NULL,attempts BIGINT NOT NULL DEFAULT 0,lease_until_ms BIGINT NOT NULL DEFAULT 0,PRIMARY KEY(subscription_id,notification_uuid))`,
|
|
`CREATE INDEX notification_push_due ON notification_push_queue(next_at_ms,lease_until_ms)`,
|
|
`CREATE INDEX notification_push_age ON notification_push_queue(created_at_ms)`,
|
|
}, Assertions: []string{
|
|
`SELECT id,private_key,public_key FROM notification_push_keys WHERE 1=0`,
|
|
`SELECT id,domain_uuid,owner_uuid,session_hash,subscription,expires_at_ms,foreground_until_ms FROM notification_push_subscriptions WHERE 1=0`,
|
|
`SELECT subscription_id,notification_uuid,created_at_ms,next_at_ms,attempts,lease_until_ms FROM notification_push_queue WHERE 1=0`,
|
|
}}})
|
|
}
|
|
|
|
// Inbox storage only. Push subscriptions, delivery leases and event registration
|
|
// remain separate ports and do not become available merely by creating this table.
|
|
func (s *Store) ensurePostgreSQLNotificationInbox(ctx context.Context) error {
|
|
return db.ApplyPostgreSQLMigrations(ctx, s.DB, "store-notification-inbox", []db.PostgreSQLMigration{{Version: 1, Name: "durable notification inbox", Statements: []string{
|
|
`CREATE TABLE notification_inbox(uuid TEXT COLLATE "C" PRIMARY KEY,domain_uuid TEXT COLLATE "C" NOT NULL,recipient_uuid TEXT COLLATE "C" NOT NULL,event_uuid TEXT COLLATE "C" NOT NULL DEFAULT '',kind TEXT COLLATE "C" NOT NULL,status TEXT COLLATE "C" NOT NULL DEFAULT 'unread',request_uuid TEXT COLLATE "C" NOT NULL DEFAULT '',href TEXT NOT NULL DEFAULT '',message TEXT NOT NULL DEFAULT '',created_at_ms BIGINT NOT NULL,read_at_ms BIGINT NOT NULL DEFAULT 0,UNIQUE(domain_uuid,recipient_uuid,event_uuid,kind))`,
|
|
`CREATE INDEX idx_notification_recipient ON notification_inbox(domain_uuid,recipient_uuid,status,created_at_ms DESC,uuid DESC)`,
|
|
`CREATE INDEX idx_notification_timeline ON notification_inbox(domain_uuid,recipient_uuid,created_at_ms DESC,uuid DESC)`,
|
|
}, Assertions: []string{`SELECT uuid,domain_uuid,recipient_uuid,event_uuid,kind,status,request_uuid,href,message,created_at_ms,read_at_ms FROM notification_inbox WHERE 1=0`}}})
|
|
}
|