Skip to content

Commit 9a5843f

Browse files
committed
Fix: Add a secondary try-catch for empty table generation to give a real error
1 parent 627b38b commit 9a5843f

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

DiscordBot/Services/DatabaseService.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,23 @@ public DatabaseService(ILoggingService logging, BotSettings settings)
4646
LoggingService.LogToConsole(
4747
"DatabaseService: Table 'users' does not exist, attempting to generate table.",
4848
LogSeverity.Warning);
49-
c.ExecuteSql(
50-
"CREATE TABLE `users` (`ID` int(11) UNSIGNED NOT NULL, `UserID` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL, `Karma` int(11) UNSIGNED NOT NULL DEFAULT 0, `KarmaWeekly` int(11) UNSIGNED NOT NULL DEFAULT 0, `KarmaMonthly` int(11) UNSIGNED NOT NULL DEFAULT 0, `KarmaYearly` int(11) UNSIGNED NOT NULL DEFAULT 0, `KarmaGiven` int(11) UNSIGNED NOT NULL DEFAULT 0, `Exp` bigint(11) UNSIGNED NOT NULL DEFAULT 0, `Level` int(11) UNSIGNED NOT NULL DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
51-
c.ExecuteSql("ALTER TABLE `users` ADD PRIMARY KEY (`ID`,`UserID`), ADD UNIQUE KEY `UserID` (`UserID`)");
52-
c.ExecuteSql(
53-
"ALTER TABLE `users` MODIFY `ID` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1");
49+
try
50+
{
51+
c.ExecuteSql(
52+
"CREATE TABLE `users` (`ID` int(11) UNSIGNED NOT NULL, `UserID` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL, `Karma` int(11) UNSIGNED NOT NULL DEFAULT 0, `KarmaWeekly` int(11) UNSIGNED NOT NULL DEFAULT 0, `KarmaMonthly` int(11) UNSIGNED NOT NULL DEFAULT 0, `KarmaYearly` int(11) UNSIGNED NOT NULL DEFAULT 0, `KarmaGiven` int(11) UNSIGNED NOT NULL DEFAULT 0, `Exp` bigint(11) UNSIGNED NOT NULL DEFAULT 0, `Level` int(11) UNSIGNED NOT NULL DEFAULT 0) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
53+
c.ExecuteSql(
54+
"ALTER TABLE `users` ADD PRIMARY KEY (`ID`,`UserID`), ADD UNIQUE KEY `UserID` (`UserID`)");
55+
c.ExecuteSql(
56+
"ALTER TABLE `users` MODIFY `ID` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1");
57+
}
58+
catch (Exception e)
59+
{
60+
LoggingService.LogToConsole(
61+
$"SQL Exception: Failed to generate table 'users'.\nMessage: {e}",
62+
LogSeverity.Critical);
63+
c.Close();
64+
return;
65+
}
5466
LoggingService.LogToConsole("DatabaseService: Table 'users' generated without errors.",
5567
LogSeverity.Info);
5668
c.Close();

0 commit comments

Comments
 (0)