Skip to content

Commit d7f053e

Browse files
committed
convert souls database to sqlite
1 parent 5141631 commit d7f053e

File tree

5 files changed

+97
-16
lines changed

5 files changed

+97
-16
lines changed

docker-compose.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ services:
1212
EULA: true
1313
ONLINE_MODE: false
1414
# Forge --------------------
15-
TYPE: NEOFORGE
16-
VERSION: "1.21.1"
17-
NEOFORGE_VERSION: "21.1.209"
15+
# TYPE: NEOFORGE
16+
# VERSION: "1.21.1"
17+
# NEOFORGE_VERSION: "21.1.209"
1818
# Paper --------------------
19-
#TYPE: PAPER
20-
#VERSION: 1.21.8
19+
TYPE: PAPER
20+
VERSION: 1.21.8
2121
# Custom -------------------
2222
# CUSTOM_SERVER: https://api.papermc.io/v2/projects/paper/versions/1.21.1/builds/121/downloads/paper-1.21.1-121.jar
2323
volumes:
24-
- ./build/neoforge:/data # Forge
25-
# - ./build/bukkit:/data # Bukkit/Paper/Spigot
24+
# - ./build/neoforge:/data # Forge
25+
- ./build/bukkit:/data # Bukkit/Paper/Spigot
2626
# - ./build/velocity:/data # Velocity
2727
# - ./build/fabric:/data # Fabric

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ makeevrserg.java.ktarget=21
1313
# Project
1414
makeevrserg.project.name=SoulKeeper
1515
makeevrserg.project.group=ru.astrainteractive.soulkeeper
16-
makeevrserg.project.version.string=1.2.8
16+
makeevrserg.project.version.string=1.2.9
1717
makeevrserg.project.description=Keep your items after death
1818
makeevrserg.project.developers=makeevrserg|Makeev Roman|[email protected]
1919
makeevrserg.project.url=https://github.com/Astra-Interactive/SoulKeeper

instances/bukkit/src/main/resources/plugin.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ softdepend: [ packetevents ]
1111
depend: [ ]
1212
loadbefore: [ Towny ]
1313
libraries:
14-
- "org.xerial:sqlite-jdbc:3.46.1.0"
14+
- "org.xerial:sqlite-jdbc:3.51.1.0"
1515
- "mysql:mysql-connector-java:8.0.33"
16-
- "com.h2database:h2:2.3.232"
16+
- "com.h2database:h2:2.4.240"
1717
commands:
1818
souls:
1919
skreload:

modules/dao/src/main/kotlin/ru/astrainteractive/soulkeeper/module/souls/di/SoulsDaoModule.kt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package ru.astrainteractive.soulkeeper.module.souls.di
22

33
import kotlinx.coroutines.CoroutineScope
44
import kotlinx.coroutines.GlobalScope
5+
import kotlinx.coroutines.delay
56
import kotlinx.coroutines.flow.Flow
67
import kotlinx.coroutines.flow.SharingStarted
78
import kotlinx.coroutines.flow.firstOrNull
89
import kotlinx.coroutines.flow.flow
10+
import kotlinx.coroutines.flow.retry
911
import kotlinx.coroutines.flow.shareIn
1012
import kotlinx.coroutines.launch
1113
import org.jetbrains.exposed.sql.Database
@@ -14,12 +16,15 @@ import org.jetbrains.exposed.sql.transactions.TransactionManager
1416
import org.jetbrains.exposed.sql.transactions.transaction
1517
import ru.astrainteractive.astralibs.lifecycle.Lifecycle
1618
import ru.astrainteractive.klibs.mikro.core.dispatchers.KotlinDispatchers
19+
import ru.astrainteractive.klibs.mikro.core.logging.JUtiltLogger
20+
import ru.astrainteractive.klibs.mikro.core.logging.Logger
1721
import ru.astrainteractive.klibs.mikro.exposed.model.DatabaseConfiguration
1822
import ru.astrainteractive.klibs.mikro.exposed.util.connect
1923
import ru.astrainteractive.soulkeeper.module.souls.dao.SoulsDao
2024
import ru.astrainteractive.soulkeeper.module.souls.dao.SoulsDaoImpl
2125
import ru.astrainteractive.soulkeeper.module.souls.database.table.SoulItemsTable
2226
import ru.astrainteractive.soulkeeper.module.souls.database.table.SoulTable
27+
import ru.astrainteractive.soulkeeper.module.souls.migration.H2ToSqliteMigration
2328
import java.io.File
2429

