Skip to content

Commit ba95662

Browse files
committed
chore(database): migrate initTables to seperate file
1 parent 84d90c7 commit ba95662

File tree

4 files changed

+182
-167
lines changed

4 files changed

+182
-167
lines changed

new.dbml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ Table bot_info_notifications {
7373
total_twitch int [not null, default: 0]
7474
}
7575

76+
Table bot_info_notifications_timings {
77+
time datetime [not null]
78+
channel_id string [not null, ref: > youtube.youtube_channel_id]
79+
time_ms int [not null, default: 0]
80+
}
81+
7682
Table bot_info_top_channels {
7783
youtube_channel_id string [not null, pk, ref: > youtube.youtube_channel_id]
7884
guilds_tracking int [not null, default: 0]

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { CronJob } from "cron";
1313

1414
import { env } from "./config.ts";
1515
import commandsMap from "./commands.ts";
16-
import { initTables } from "./utils/database.ts";
16+
import initTables from "./utils/db/init.ts";
1717
import { getTwitchToken } from "./utils/twitch/auth.ts";
1818
import backup from "./utils/backup.ts";
1919

@@ -67,7 +67,7 @@ const data = (await rest.put(Routes.applicationCommands(getAppId.id), {
6767

6868
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
6969

70-
// Check if MySQL is set up properly and its working
70+
// Check if Postgres is set up properly and its working
7171
if (!(await initTables())) {
7272
throw new Error("Error initializing tables");
7373
}

src/utils/database.ts

Lines changed: 0 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -26,171 +26,6 @@ export const pool: Pool = new Pool({
2626
database: dbCredentials.database,
2727
});
2828

29-
// #region Init Tables
30-
export async function initTables(): Promise<boolean> {
31-
const createDiscordTable = `
32-
CREATE TABLE IF NOT EXISTS discord (
33-
guild_id TEXT PRIMARY KEY,
34-
is_dm BOOLEAN NOT NULL DEFAULT FALSE,
35-
allowed_public_sharing BOOLEAN NOT NULL DEFAULT FALSE
36-
);
37-
`;
38-
39-
const createBlueskyTable = `
40-
CREATE TABLE IF NOT EXISTS bluesky (
41-
bluesky_user_id TEXT PRIMARY KEY,
42-
latest_post_id TEXT,
43-
latest_reply_id TEXT
44-
);
45-
`;
46-
47-
const createYouTubeTable = `
48-
CREATE TABLE IF NOT EXISTS youtube (
49-
youtube_channel_id TEXT PRIMARY KEY,
50-
latest_video_id TEXT,
51-
latest_video_id_updated TIMESTAMP,
52-
latest_short_id TEXT,
53-
latest_short_id_updated TIMESTAMP,
54-
latest_stream_id TEXT,
55-
latest_stream_id_updated TIMESTAMP,
56-
youtube_channel_is_live BOOLEAN
57-
);
58-
`;
59-
60-
const createTwitchTable = `
61-
CREATE TABLE IF NOT EXISTS twitch (
62-
twitch_channel_id TEXT PRIMARY KEY,
63-
twitch_channel_is_live BOOLEAN NOT NULL
64-
);
65-
`;
66-
67-
const createGuildBlueskySubscriptionsTable = `
68-
CREATE TABLE IF NOT EXISTS guild_bluesky_subscriptions (
69-
id SERIAL PRIMARY KEY,
70-
guild_id TEXT NOT NULL REFERENCES discord(guild_id),
71-
bluesky_user_id TEXT NOT NULL REFERENCES bluesky(bluesky_user_id),
72-
notification_channel_id TEXT NOT NULL,
73-
notification_role_id TEXT,
74-
is_dm BOOLEAN NOT NULL DEFAULT FALSE,
75-
check_for_replies BOOLEAN NOT NULL DEFAULT FALSE
76-
);
77-
`;
78-
79-
const createGuildYouTubeSubscriptionsTable = `
80-
CREATE TABLE IF NOT EXISTS guild_youtube_subscriptions (
81-
id SERIAL PRIMARY KEY,
82-
guild_id TEXT NOT NULL REFERENCES discord(guild_id),
83-
youtube_channel_id TEXT NOT NULL REFERENCES youtube(youtube_channel_id),
84-
notification_channel_id TEXT NOT NULL,
85-
notification_role_id TEXT,
86-
is_dm BOOLEAN NOT NULL DEFAULT FALSE,
87-
track_videos BOOLEAN NOT NULL DEFAULT FALSE,
88-
track_shorts BOOLEAN NOT NULL DEFAULT FALSE,
89-
track_streams BOOLEAN NOT NULL DEFAULT FALSE
90-
);
91-
`;
92-
93-
const createGuildTwitchSubscriptionsTable = `
94-
CREATE TABLE IF NOT EXISTS guild_twitch_subscriptions (
95-
id SERIAL PRIMARY KEY,
96-
guild_id TEXT NOT NULL REFERENCES discord(guild_id),
97-
twitch_channel_id TEXT NOT NULL REFERENCES twitch(twitch_channel_id),
98-
notification_channel_id TEXT NOT NULL,
99-
notification_role_id TEXT,
100-
is_dm BOOLEAN NOT NULL DEFAULT FALSE
101-
);
102-
`;
103-
104-
const createBotInfoTable = `
105-
CREATE TABLE IF NOT EXISTS bot_info (
106-
guilds_total INTEGER NOT NULL DEFAULT 0,
107-
channels_tracked INTEGER NOT NULL DEFAULT 0,
108-
total_members INTEGER NOT NULL DEFAULT 0,
109-
updated_at TIMESTAMP NOT NULL DEFAULT now(),
110-
extended_info_updated_at TIMESTAMP NOT NULL DEFAULT now()
111-
);
112-
`;
113-
114-
const createBotInfoNotificationsTable = `
115-
CREATE TABLE IF NOT EXISTS bot_info_notifications (
116-
date DATE NOT NULL,
117-
total_youtube INTEGER NOT NULL DEFAULT 0,
118-
total_twitch INTEGER NOT NULL DEFAULT 0
119-
);
120-
`;
121-
122-
const createBotInfoTopChannelsTable = `
123-
CREATE TABLE IF NOT EXISTS bot_info_top_channels (
124-
youtube_channel_id TEXT PRIMARY KEY REFERENCES youtube(youtube_channel_id),
125-
guilds_tracking INTEGER NOT NULL DEFAULT 0
126-
);
127-
`;
128-
129-
const createBotInfoTopGuildsTable = `
130-
CREATE TABLE IF NOT EXISTS bot_info_top_guilds (
131-
guild_id TEXT PRIMARY KEY REFERENCES discord(guild_id),
132-
members INTEGER NOT NULL DEFAULT 0
133-
);
134-
`;
135-
136-
const createAuditLogsTable = `
137-
CREATE TABLE IF NOT EXISTS audit_logs (
138-
id SERIAL PRIMARY KEY,
139-
event_type TEXT NOT NULL,
140-
guild_id TEXT NOT NULL REFERENCES discord(guild_id),
141-
related_id TEXT NOT NULL,
142-
note TEXT,
143-
occurred_at TIMESTAMP DEFAULT now()
144-
);
145-
`;
146-
147-
try {
148-
await pool.query(createDiscordTable);
149-
console.log("Discord table created");
150-
151-
await pool.query(createBlueskyTable);
152-
console.log("Bluesky table created");
153-
154-
await pool.query(createYouTubeTable);
155-
console.log("YouTube table created");
156-
157-
await pool.query(createTwitchTable);
158-
console.log("Twitch table created");
159-
160-
await pool.query(createGuildBlueskySubscriptionsTable);
161-
console.log("Guild Bluesky Subscriptions table created");
162-
163-
await pool.query(createGuildYouTubeSubscriptionsTable);
164-
console.log("Guild YouTube Subscriptions table created");
165-
166-
await pool.query(createGuildTwitchSubscriptionsTable);
167-
console.log("Guild Twitch Subscriptions table created");
168-
169-
await pool.query(createBotInfoTable);
170-
console.log("Bot Info table created");
171-
172-
await pool.query(createBotInfoNotificationsTable);
173-
console.log("Bot Info Notifications table created");
174-
175-
await pool.query(createBotInfoTopChannelsTable);
176-
console.log("Bot Info Top Channels table created");
177-
178-
await pool.query(createBotInfoTopGuildsTable);
179-
console.log("Bot Info Top Guilds table created");
180-
181-
await pool.query(createAuditLogsTable);
182-
console.log("Audit Logs table created");
183-
184-
return true;
185-
} catch (err) {
186-
console.error("Error creating tables:", err);
187-
188-
return false;
189-
}
190-
}
191-
192-
// #endregion
193-
19429
// #region YouTube
19530
// These two functions are for checking/adding a new channel to the youtube table
19631
export async function checkIfChannelIsAlreadyTracked(channelId: string) {

src/utils/db/init.ts

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
import { pool } from "../database";
2+
3+
export default async function initTables(): Promise<boolean> {
4+
const createDiscordTable = `
5+
CREATE TABLE IF NOT EXISTS discord (
6+
guild_id TEXT PRIMARY KEY,
7+
is_dm BOOLEAN NOT NULL DEFAULT FALSE,
8+
allowed_public_sharing BOOLEAN NOT NULL DEFAULT FALSE
9+
);
10+
`;
11+
12+
const createBlueskyTable = `
13+
CREATE TABLE IF NOT EXISTS bluesky (
14+
bluesky_user_id TEXT PRIMARY KEY,
15+
latest_post_id TEXT,
16+
latest_reply_id TEXT
17+
);
18+
`;
19+
20+
const createYouTubeTable = `
21+
CREATE TABLE IF NOT EXISTS youtube (
22+
youtube_channel_id TEXT PRIMARY KEY,
23+
latest_video_id TEXT,
24+
latest_video_id_updated TIMESTAMP,
25+
latest_short_id TEXT,
26+
latest_short_id_updated TIMESTAMP,
27+
latest_stream_id TEXT,
28+
latest_stream_id_updated TIMESTAMP,
29+
youtube_channel_is_live BOOLEAN
30+
);
31+
`;
32+
33+
const createTwitchTable = `
34+
CREATE TABLE IF NOT EXISTS twitch (
35+
twitch_channel_id TEXT PRIMARY KEY,
36+
twitch_channel_is_live BOOLEAN NOT NULL
37+
);
38+
`;
39+
40+
const createGuildBlueskySubscriptionsTable = `
41+
CREATE TABLE IF NOT EXISTS guild_bluesky_subscriptions (
42+
id SERIAL PRIMARY KEY,
43+
guild_id TEXT NOT NULL REFERENCES discord(guild_id),
44+
bluesky_user_id TEXT NOT NULL REFERENCES bluesky(bluesky_user_id),
45+
notification_channel_id TEXT NOT NULL,
46+
notification_role_id TEXT,
47+
is_dm BOOLEAN NOT NULL DEFAULT FALSE,
48+
check_for_replies BOOLEAN NOT NULL DEFAULT FALSE
49+
);
50+
`;
51+
52+
const createGuildYouTubeSubscriptionsTable = `
53+
CREATE TABLE IF NOT EXISTS guild_youtube_subscriptions (
54+
id SERIAL PRIMARY KEY,
55+
guild_id TEXT NOT NULL REFERENCES discord(guild_id),
56+
youtube_channel_id TEXT NOT NULL REFERENCES youtube(youtube_channel_id),
57+
notification_channel_id TEXT NOT NULL,
58+
notification_role_id TEXT,
59+
is_dm BOOLEAN NOT NULL DEFAULT FALSE,
60+
track_videos BOOLEAN NOT NULL DEFAULT FALSE,
61+
track_shorts BOOLEAN NOT NULL DEFAULT FALSE,
62+
track_streams BOOLEAN NOT NULL DEFAULT FALSE
63+
);
64+
`;
65+
66+
const createGuildTwitchSubscriptionsTable = `
67+
CREATE TABLE IF NOT EXISTS guild_twitch_subscriptions (
68+
id SERIAL PRIMARY KEY,
69+
guild_id TEXT NOT NULL REFERENCES discord(guild_id),
70+
twitch_channel_id TEXT NOT NULL REFERENCES twitch(twitch_channel_id),
71+
notification_channel_id TEXT NOT NULL,
72+
notification_role_id TEXT,
73+
is_dm BOOLEAN NOT NULL DEFAULT FALSE
74+
);
75+
`;
76+
77+
const createBotInfoTable = `
78+
CREATE TABLE IF NOT EXISTS bot_info (
79+
guilds_total INTEGER NOT NULL DEFAULT 0,
80+
channels_tracked INTEGER NOT NULL DEFAULT 0,
81+
total_members INTEGER NOT NULL DEFAULT 0,
82+
updated_at TIMESTAMP NOT NULL DEFAULT now(),
83+
extended_info_updated_at TIMESTAMP NOT NULL DEFAULT now()
84+
);
85+
`;
86+
87+
const createBotInfoNotificationsTable = `
88+
CREATE TABLE IF NOT EXISTS bot_info_notifications (
89+
date DATE NOT NULL,
90+
total_youtube INTEGER NOT NULL DEFAULT 0,
91+
total_twitch INTEGER NOT NULL DEFAULT 0
92+
);
93+
`;
94+
95+
const createBotInfoNotificationsTimingsTable = `
96+
CREATE TABLE IF NOT EXISTS bot_info_notifications_timings (
97+
time DATETIME NOT NULL,
98+
channel_id TEXT NOT NULL REFERENCES youtube(youtube_channel_id),
99+
time_ms INTEGER NOT NULL DEFAULT 0,
100+
);
101+
`;
102+
103+
const createBotInfoTopChannelsTable = `
104+
CREATE TABLE IF NOT EXISTS bot_info_top_channels (
105+
youtube_channel_id TEXT PRIMARY KEY REFERENCES youtube(youtube_channel_id),
106+
guilds_tracking INTEGER NOT NULL DEFAULT 0
107+
);
108+
`;
109+
110+
const createBotInfoTopGuildsTable = `
111+
CREATE TABLE IF NOT EXISTS bot_info_top_guilds (
112+
guild_id TEXT PRIMARY KEY REFERENCES discord(guild_id),
113+
members INTEGER NOT NULL DEFAULT 0
114+
);
115+
`;
116+
117+
const createAuditLogsTable = `
118+
CREATE TABLE IF NOT EXISTS audit_logs (
119+
id SERIAL PRIMARY KEY,
120+
event_type TEXT NOT NULL,
121+
guild_id TEXT NOT NULL REFERENCES discord(guild_id),
122+
related_id TEXT NOT NULL,
123+
note TEXT,
124+
occurred_at TIMESTAMP DEFAULT now()
125+
);
126+
`;
127+
128+
try {
129+
await pool.query(createDiscordTable);
130+
console.log("Discord table created");
131+
132+
await pool.query(createBlueskyTable);
133+
console.log("Bluesky table created");
134+
135+
await pool.query(createYouTubeTable);
136+
console.log("YouTube table created");
137+
138+
await pool.query(createTwitchTable);
139+
console.log("Twitch table created");
140+
141+
await pool.query(createGuildBlueskySubscriptionsTable);
142+
console.log("Guild Bluesky Subscriptions table created");
143+
144+
await pool.query(createGuildYouTubeSubscriptionsTable);
145+
console.log("Guild YouTube Subscriptions table created");
146+
147+
await pool.query(createGuildTwitchSubscriptionsTable);
148+
console.log("Guild Twitch Subscriptions table created");
149+
150+
await pool.query(createBotInfoTable);
151+
console.log("Bot Info table created");
152+
153+
await pool.query(createBotInfoNotificationsTable);
154+
console.log("Bot Info Notifications table created");
155+
156+
await pool.query(createBotInfoNotificationsTimingsTable);
157+
console.log("Bot Info Notifications Timings table created");
158+
159+
await pool.query(createBotInfoTopChannelsTable);
160+
console.log("Bot Info Top Channels table created");
161+
162+
await pool.query(createBotInfoTopGuildsTable);
163+
console.log("Bot Info Top Guilds table created");
164+
165+
await pool.query(createAuditLogsTable);
166+
console.log("Audit Logs table created");
167+
168+
return true;
169+
} catch (err) {
170+
console.error("Error creating tables:", err);
171+
172+
return false;
173+
}
174+
}

0 commit comments

Comments
 (0)