@@ -2,4 +2,138 @@ import path from "path";
22
33import { Database } from "bun:sqlite" ;
44
5- const db = new Database ( path . resolve ( process . cwd ( ) , "db.sqlite3" ) ) ;
5+ import { pool } from "./database" ;
6+
7+ const sqliteDb = new Database ( path . resolve ( process . cwd ( ) , "db.sqlite3" ) ) ;
8+ // const sqliteDb = new Database(path.resolve(process.cwd(), "db-prod.sqlite3"));
9+
10+ const client = await pool . connect ( ) ;
11+
12+ console . log ( "▶️ Starting migration..." ) ;
13+
14+ // Load SQLite data
15+ const discordData = sqliteDb . query ( "SELECT * FROM discord" ) . all ( ) ;
16+
17+ console . log (
18+ `🔃 Loaded ${ discordData . length } rows from discord table in SQLite` ,
19+ ) ;
20+ const youtubeData = sqliteDb . query ( "SELECT * FROM youtube" ) . all ( ) ;
21+
22+ console . log (
23+ `🔃 Loaded ${ youtubeData . length } rows from youtube table in SQLite` ,
24+ ) ;
25+ const twitchData = sqliteDb . query ( "SELECT * FROM twitch" ) . all ( ) ;
26+
27+ console . log ( `🔃 Loaded ${ twitchData . length } rows from twitch table in SQLite` ) ;
28+ const botInfoData = sqliteDb . query ( "SELECT * FROM bot_info" ) . all ( ) ;
29+
30+ console . log (
31+ `🔃 Loaded ${ botInfoData . length } rows from bot_info table in SQLite` ,
32+ ) ;
33+
34+ // Insert into discord table
35+ const uniqueGuilds = new Set ( discordData . map ( ( d : any ) => d . guild_id ) ) ;
36+
37+ for ( const guild_id of uniqueGuilds ) {
38+ await client . query (
39+ `
40+ INSERT INTO discord (guild_id, is_dm, allowed_public_sharing)
41+ VALUES ($1, false, false)
42+ ON CONFLICT (guild_id) DO NOTHING
43+ ` ,
44+ [ guild_id ] ,
45+ ) ;
46+ console . log ( `➡️ Inserted guild ${ guild_id } into discord table` ) ;
47+ }
48+
49+ // Insert into twitch table
50+ for ( const row of twitchData as any ) {
51+ await client . query (
52+ `
53+ INSERT INTO twitch (twitch_channel_id, twitch_channel_is_live)
54+ VALUES ($1, $2)
55+ ON CONFLICT (twitch_channel_id) DO NOTHING
56+ ` ,
57+ [ row . twitch_channel_id , row . is_live === 1 ] ,
58+ ) ;
59+ console . log (
60+ `➡️ Inserted twitch channel ${ row . twitch_channel_id } into twitch table` ,
61+ ) ;
62+ }
63+
64+ // Insert into youtube table
65+ for ( const row of youtubeData as any ) {
66+ await client . query (
67+ `
68+ INSERT INTO youtube (youtube_channel_id, latest_video_id)
69+ VALUES ($1, $2)
70+ ON CONFLICT (youtube_channel_id) DO UPDATE SET latest_video_id = EXCLUDED.latest_video_id
71+ ` ,
72+ [ row . youtube_channel_id , row . latest_video_id ] ,
73+ ) ;
74+ console . log (
75+ `➡️ Inserted youtube channel ${ row . youtube_channel_id } into youtube table` ,
76+ ) ;
77+ }
78+
79+ // Insert into guild_twitch_subscriptions and guild_youtube_subscriptions
80+ for ( const row of discordData as any ) {
81+ const is_dm = false ;
82+
83+ if ( row . guild_platform === "twitch" ) {
84+ await client . query (
85+ `
86+ INSERT INTO guild_twitch_subscriptions
87+ (guild_id, twitch_channel_id, notification_channel_id, notification_role_id, is_dm)
88+ VALUES ($1, $2, $3, $4, $5)
89+ ` ,
90+ [
91+ row . guild_id ,
92+ row . platform_user_id ,
93+ row . guild_channel_id ,
94+ row . guild_ping_role ,
95+ is_dm ,
96+ ] ,
97+ ) ;
98+ } else if ( row . guild_platform === "youtube" ) {
99+ await client . query (
100+ `
101+ INSERT INTO guild_youtube_subscriptions
102+ (guild_id, youtube_channel_id, notification_channel_id, notification_role_id, is_dm, track_videos, track_shorts, track_streams)
103+ VALUES ($1, $2, $3, $4, $5, false, false, false)
104+ ` ,
105+ [
106+ row . guild_id ,
107+ row . platform_user_id ,
108+ row . guild_channel_id ,
109+ row . guild_ping_role ,
110+ is_dm ,
111+ ] ,
112+ ) ;
113+ }
114+ console . log (
115+ `➡️ Inserted ${ row . guild_platform } subscription for guild ${ row . guild_id } into subscriptions table` ,
116+ ) ;
117+ }
118+
119+ // Insert into bot_info
120+ if ( botInfoData . length > 0 ) {
121+ const row = botInfoData [ 0 ] as any ;
122+
123+ await client . query (
124+ `
125+ INSERT INTO bot_info (guilds_total, channels_tracked, total_members, updated_at, extended_info_updated_at)
126+ VALUES ($1, 0, $2, now(), now())
127+ ` ,
128+ [ row . total_servers , row . total_members ] ,
129+ ) ;
130+ console . log (
131+ `➡️ Inserted bot info with guilds_total: ${ row . total_servers } and total_members: ${ row . total_members } ` ,
132+ ) ;
133+ }
134+
135+ console . log ( "✅ Migration complete." ) ;
136+
137+ // Sometimes the connection pool doesn't close properly
138+ // so we need to force it to close by... doing this
139+ process . exit ( 0 ) ;
0 commit comments