Skip to content

Commit 5b01637

Browse files
committed
Fix for migration / db drift
1 parent df0ad7a commit 5b01637

File tree

1 file changed

+57
-4
lines changed
  • prisma/migrations/20251107090000_add_my_reviews_indexes

1 file changed

+57
-4
lines changed
Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,66 @@
11
-- Add indexes to support faster `/v6/my-reviews` queries.
22

3-
CREATE EXTENSION IF NOT EXISTS pg_trgm;
3+
CREATE SCHEMA IF NOT EXISTS skills;
4+
CREATE SCHEMA IF NOT EXISTS reviews;
5+
6+
CREATE EXTENSION IF NOT EXISTS fuzzystrmatch WITH SCHEMA skills;
7+
CREATE EXTENSION IF NOT EXISTS pg_trgm WITH SCHEMA pg_catalog;
8+
CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA reviews;
9+
CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA skills;
410

511
CREATE INDEX IF NOT EXISTS "challenge_status_type_track_created_at_idx"
612
ON "Challenge" ("status", "typeId", "trackId", "createdAt" DESC);
713

14+
DROP INDEX IF EXISTS "challenge_name_idx";
15+
16+
CREATE INDEX IF NOT EXISTS "challenge_name_trgm_idx"
17+
ON "Challenge" USING gin ("name" pg_catalog.gin_trgm_ops);
18+
19+
DO
20+
$$
21+
DECLARE
22+
challenge_phase_schema TEXT;
23+
BEGIN
24+
SELECT n.nspname
25+
INTO challenge_phase_schema
26+
FROM pg_class c
27+
JOIN pg_namespace n ON n.oid = c.relnamespace
28+
WHERE c.relname = 'ChallengePhase'
29+
AND c.relkind = 'r'
30+
LIMIT 1;
31+
32+
IF challenge_phase_schema IS NULL THEN
33+
RETURN;
34+
END IF;
35+
36+
IF NOT EXISTS (
37+
SELECT 1
38+
FROM pg_class idx
39+
JOIN pg_namespace ns ON ns.oid = idx.relnamespace
40+
WHERE idx.relname = 'challenge_phase_order_idx'
41+
AND ns.nspname = challenge_phase_schema
42+
)
43+
AND EXISTS (
44+
SELECT 1
45+
FROM pg_class idx
46+
JOIN pg_namespace ns ON ns.oid = idx.relnamespace
47+
WHERE idx.relname = 'challenge_phase_challenge_open_end_idx'
48+
AND ns.nspname = challenge_phase_schema
49+
AND pg_get_indexdef(idx.oid) LIKE '%("challengeId", "isOpen", "scheduledEndDate", "actualEndDate", name)%'
50+
)
51+
THEN
52+
EXECUTE format(
53+
'ALTER INDEX %I.%I RENAME TO %I',
54+
challenge_phase_schema,
55+
'challenge_phase_challenge_open_end_idx',
56+
'challenge_phase_order_idx'
57+
);
58+
END IF;
59+
END
60+
$$ LANGUAGE plpgsql;
61+
862
CREATE INDEX IF NOT EXISTS "challenge_phase_challenge_open_end_idx"
963
ON "ChallengePhase" ("challengeId", "isOpen", "scheduledEndDate", "actualEndDate");
1064

11-
CREATE INDEX IF NOT EXISTS "challenge_name_trgm_idx"
12-
ON "Challenge"
13-
USING gin ("name" pg_catalog.gin_trgm_ops);
65+
CREATE INDEX IF NOT EXISTS "challenge_phase_order_idx"
66+
ON "ChallengePhase" ("challengeId", "isOpen", "scheduledEndDate", "actualEndDate", "name");

0 commit comments

Comments
 (0)