|
| 1 | +DO $$ |
| 2 | +DECLARE |
| 3 | + la_jurisdiction_id UUID; |
| 4 | +BEGIN |
| 5 | + SELECT j.id |
| 6 | + INTO la_jurisdiction_id |
| 7 | + FROM jurisdictions j |
| 8 | + WHERE j.name = 'Los Angeles' |
| 9 | + ORDER BY j.created_at ASC |
| 10 | + LIMIT 1; |
| 11 | + |
| 12 | + -- If the Los Angeles jurisdiction does not yet exist, safely no-op |
| 13 | + IF la_jurisdiction_id IS NULL THEN |
| 14 | + RETURN; |
| 15 | + END IF; |
| 16 | + |
| 17 | + INSERT INTO reserved_community_types (created_at, updated_at, description, jurisdiction_id, name) |
| 18 | + SELECT |
| 19 | + now(), |
| 20 | + now(), |
| 21 | + NULL, |
| 22 | + la_jurisdiction_id, |
| 23 | + allowed_name |
| 24 | + FROM unnest(ARRAY['senior55', 'senior62', 'referralOnly']::TEXT[]) AS allowed_name |
| 25 | + WHERE NOT EXISTS ( |
| 26 | + SELECT 1 |
| 27 | + FROM reserved_community_types rct |
| 28 | + WHERE rct.name = allowed_name |
| 29 | + ); |
| 30 | + |
| 31 | + CREATE TEMP TABLE tmp_reserved_community_type_keep ( |
| 32 | + name TEXT PRIMARY KEY, |
| 33 | + keep_id UUID NOT NULL |
| 34 | + ) ON COMMIT DROP; |
| 35 | + |
| 36 | + INSERT INTO tmp_reserved_community_type_keep (name, keep_id) |
| 37 | + SELECT |
| 38 | + allowed_name, |
| 39 | + ( |
| 40 | + SELECT rct.id |
| 41 | + FROM reserved_community_types rct |
| 42 | + WHERE rct.name = allowed_name |
| 43 | + ORDER BY |
| 44 | + CASE |
| 45 | + WHEN rct.jurisdiction_id = la_jurisdiction_id THEN 0 |
| 46 | + ELSE 1 |
| 47 | + END, |
| 48 | + rct.created_at ASC, |
| 49 | + rct.id ASC |
| 50 | + LIMIT 1 |
| 51 | + ) |
| 52 | + FROM unnest(ARRAY['senior55', 'senior62', 'referralOnly']::TEXT[]) AS allowed_name; |
| 53 | + |
| 54 | + UPDATE reserved_community_types rct |
| 55 | + SET |
| 56 | + jurisdiction_id = la_jurisdiction_id, |
| 57 | + updated_at = now() |
| 58 | + FROM tmp_reserved_community_type_keep keep |
| 59 | + WHERE rct.id = keep.keep_id |
| 60 | + AND rct.jurisdiction_id IS DISTINCT FROM la_jurisdiction_id; |
| 61 | + |
| 62 | + UPDATE listings l |
| 63 | + SET |
| 64 | + reserved_community_type_id = keep.keep_id, |
| 65 | + updated_at = now() |
| 66 | + FROM reserved_community_types rct |
| 67 | + JOIN tmp_reserved_community_type_keep keep |
| 68 | + ON keep.name = rct.name |
| 69 | + WHERE l.reserved_community_type_id = rct.id |
| 70 | + AND rct.id <> keep.keep_id; |
| 71 | + |
| 72 | + UPDATE listing_snapshot ls |
| 73 | + SET reserved_community_type_id = keep.keep_id |
| 74 | + FROM reserved_community_types rct |
| 75 | + JOIN tmp_reserved_community_type_keep keep |
| 76 | + ON keep.name = rct.name |
| 77 | + WHERE ls.reserved_community_type_id = rct.id |
| 78 | + AND rct.id <> keep.keep_id; |
| 79 | + |
| 80 | + UPDATE listings l |
| 81 | + SET |
| 82 | + reserved_community_type_id = NULL, |
| 83 | + updated_at = now() |
| 84 | + WHERE l.reserved_community_type_id IN ( |
| 85 | + SELECT rct.id |
| 86 | + FROM reserved_community_types rct |
| 87 | + WHERE rct.name NOT IN ('senior55', 'senior62', 'referralOnly') |
| 88 | + ); |
| 89 | + |
| 90 | + UPDATE listing_snapshot ls |
| 91 | + SET reserved_community_type_id = NULL |
| 92 | + WHERE ls.reserved_community_type_id IN ( |
| 93 | + SELECT rct.id |
| 94 | + FROM reserved_community_types rct |
| 95 | + WHERE rct.name NOT IN ('senior55', 'senior62', 'referralOnly') |
| 96 | + ); |
| 97 | + |
| 98 | + DELETE FROM reserved_community_types rct |
| 99 | + WHERE rct.id NOT IN ( |
| 100 | + SELECT keep.keep_id |
| 101 | + FROM tmp_reserved_community_type_keep keep |
| 102 | + ); |
| 103 | +END $$; |
0 commit comments