Fixing MySQL issues

This commit is contained in:
2026-04-08 21:08:18 -04:00
parent ae14755205
commit 7bfe6b19bb
3 changed files with 27 additions and 7 deletions
@@ -26,6 +26,7 @@ import java.io.IOException;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLSyntaxErrorException;
import java.util.Properties;
import java.util.logging.Level;
@@ -48,9 +49,21 @@ public class MySQL {
}
conn.setAutoCommit(true);
Query.use(conn);
Query.prepare("CREATE DATABASE IF NOT EXISTS `"
+ AntiVPN.getInstance().getVpnConfig().getDatabaseName() + "`").execute();
Query.prepare("USE `" + AntiVPN.getInstance().getVpnConfig().getDatabaseName() + "`").execute();
String databaseName = AntiVPN.getInstance().getVpnConfig().getDatabaseName();
try {
Query.prepare("CREATE DATABASE IF NOT EXISTS `" + databaseName + "`").execute();
} catch (SQLException ex) {
if (!isDatabaseCreationPermissionIssue(ex)) {
throw ex;
}
AntiVPN.getInstance().getExecutor().log(
"No permission to create MySQL database `" + databaseName
+ "`. Attempting to use the existing database instead.");
}
Query.prepare("USE `" + databaseName + "`").execute();
AntiVPN.getInstance().getExecutor().log("Connection to MySQL has been established.");
}
} catch (Exception e) {
@@ -59,6 +72,12 @@ public class MySQL {
}
}
private static boolean isDatabaseCreationPermissionIssue(SQLException ex) {
return ex instanceof SQLSyntaxErrorException
&& ex.getMessage() != null
&& ex.getMessage().contains("Access denied");
}
public static void initH2() {
initH2(true);
}
@@ -7,8 +7,6 @@ import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import java.sql.DriverManager;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
@@ -38,7 +36,7 @@ class MySqlDatabaseIntegrationTest extends DatabaseIntegrationTestSupport {
when(vpnConfig.getIp()).thenReturn(MYSQL.getHost());
when(vpnConfig.getPort()).thenReturn(MYSQL.getMappedPort(3306));
when(vpnConfig.getDatabaseName()).thenReturn("antivpn_" + UUID.randomUUID().toString().replace("-", ""));
when(vpnConfig.getDatabaseName()).thenReturn(MYSQL.getDatabaseName());
when(vpnConfig.getUsername()).thenReturn(MYSQL.getUsername());
when(vpnConfig.getPassword()).thenReturn(MYSQL.getPassword());
+4 -1
View File
@@ -45,6 +45,10 @@ allprojects {
}
}
tasks.named('test') {
dependsOn(subprojects.collect { it.tasks.named('test') })
}
evaluationDependsOn(':Common:Source')
evaluationDependsOn(':Bukkit:Loader')
evaluationDependsOn(':Velocity:VelocityLoader')
@@ -77,4 +81,3 @@ tasks.named('shadowJar') {
}
tasks.build.dependsOn shadowJar