a cache for slack profile pictures and emojis
1import type { Database } from "bun:sqlite";
2import type { Migration } from "./types";
3
4/**
5 * Migration to fix endpoint grouping in analytics
6 * This migration updates existing analytics data to use consistent endpoint grouping
7 */
8export const endpointGroupingMigration: Migration = {
9 version: "0.3.1",
10 description: "Fix endpoint grouping in analytics data",
11
12 async up(db: Database): Promise<void> {
13 console.log("Running endpoint grouping migration...");
14
15 // Get all request_analytics entries with specific URLs
16 const results = db
17 .query(`
18 SELECT id, endpoint FROM request_analytics
19 WHERE endpoint LIKE '/users/%' OR endpoint LIKE '/emojis/%'
20 `)
21 .all() as Array<{ id: string; endpoint: string }>;
22
23 console.log(`Found ${results.length} entries to update`);
24
25 // Process each entry and update with the correct grouping
26 for (const entry of results) {
27 let newEndpoint = entry.endpoint;
28
29 // Apply the same grouping logic we use in the analytics
30 if (entry.endpoint.match(/^\/users\/[^/]+$/)) {
31 // Keep as is - these are already correctly grouped
32 continue;
33 } else if (entry.endpoint.match(/^\/users\/[^/]+\/r$/)) {
34 // Keep as is - these are already correctly grouped
35 continue;
36 } else if (entry.endpoint.match(/^\/emojis\/[^/]+$/)) {
37 // Keep as is - these are already correctly grouped
38 continue;
39 } else if (entry.endpoint.match(/^\/emojis\/[^/]+\/r$/)) {
40 // Keep as is - these are already correctly grouped
41 continue;
42 } else if (
43 entry.endpoint.includes("/users/") &&
44 entry.endpoint.includes("/r")
45 ) {
46 // This is a user redirect with a non-standard format
47 newEndpoint = "/users/USER_ID/r";
48 } else if (entry.endpoint.includes("/users/")) {
49 // This is a user data endpoint with a non-standard format
50 newEndpoint = "/users/USER_ID";
51 } else if (
52 entry.endpoint.includes("/emojis/") &&
53 entry.endpoint.includes("/r")
54 ) {
55 // This is an emoji redirect with a non-standard format
56 newEndpoint = "/emojis/EMOJI_NAME/r";
57 } else if (entry.endpoint.includes("/emojis/")) {
58 // This is an emoji data endpoint with a non-standard format
59 newEndpoint = "/emojis/EMOJI_NAME";
60 }
61
62 // Only update if the endpoint has changed
63 if (newEndpoint !== entry.endpoint) {
64 db.run(
65 `
66 UPDATE request_analytics
67 SET endpoint = ?
68 WHERE id = ?
69 `,
70 [newEndpoint, entry.id],
71 );
72 }
73 }
74
75 console.log("Endpoint grouping migration completed");
76 },
77};