Add webhook logging, pagination, middleware, migrations, and prod hardening
- Webhook event logging repo and subscription webhook idempotency - Pagination helper (echohelpers) with cursor/offset support - Request ID and structured logging middleware - Push client improvements (FCM HTTP v1, better error handling) - Task model version column, business constraint migrations, targeted indexes - Expanded categorization chain tests - Email service and config hardening - CI workflow updates, .gitignore additions, .env.example updates Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1
migrations/000012_webhook_event_log.down.sql
Normal file
1
migrations/000012_webhook_event_log.down.sql
Normal file
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS webhook_event_log;
|
||||
9
migrations/000012_webhook_event_log.up.sql
Normal file
9
migrations/000012_webhook_event_log.up.sql
Normal file
@@ -0,0 +1,9 @@
|
||||
CREATE TABLE IF NOT EXISTS webhook_event_log (
|
||||
id SERIAL PRIMARY KEY,
|
||||
event_id VARCHAR(255) NOT NULL,
|
||||
provider VARCHAR(20) NOT NULL,
|
||||
event_type VARCHAR(100) NOT NULL,
|
||||
processed_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
payload_hash VARCHAR(64),
|
||||
UNIQUE(provider, event_id)
|
||||
);
|
||||
5
migrations/000013_business_constraints.down.sql
Normal file
5
migrations/000013_business_constraints.down.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
ALTER TABLE notifications_notificationpreference DROP CONSTRAINT IF EXISTS uq_notif_pref_user;
|
||||
ALTER TABLE subscriptions_usersubscription DROP CONSTRAINT IF EXISTS uq_subscription_user;
|
||||
ALTER TABLE notifications_notification DROP CONSTRAINT IF EXISTS chk_notification_sent_consistency;
|
||||
ALTER TABLE subscriptions_usersubscription DROP CONSTRAINT IF EXISTS chk_subscription_tier;
|
||||
ALTER TABLE task_task DROP CONSTRAINT IF EXISTS chk_task_not_cancelled_and_archived;
|
||||
19
migrations/000013_business_constraints.up.sql
Normal file
19
migrations/000013_business_constraints.up.sql
Normal file
@@ -0,0 +1,19 @@
|
||||
-- Prevent task from being both cancelled and archived simultaneously
|
||||
ALTER TABLE task_task ADD CONSTRAINT chk_task_not_cancelled_and_archived
|
||||
CHECK (NOT (is_cancelled = true AND is_archived = true));
|
||||
|
||||
-- Subscription tier must be a valid value
|
||||
ALTER TABLE subscriptions_usersubscription ADD CONSTRAINT chk_subscription_tier
|
||||
CHECK (tier IN ('free', 'pro'));
|
||||
|
||||
-- Notification: sent_at must be set when sent is true
|
||||
ALTER TABLE notifications_notification ADD CONSTRAINT chk_notification_sent_consistency
|
||||
CHECK ((sent = false) OR (sent = true AND sent_at IS NOT NULL));
|
||||
|
||||
-- One subscription per user
|
||||
ALTER TABLE subscriptions_usersubscription ADD CONSTRAINT uq_subscription_user
|
||||
UNIQUE (user_id);
|
||||
|
||||
-- One notification preference per user
|
||||
ALTER TABLE notifications_notificationpreference ADD CONSTRAINT uq_notif_pref_user
|
||||
UNIQUE (user_id);
|
||||
1
migrations/000014_task_version_column.down.sql
Normal file
1
migrations/000014_task_version_column.down.sql
Normal file
@@ -0,0 +1 @@
|
||||
ALTER TABLE task_task DROP COLUMN IF EXISTS version;
|
||||
1
migrations/000014_task_version_column.up.sql
Normal file
1
migrations/000014_task_version_column.up.sql
Normal file
@@ -0,0 +1 @@
|
||||
ALTER TABLE task_task ADD COLUMN IF NOT EXISTS version INTEGER NOT NULL DEFAULT 1;
|
||||
3
migrations/000015_targeted_indexes.down.sql
Normal file
3
migrations/000015_targeted_indexes.down.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
DROP INDEX IF EXISTS idx_document_residence_active;
|
||||
DROP INDEX IF EXISTS idx_notification_user_unread;
|
||||
DROP INDEX IF EXISTS idx_task_kanban_query;
|
||||
14
migrations/000015_targeted_indexes.up.sql
Normal file
14
migrations/000015_targeted_indexes.up.sql
Normal file
@@ -0,0 +1,14 @@
|
||||
-- Kanban: composite partial index for active task queries by residence with due date ordering
|
||||
CREATE INDEX IF NOT EXISTS idx_task_kanban_query
|
||||
ON task_task (residence_id, next_due_date, due_date)
|
||||
WHERE is_cancelled = false AND is_archived = false;
|
||||
|
||||
-- Notifications: partial index for unread count (hot query)
|
||||
CREATE INDEX IF NOT EXISTS idx_notification_user_unread
|
||||
ON notifications_notification (user_id, read)
|
||||
WHERE read = false;
|
||||
|
||||
-- Documents: partial index for active documents by residence
|
||||
CREATE INDEX IF NOT EXISTS idx_document_residence_active
|
||||
ON documents_document (residence_id, is_active)
|
||||
WHERE is_active = true;
|
||||
1
migrations/012_webhook_event_log.down.sql
Normal file
1
migrations/012_webhook_event_log.down.sql
Normal file
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS webhook_event_log;
|
||||
9
migrations/012_webhook_event_log.up.sql
Normal file
9
migrations/012_webhook_event_log.up.sql
Normal file
@@ -0,0 +1,9 @@
|
||||
CREATE TABLE IF NOT EXISTS webhook_event_log (
|
||||
id SERIAL PRIMARY KEY,
|
||||
event_id VARCHAR(255) NOT NULL,
|
||||
provider VARCHAR(20) NOT NULL,
|
||||
event_type VARCHAR(100) NOT NULL,
|
||||
processed_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
payload_hash VARCHAR(64),
|
||||
UNIQUE(provider, event_id)
|
||||
);
|
||||
5
migrations/013_business_constraints.down.sql
Normal file
5
migrations/013_business_constraints.down.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
ALTER TABLE task_task DROP CONSTRAINT IF EXISTS chk_task_not_cancelled_and_archived;
|
||||
ALTER TABLE subscriptions_usersubscription DROP CONSTRAINT IF EXISTS chk_subscription_tier;
|
||||
ALTER TABLE notifications_notification DROP CONSTRAINT IF EXISTS chk_notification_sent_consistency;
|
||||
ALTER TABLE subscriptions_usersubscription DROP CONSTRAINT IF EXISTS uq_subscription_user;
|
||||
ALTER TABLE notifications_notificationpreference DROP CONSTRAINT IF EXISTS uq_notif_pref_user;
|
||||
31
migrations/013_business_constraints.up.sql
Normal file
31
migrations/013_business_constraints.up.sql
Normal file
@@ -0,0 +1,31 @@
|
||||
-- Prevent task from being both cancelled and archived simultaneously
|
||||
ALTER TABLE task_task ADD CONSTRAINT chk_task_not_cancelled_and_archived
|
||||
CHECK (NOT (is_cancelled = true AND is_archived = true));
|
||||
|
||||
-- Subscription tier must be valid
|
||||
ALTER TABLE subscriptions_usersubscription ADD CONSTRAINT chk_subscription_tier
|
||||
CHECK (tier IN ('free', 'pro'));
|
||||
|
||||
-- Notification: sent_at must be set if sent is true
|
||||
ALTER TABLE notifications_notification ADD CONSTRAINT chk_notification_sent_consistency
|
||||
CHECK ((sent = false) OR (sent = true AND sent_at IS NOT NULL));
|
||||
|
||||
-- One subscription per user
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM pg_constraint WHERE conname = 'uq_subscription_user'
|
||||
) THEN
|
||||
ALTER TABLE subscriptions_usersubscription ADD CONSTRAINT uq_subscription_user UNIQUE (user_id);
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- One notification preference per user
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM pg_constraint WHERE conname = 'uq_notif_pref_user'
|
||||
) THEN
|
||||
ALTER TABLE notifications_notificationpreference ADD CONSTRAINT uq_notif_pref_user UNIQUE (user_id);
|
||||
END IF;
|
||||
END $$;
|
||||
1
migrations/014_task_version_column.down.sql
Normal file
1
migrations/014_task_version_column.down.sql
Normal file
@@ -0,0 +1 @@
|
||||
ALTER TABLE task_task DROP COLUMN IF EXISTS version;
|
||||
1
migrations/014_task_version_column.up.sql
Normal file
1
migrations/014_task_version_column.up.sql
Normal file
@@ -0,0 +1 @@
|
||||
ALTER TABLE task_task ADD COLUMN IF NOT EXISTS version INTEGER NOT NULL DEFAULT 1;
|
||||
3
migrations/015_targeted_indexes.down.sql
Normal file
3
migrations/015_targeted_indexes.down.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
DROP INDEX IF EXISTS idx_task_kanban_query;
|
||||
DROP INDEX IF EXISTS idx_notification_user_unread;
|
||||
DROP INDEX IF EXISTS idx_document_residence_active;
|
||||
14
migrations/015_targeted_indexes.up.sql
Normal file
14
migrations/015_targeted_indexes.up.sql
Normal file
@@ -0,0 +1,14 @@
|
||||
-- Kanban: composite index for active task queries by residence with due date ordering
|
||||
CREATE INDEX IF NOT EXISTS idx_task_kanban_query
|
||||
ON task_task (residence_id, is_cancelled, is_archived, next_due_date, due_date)
|
||||
WHERE is_cancelled = false AND is_archived = false;
|
||||
|
||||
-- Notifications: index for unread count (hot query)
|
||||
CREATE INDEX IF NOT EXISTS idx_notification_user_unread
|
||||
ON notifications_notification (user_id, read)
|
||||
WHERE read = false;
|
||||
|
||||
-- Documents: residence + active filter
|
||||
CREATE INDEX IF NOT EXISTS idx_document_residence_active
|
||||
ON documents_document (residence_id, is_active)
|
||||
WHERE is_active = true;
|
||||
Reference in New Issue
Block a user