aboutsummaryrefslogtreecommitdiff
path: root/mod/src/main/kotlin/moe/nea/ledger/database
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2025-01-16 21:49:20 +0100
committerLinnea Gräf <nea@nea.moe>2025-01-16 21:49:20 +0100
commit305178f5511bbf7b4499d7cf4142a067ab42e2fc (patch)
tree845b3cca6490fb9c1252215aa310eec108316bf1 /mod/src/main/kotlin/moe/nea/ledger/database
parent40ed3a1f667d58501fc43fb45f53585315c013d1 (diff)
downloadLocalTransactionLedger-305178f5511bbf7b4499d7cf4142a067ab42e2fc.tar.gz
LocalTransactionLedger-305178f5511bbf7b4499d7cf4142a067ab42e2fc.tar.bz2
LocalTransactionLedger-305178f5511bbf7b4499d7cf4142a067ab42e2fc.zip
refactor: Extract database models to own modules
Diffstat (limited to 'mod/src/main/kotlin/moe/nea/ledger/database')
-rw-r--r--mod/src/main/kotlin/moe/nea/ledger/database/DBLogEntry.kt24
-rw-r--r--mod/src/main/kotlin/moe/nea/ledger/database/DBUpgrade.kt68
-rw-r--r--mod/src/main/kotlin/moe/nea/ledger/database/Database.kt57
-rw-r--r--mod/src/main/kotlin/moe/nea/ledger/database/Upgrades.kt20
-rw-r--r--mod/src/main/kotlin/moe/nea/ledger/database/schema.dot23
5 files changed, 0 insertions, 192 deletions
diff --git a/mod/src/main/kotlin/moe/nea/ledger/database/DBLogEntry.kt b/mod/src/main/kotlin/moe/nea/ledger/database/DBLogEntry.kt
deleted file mode 100644
index b162c6f..0000000
--- a/mod/src/main/kotlin/moe/nea/ledger/database/DBLogEntry.kt
+++ /dev/null
@@ -1,24 +0,0 @@
-package moe.nea.ledger.database
-
-import moe.nea.ledger.ItemChange
-import moe.nea.ledger.ItemId
-import moe.nea.ledger.TransactionType
-import moe.nea.ledger.database.columns.DBDouble
-import moe.nea.ledger.database.columns.DBEnum
-import moe.nea.ledger.database.columns.DBString
-import moe.nea.ledger.database.columns.DBUlid
-import moe.nea.ledger.database.columns.DBUuid
-
-object DBLogEntry : Table("LogEntry") {
- val transactionId = column("transactionId", DBUlid)
- val type = column("type", DBEnum<TransactionType>())
- val profileId = column("profileId", DBUuid)
- val playerId = column("playerId", DBUuid)
-}
-
-object DBItemEntry : Table("ItemEntry") {
- val transactionId = column("transactionId", DBUlid) // TODO: add foreign keys
- val mode = column("mode", DBEnum<ItemChange.ChangeDirection>())
- val itemId = column("item", DBString.mapped(ItemId::string, ::ItemId))
- val size = column("size", DBDouble)
-}
diff --git a/mod/src/main/kotlin/moe/nea/ledger/database/DBUpgrade.kt b/mod/src/main/kotlin/moe/nea/ledger/database/DBUpgrade.kt
deleted file mode 100644
index 7d1782a..0000000
--- a/mod/src/main/kotlin/moe/nea/ledger/database/DBUpgrade.kt
+++ /dev/null
@@ -1,68 +0,0 @@
-package moe.nea.ledger.database
-
-import java.sql.Connection
-
-interface DBUpgrade {
- val toVersion: Long
- val fromVersion get() = toVersion - 1
- fun performUpgrade(connection: Connection)
-
- companion object {
-
- fun performUpgrades(
- connection: Connection,
- upgrades: Iterable<DBUpgrade>,
- ) {
- for (upgrade in upgrades) {
- upgrade.performUpgrade(connection)
- }
- }
-
- fun performUpgradeChain(
- connection: Connection,
- from: Long, to: Long,
- upgrades: Iterable<DBUpgrade>,
- afterEach: (newVersion: Long) -> Unit,
- ) {
- val table = buildLookup(upgrades)
- for (version in (from + 1)..(to)) {
- val currentUpgrades = table[version] ?: listOf()
- println("Scheduled ${currentUpgrades.size} upgrades to reach DB version $version")
- performUpgrades(connection, currentUpgrades)
- afterEach(version)
- }
- }
-
- fun buildLookup(upgrades: Iterable<DBUpgrade>): Map<Long, List<DBUpgrade>> {
- return upgrades.groupBy { it.toVersion }
- }
-
- fun createTable(to: Long, table: Table, vararg columns: Column<*>): DBUpgrade {
- require(columns.all { it in table.columns })
- return of("Create table ${table}", to) {
- table.createIfNotExists(it, columns.toList())
- }
- }
-
- fun addColumns(to: Long, table: Table, vararg columns: Column<*>): DBUpgrade {
- return of("Add columns to table $table", to) {
- table.alterTableAddColumns(it, columns.toList())
- }
- }
-
- fun of(name: String, to: Long, block: (Connection) -> Unit): DBUpgrade {
- return object : DBUpgrade {
- override val toVersion: Long
- get() = to
-
- override fun performUpgrade(connection: Connection) {
- block(connection)
- }
-
- override fun toString(): String {
- return name
- }
- }
- }
- }
-} \ No newline at end of file
diff --git a/mod/src/main/kotlin/moe/nea/ledger/database/Database.kt b/mod/src/main/kotlin/moe/nea/ledger/database/Database.kt
deleted file mode 100644
index 025888c..0000000
--- a/mod/src/main/kotlin/moe/nea/ledger/database/Database.kt
+++ /dev/null
@@ -1,57 +0,0 @@
-package moe.nea.ledger.database
-
-import moe.nea.ledger.Ledger
-import moe.nea.ledger.database.columns.DBString
-import java.sql.Connection
-import java.sql.DriverManager
-
-class Database {
- lateinit var connection: Connection
-
- object MetaTable : Table("LedgerMeta") {
- val key = column("key", DBString)
- val value = column("value", DBString)
-
- init {
- unique(key)
- }
- }
-
- data class MetaKey(val name: String) {
- companion object {
- val DATABASE_VERSION = MetaKey("databaseVersion")
- val LAST_LAUNCH = MetaKey("lastLaunch")
- }
- }
-
- fun setMetaKey(key: MetaKey, value: String) {
- MetaTable.insert(connection, Table.OnConflict.REPLACE) {
- it[MetaTable.key] = key.name
- it[MetaTable.value] = value
- }
- }
-
- val databaseVersion: Long = 1
-
- fun loadAndUpgrade() {
- connection = DriverManager.getConnection("jdbc:sqlite:${Ledger.dataFolder.resolve("database.db")}")
- MetaTable.createIfNotExists(connection)
- val meta = MetaTable.selectAll(connection).associate { MetaKey(it[MetaTable.key]) to it[MetaTable.value] }
- val lastLaunch = meta[MetaKey.LAST_LAUNCH]?.toLong() ?: 0L
- println("Last launch $lastLaunch")
- setMetaKey(MetaKey.LAST_LAUNCH, System.currentTimeMillis().toString())
-
- val oldVersion = meta[MetaKey.DATABASE_VERSION]?.toLong() ?: -1
- println("Old Database Version: $oldVersion; Current version: $databaseVersion")
- if (oldVersion > databaseVersion)
- error("Outdated software. Database is newer than me!")
- // TODO: create a backup if there is a db version upgrade happening
- DBUpgrade.performUpgradeChain(
- connection, oldVersion, databaseVersion,
- Upgrades().upgrades
- ) { version ->
- setMetaKey(MetaKey.DATABASE_VERSION, version.toString())
- }
- }
-
-} \ No newline at end of file
diff --git a/mod/src/main/kotlin/moe/nea/ledger/database/Upgrades.kt b/mod/src/main/kotlin/moe/nea/ledger/database/Upgrades.kt
deleted file mode 100644
index e83abe7..0000000
--- a/mod/src/main/kotlin/moe/nea/ledger/database/Upgrades.kt
+++ /dev/null
@@ -1,20 +0,0 @@
-package moe.nea.ledger.database
-
-class Upgrades {
- val upgrades = mutableListOf<DBUpgrade>()
-
- fun add(upgrade: DBUpgrade) = upgrades.add(upgrade)
-
- init {
- add(DBUpgrade.createTable(
- 0, DBLogEntry,
- DBLogEntry.type, DBLogEntry.playerId, DBLogEntry.profileId,
- DBLogEntry.transactionId))
- add(DBUpgrade.createTable(
- 0, DBItemEntry,
- DBItemEntry.itemId, DBItemEntry.size, DBItemEntry.mode, DBItemEntry.transactionId
- ))
- }
-
-
-} \ No newline at end of file
diff --git a/mod/src/main/kotlin/moe/nea/ledger/database/schema.dot b/mod/src/main/kotlin/moe/nea/ledger/database/schema.dot
deleted file mode 100644
index d932f6a..0000000
--- a/mod/src/main/kotlin/moe/nea/ledger/database/schema.dot
+++ /dev/null
@@ -1,23 +0,0 @@
-digraph {
- node [shape=plain];
- rankdir=LR;
- entry [label=<
- <table border="0" cellborder="1" cellspacing="0">
- <tr><td>Log Entry</td></tr>
- <tr><td port="player">playerId</td></tr>
- <tr><td port="profile">profileId</td></tr>
- <tr><td port="date">timestamp</td></tr>
- <tr><td port="type">Type</td></tr>
- </table>
- >];
- item [label=<
- <table border="0" cellborder="1" cellspacing="0">
- <tr><td>Item Stack</td><tr>
- <tr><td port="transaction">Transaction</td></tr>
- <tr><td port="id">Item ID</td></tr>
- <tr><td port="count">Count</td></tr>
- <tr><td port="direction">Transfer Direction</td></tr>
- </table>
- >];
-// item:transaction -> entry;
-} \ No newline at end of file