|
1 | 1 | -- Add indexes to support faster `/v6/my-reviews` queries. |
2 | 2 |
|
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; |
4 | 10 |
|
5 | 11 | CREATE INDEX IF NOT EXISTS "challenge_status_type_track_created_at_idx" |
6 | 12 | ON "Challenge" ("status", "typeId", "trackId", "createdAt" DESC); |
7 | 13 |
|
| 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 | + |
8 | 62 | CREATE INDEX IF NOT EXISTS "challenge_phase_challenge_open_end_idx" |
9 | 63 | ON "ChallengePhase" ("challengeId", "isOpen", "scheduledEndDate", "actualEndDate"); |
10 | 64 |
|
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