2530
interface SoulsDaoModule {
@@ -33,20 +38,27 @@ interface SoulsDaoModule {
3338
dataFolder: File,
3439
ioScope: CoroutineScope,
3540
dispatchers: KotlinDispatchers
36-
) : SoulsDaoModule {
41+
) : SoulsDaoModule, Logger by JUtiltLogger("SoulsDaoModule") {
3742
override val databaseFlow: Flow<Database> = flow {
43+
H2ToSqliteMigration(dataFolder).migrate()
3844
if (!dataFolder.exists()) dataFolder.mkdirs()
39-
val database = dataFolder.resolve("souls_v2").absolutePath
40-
.let(DatabaseConfiguration::H2)
45+
val database = dataFolder.resolve("souls_v3")
46+
.absolutePath
47+
.let(DatabaseConfiguration::SQLite)
4148
.connect()
4249
TransactionManager.manager.defaultIsolationLevel = java.sql.Connection.TRANSACTION_SERIALIZABLE
4350
transaction(database) {
4451
SchemaUtils.create(SoulTable)
45-
SchemaUtils.createMissingTablesAndColumns(SoulTable)
46-
SchemaUtils.createMissingTablesAndColumns(SoulItemsTable)
52+
SchemaUtils.create(SoulItemsTable)
4753
}
4854
emit(database)
49-
}.shareIn(ioScope, SharingStarted.Eagerly, 1)
55+
}
56+
.retry { throwable ->
57+
error(throwable) { "Could not connect to database" }
58+
delay(5000L)
59+
true
60+
}
61+
.shareIn(ioScope, SharingStarted.Eagerly, 1)
5062

5163
override val soulsDao: SoulsDao = SoulsDaoImpl(
5264
databaseFlow = databaseFlow,
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package ru.astrainteractive.soulkeeper.module.souls.migration
2+
3+
import kotlinx.coroutines.async
4+
import kotlinx.coroutines.awaitAll
5+
import kotlinx.coroutines.coroutineScope
6+
import kotlinx.coroutines.flow.flowOf
7+
import org.jetbrains.exposed.sql.SchemaUtils
8+
import org.jetbrains.exposed.sql.transactions.TransactionManager
9+
import org.jetbrains.exposed.sql.transactions.transaction
10+
import ru.astrainteractive.klibs.mikro.core.logging.JUtiltLogger
11+
import ru.astrainteractive.klibs.mikro.core.logging.Logger
12+
import ru.astrainteractive.klibs.mikro.exposed.model.DatabaseConfiguration
13+
import ru.astrainteractive.klibs.mikro.exposed.util.connect
14+
import ru.astrainteractive.soulkeeper.module.souls.dao.SoulsDaoImpl
15+
import ru.astrainteractive.soulkeeper.module.souls.database.model.DefaultSoul
16+
import ru.astrainteractive.soulkeeper.module.souls.database.table.SoulItemsTable
17+
import ru.astrainteractive.soulkeeper.module.souls.database.table.SoulTable
18+
import java.io.File
19+
20+
class H2ToSqliteMigration(
21+
val dataFolder: File
22+
) : Logger by JUtiltLogger("H2ToSqliteMigration") {
23+
24+
suspend fun migrate() = coroutineScope {
25+
if (!dataFolder.resolve("souls_v2.mv.db").exists()) return@coroutineScope
26+
info { "#migrate started migration of database..." }
27+
val h2Database = dataFolder.resolve("souls_v2")
28+
.absolutePath
29+
.let(DatabaseConfiguration::H2)
30+
.connect()
31+
val sqliteDatabase = dataFolder.resolve("souls_v3")
32+
.absolutePath
33+
.let(DatabaseConfiguration::SQLite)
34+
.connect()
35+
transaction(sqliteDatabase) {
36+
SchemaUtils.create(SoulTable)
37+
SchemaUtils.create(SoulItemsTable)
38+
}
39+
40+
val sqliteSoulsDao = SoulsDaoImpl(flowOf(sqliteDatabase))
41+
val hwSoulsDao = SoulsDaoImpl(flowOf(h2Database))
42+
hwSoulsDao.getSouls()
43+
.onFailure {
44+
error(it) { "Could not convert souls from H2 to SQLite" }
45+
return@coroutineScope
46+
}
47+
.getOrNull()
48+
.orEmpty()
49+
.map { dbSoul -> async { hwSoulsDao.toItemDatabaseSoul(dbSoul) } }
50+
.awaitAll()
51+
.mapNotNull { soul -> soul.getOrNull() }
52+
.map { soul ->
53+
DefaultSoul(
54+
ownerUUID = soul.ownerUUID,
55+
ownerLastName = soul.ownerLastName,
56+
createdAt = soul.createdAt,
57+
isFree = soul.isFree,
58+
location = soul.location,
59+
exp = soul.exp,
60+
items = soul.items,
61+
)
62+
}
63+
.forEach { soul -> sqliteSoulsDao.insertSoul(soul) }
64+
info { "#migrate Migration has been completed. Closing databases..." }
65+
TransactionManager.closeAndUnregister(h2Database)
66+
TransactionManager.closeAndUnregister(sqliteDatabase)
67+
dataFolder.resolve("souls_v2.mv.db").delete()
68+
}
69+
}

0 commit comments

Comments
 (0)