Skip to content

Commit 24b075e

Browse files
authored
add: mysql driver and new mysql/mariadb schema (#39)
1 parent 63b61ce commit 24b075e

File tree

5 files changed

+101
-9
lines changed

5 files changed

+101
-9
lines changed

controller-runtime/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dependencies {
99
api(libs.bundles.jooq)
1010
api(libs.sqlite.jdbc)
1111

12+
implementation(libs.mysql.connector)
1213
implementation(libs.simplecloud.metrics)
1314
implementation(libs.bundles.log4j)
1415
implementation(libs.clikt)
@@ -62,7 +63,7 @@ jooq {
6263
// - ? matches a single character in a directory / file name
6364
property {
6465
key = "scripts"
65-
value = "src/main/db/schema.sql"
66+
value = "src/main/db/schema/default.sql"
6667
}
6768

6869
// The sort order of the scripts within a directory, where:
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
MySQL-spezifisches Schema für den v3 Backend.
3+
Diese Datei enthält Anpassungen für MySQL/MariaDB.
4+
Wird automatisch verwendet, wenn MySQL als Datenbank konfiguriert ist.
5+
*/
6+
7+
CREATE TABLE IF NOT EXISTS cloud_servers (
8+
unique_id VARCHAR(36) NOT NULL PRIMARY KEY,
9+
group_name VARCHAR(255) NOT NULL,
10+
host_id VARCHAR(36) NOT NULL,
11+
numerical_id INT NOT NULL,
12+
ip VARCHAR(45) NOT NULL,
13+
port INT NOT NULL,
14+
minimum_memory INT NOT NULL,
15+
maximum_memory INT NOT NULL,
16+
max_players INT NOT NULL,
17+
player_count INT NOT NULL,
18+
state VARCHAR(50) NOT NULL,
19+
type VARCHAR(50) NOT NULL,
20+
created_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
21+
updated_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)
22+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
23+
24+
CREATE TABLE IF NOT EXISTS cloud_server_properties (
25+
server_id VARCHAR(36) NOT NULL,
26+
`key` VARCHAR(255) NOT NULL,
27+
value TEXT,
28+
PRIMARY KEY (server_id, `key`),
29+
CONSTRAINT fk_server_props_server FOREIGN KEY (server_id)
30+
REFERENCES cloud_servers(unique_id) ON DELETE CASCADE
31+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
32+
33+
CREATE TABLE IF NOT EXISTS oauth2_client_details (
34+
client_id VARCHAR(36) PRIMARY KEY,
35+
client_secret VARCHAR(255),
36+
redirect_uri VARCHAR(512),
37+
grant_types TEXT,
38+
scope TEXT
39+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
40+
41+
CREATE TABLE IF NOT EXISTS oauth2_users (
42+
user_id VARCHAR(36) PRIMARY KEY,
43+
scopes TEXT,
44+
username VARCHAR(255) UNIQUE NOT NULL,
45+
hashed_password VARCHAR(255) NOT NULL
46+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
47+
48+
CREATE TABLE IF NOT EXISTS oauth2_tokens (
49+
token_id VARCHAR(36) PRIMARY KEY,
50+
client_id VARCHAR(36),
51+
access_token TEXT,
52+
scope TEXT,
53+
expires_in TIMESTAMP NULL DEFAULT NULL,
54+
user_id VARCHAR(36),
55+
CONSTRAINT fk_token_user FOREIGN KEY (user_id)
56+
REFERENCES oauth2_users(user_id) ON DELETE CASCADE,
57+
CONSTRAINT fk_token_client FOREIGN KEY (client_id)
58+
REFERENCES oauth2_client_details(client_id) ON DELETE CASCADE
59+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
60+
61+
CREATE TABLE IF NOT EXISTS oauth2_groups (
62+
group_name VARCHAR(255) PRIMARY KEY,
63+
scopes TEXT
64+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
65+
66+
CREATE TABLE IF NOT EXISTS oauth2_user_groups (
67+
user_id VARCHAR(36),
68+
group_name VARCHAR(255),
69+
PRIMARY KEY (user_id, group_name)
70+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
11
package app.simplecloud.controller.runtime.database
22

3+
import org.apache.logging.log4j.LogManager
34
import org.jooq.DSLContext
5+
import org.jooq.SQLDialect
6+
import org.jooq.impl.DSL
47

58
class Database(
69
val context: DSLContext
710
) {
811

12+
private val logger = LogManager.getLogger(Database::class.java)
13+
914
fun setup() {
1015
System.setProperty("org.jooq.no-logo", "true")
1116
System.setProperty("org.jooq.no-tips", "true")
12-
val setupInputStream = Database::class.java.getResourceAsStream("/schema.sql")
13-
?: throw IllegalArgumentException("Database schema not found.")
14-
val setupCommands = setupInputStream.bufferedReader().use { it.readText() }.split(";")
15-
setupCommands.forEach {
16-
val trimmed = it.trim()
17-
if (trimmed.isNotEmpty())
18-
context.execute(org.jooq.impl.DSL.sql(trimmed))
17+
18+
val schemaFile = when (context.dialect().family()) {
19+
SQLDialect.MARIADB, SQLDialect.MYSQL -> "/schema/mysql.sql"
20+
else -> "/schema/default.sql"
21+
}
22+
23+
val setupInputStream = Database::class.java.getResourceAsStream(schemaFile)
24+
?: throw IllegalArgumentException("Database schema not found: $schemaFile")
25+
26+
val setupCommands = setupInputStream.bufferedReader().use { it.readText() }
27+
.split(";")
28+
.map { it.trim() }
29+
.filter { it.isNotEmpty() }
30+
31+
setupCommands.forEach { command ->
32+
try {
33+
context.execute(DSL.using(context.dialect()).parser().parseQuery(command))
34+
} catch (e: Exception) {
35+
logger.error("Error executing SQL command: {}", command, e)
36+
throw e
37+
}
1938
}
2039
}
21-
2240
}

gradle/libs.versions.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ sonatype-central-portal-publisher = "1.2.3"
1414
spotify-completablefutures = "0.3.6"
1515
spring-crypto = "6.3.4"
1616
envoy = "1.0.46"
17+
mysql-connector = "8.0.33"
1718

1819
[libraries]
1920
kotlin-jvm = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8", version.ref = "kotlin" }
@@ -45,6 +46,8 @@ spotify-completablefutures = { module = "com.spotify:completable-futures", versi
4546
spring-crypto = { module = "org.springframework.security:spring-security-crypto", version.ref = "spring-crypto" }
4647
envoy-controlplane = { module = "io.envoyproxy.controlplane:server", version.ref = "envoy" }
4748

49+
mysql-connector = { module = "mysql:mysql-connector-java", version.ref = "mysql-connector" }
50+
4851

4952
[bundles]
5053
log4j = [

0 commit comments

Comments
 (0)