From: Olaf Wintermann Date: Fri, 5 Sep 2025 16:51:05 +0000 (+0200) Subject: add db_version table X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=8634f9e8d80552678984b3c6c26b56923e89e980;p=rssreader.git add db_version table --- diff --git a/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt b/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt index 93d6e5b..6aa07fa 100644 --- a/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt +++ b/rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt @@ -12,8 +12,10 @@ import java.sql.DriverManager import java.sql.Statement object Database { + const val DB_VERSION = 1 val dataSource: HikariDataSource + init { val config = HikariConfig() config.jdbcUrl = "jdbc:h2:~/.rssreader/feeds" @@ -26,19 +28,28 @@ object Database { } private fun ensureSchema(conn: Connection) { - var tableExists = false + var dbVersion = 0 conn.createStatement().use { stmt -> - stmt.executeQuery("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'FEEDCOLLECTIONS'").use { rs -> - if (rs.next()) { - tableExists = rs.getInt(1) > 0 + try { + stmt.executeQuery("select version from db_version").use { rs -> + if (rs.next()) { + dbVersion = rs.getInt(1) + } } - } + } catch (e: Exception) {} } - if (!tableExists) { + if (dbVersion == 0) { println("Database empty: creating tables") conn.createStatement().use { createStmt -> + createStmt.addBatch(""" + CREATE TABLE db_version ( + version INT NOT NULL + ) + """.trimIndent()) + createStmt.addBatch("insert into db_version(version) values (${DB_VERSION})") + createStmt.addBatch(""" CREATE TABLE groups ( group_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,