diff --git a/apply-supplementary-indexes-migration.sql b/apply-supplementary-indexes-migration.sql index 6f531046..5a3d9d02 100644 --- a/apply-supplementary-indexes-migration.sql +++ b/apply-supplementary-indexes-migration.sql @@ -62,21 +62,10 @@ ON library."ItemValuesMap" ("ItemValueId", "ItemId"); -- 5. Index for user-specific activity queries -- Use case: "Show activity for user X" -- Improves queries like: "SELECT * FROM ActivityLogs WHERE UserId='user-id' ORDER BY DateCreated DESC" -DO $$ -BEGIN - IF EXISTS ( - SELECT 1 FROM information_schema.tables - WHERE table_schema = 'activitylog' - AND table_name = 'ActivityLogs' - ) THEN - RAISE NOTICE 'Creating activity log index...'; - CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_activitylogs_userid_datecreated - ON activitylog."ActivityLogs" ("UserId", "DateCreated" DESC) - WHERE "UserId" IS NOT NULL; - ELSE - RAISE NOTICE 'ActivityLogs table does not exist. Skipping activity log index.'; - END IF; -END $$; +-- Note: This will fail gracefully if the activitylog.ActivityLogs table doesn't exist +CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_activitylogs_userid_datecreated +ON activitylog."ActivityLogs" ("UserId", "DateCreated" DESC) +WHERE "UserId" IS NOT NULL; -- 6. Index for episode deduplication by PresentationUniqueKey -- Use case: Group and deduplicate episodes (used in GetItems queries) diff --git a/fix-activitylog-index.sql b/fix-activitylog-index.sql new file mode 100644 index 00000000..3e74067c --- /dev/null +++ b/fix-activitylog-index.sql @@ -0,0 +1,27 @@ +-- ================================================ +-- Fix for ActivityLogs Index +-- Run this separately to create the missing index +-- ================================================ +-- +-- This creates the ActivityLogs index that couldn't be created +-- from within the main migration script due to PostgreSQL +-- limitations with CONCURRENT operations in functions. +-- +-- ================================================ + +-- Create the ActivityLogs index +-- This will error gracefully if the table doesn't exist +CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_activitylogs_userid_datecreated +ON activitylog."ActivityLogs" ("UserId", "DateCreated" DESC) +WHERE "UserId" IS NOT NULL; + +-- Verify it was created +SELECT + schemaname AS "Schema", + tablename AS "Table", + indexname AS "Index Name", + indexdef AS "Definition" +FROM pg_indexes +WHERE indexname = 'idx_activitylogs_userid_datecreated'; + +-- Expected: 1 row showing the index on activitylog.ActivityLogs