|
| 1 | +import path from "path"; |
| 2 | + |
| 3 | +import { Database } from "bun:sqlite"; |
| 4 | +import { drizzle as drizzleSqlite } from "drizzle-orm/bun-sqlite"; |
| 5 | +import { drizzle as drizzlePostgres } from "drizzle-orm/node-postgres"; |
| 6 | +import { Client } from "pg"; |
| 7 | + |
| 8 | +import { config } from "../config"; |
| 9 | +import { getStreamerName } from "../utils/twitch/getStreamerName"; |
| 10 | +import checkIfChannelIdIsValid from "../utils/youtube/checkIfChannelIdIsValid"; |
| 11 | +import { Platform } from "../types/types.d"; |
| 12 | +import { getTwitchToken } from "../utils/twitch/auth"; |
| 13 | + |
| 14 | +import * as sqliteSchema from "./schemaSqlite"; |
| 15 | +import * as pgSchema from "./schema"; |
| 16 | +import { addNewChannelToTrack } from "./youtube"; |
| 17 | +import { addNewStreamerToTrack } from "./twitch"; |
| 18 | +import { discordAddGuildTrackingUser } from "./discord"; |
| 19 | + |
| 20 | +// Get Twitch token |
| 21 | +if (!(await getTwitchToken())) { |
| 22 | + throw new Error("Error getting Twitch token"); |
| 23 | +} |
| 24 | + |
| 25 | +// SQLite connection |
| 26 | +const sqlite = new Database(path.resolve(process.cwd(), "db-prod.sqlite3")); |
| 27 | +const sqliteDb = drizzleSqlite(sqlite, { schema: sqliteSchema }); |
| 28 | + |
| 29 | +// Postgres connection |
| 30 | +const client = new Client({ connectionString: config.databaseUrl }); |
| 31 | + |
| 32 | +await client.connect(); |
| 33 | +const pgDb = drizzlePostgres(client, { schema: pgSchema }); |
| 34 | + |
| 35 | +console.log("📋 Starting migration...", config.databaseUrl); |
| 36 | + |
| 37 | +async function migrate() { |
| 38 | + // 1. Bot info |
| 39 | + const botInfo = await sqliteDb.select().from(sqliteSchema.sqliteBotInfo); |
| 40 | + |
| 41 | + for (const row of botInfo) { |
| 42 | + console.log( |
| 43 | + `📋 Migrating bot info: guildsTotal=${row.totalServers ?? 0}, totalMembers=${row.totalMembers ?? 0}`, |
| 44 | + ); |
| 45 | + await pgDb.insert(pgSchema.dbBotInfoTable).values({ |
| 46 | + guildsTotal: row.totalServers ?? 0, |
| 47 | + totalMembers: row.totalMembers ?? 0, |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + // 2. Discord guilds |
| 52 | + const discordRows = await sqliteDb |
| 53 | + .select() |
| 54 | + .from(sqliteSchema.sqliteDiscord); |
| 55 | + |
| 56 | + for (const row of discordRows) { |
| 57 | + console.log("📋 Migrating discord guild:", row.guildId); |
| 58 | + await pgDb |
| 59 | + .insert(pgSchema.dbDiscordTable) |
| 60 | + .values({ guildId: row.guildId ?? "" }) |
| 61 | + .onConflictDoNothing(); |
| 62 | + } |
| 63 | + |
| 64 | + // 3. Twitch |
| 65 | + const twitchRows = await sqliteDb.select().from(sqliteSchema.sqliteTwitch); |
| 66 | + |
| 67 | + for (const row of twitchRows) { |
| 68 | + const twitchChannelName = await getStreamerName(row.twitchChannelId); |
| 69 | + |
| 70 | + if (!twitchChannelName) { |
| 71 | + console.log( |
| 72 | + `⚠️ Skipping Twitch channel ID ${row.twitchChannelId} as it no longer exists.`, |
| 73 | + ); |
| 74 | + continue; |
| 75 | + } |
| 76 | + await addNewStreamerToTrack( |
| 77 | + row.twitchChannelId, |
| 78 | + Boolean(row.isLive), |
| 79 | + twitchChannelName, |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + // 4. YouTube |
| 84 | + const youtubeRows = await sqliteDb |
| 85 | + .select() |
| 86 | + .from(sqliteSchema.sqliteYouTube); |
| 87 | + |
| 88 | + for (const row of youtubeRows) { |
| 89 | + if (!checkIfChannelIdIsValid(row.youtubeChannelId)) { |
| 90 | + console.log( |
| 91 | + `⚠️ Skipping YouTube channel ID ${row.youtubeChannelId} as it is not a valid channel ID.`, |
| 92 | + ); |
| 93 | + continue; |
| 94 | + } |
| 95 | + await addNewChannelToTrack(row.youtubeChannelId); |
| 96 | + } |
| 97 | + |
| 98 | + // 5. Guild Subscriptions (after Twitch/YouTube exist!) |
| 99 | + for (const row of discordRows) { |
| 100 | + if (row.guildPlatform === "twitch") { |
| 101 | + await discordAddGuildTrackingUser( |
| 102 | + row.guildId, |
| 103 | + Platform.Twitch, |
| 104 | + row.platformUserId, |
| 105 | + row.guildChannelId, |
| 106 | + row.guildPingRole, |
| 107 | + false, |
| 108 | + ); |
| 109 | + } else if (row.guildPlatform === "youtube") { |
| 110 | + await discordAddGuildTrackingUser( |
| 111 | + row.guildId, |
| 112 | + Platform.YouTube, |
| 113 | + row.platformUserId, |
| 114 | + row.guildChannelId, |
| 115 | + row.guildPingRole, |
| 116 | + false, |
| 117 | + true, |
| 118 | + true, |
| 119 | + true, |
| 120 | + ); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +await migrate(); |
| 126 | + |
| 127 | +// Sometimes the connection pool doesn't close properly |
| 128 | +// so we need to force it to close by... doing this |
| 129 | +process.exit(0); |
0 commit comments