Add support for sqlite

This commit is contained in:
William Brawner 2023-08-07 20:41:54 -06:00
parent fb2e04f90e
commit 96b833cb52
6 changed files with 24 additions and 9 deletions

2
.gitignore vendored
View file

@ -29,3 +29,5 @@ HELP.md
.vscode/
.gradle
*.sql
twigs.db

View file

@ -23,6 +23,7 @@ import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json
import org.slf4j.LoggerFactory
import java.lang.RuntimeException
import java.util.concurrent.TimeUnit
fun main(args: Array<String>): Unit = io.ktor.server.cio.EngineMain.main(args)
@ -30,13 +31,25 @@ fun main(args: Array<String>): Unit = io.ktor.server.cio.EngineMain.main(args)
private const val DATABASE_VERSION = 3
fun Application.module() {
val dbType = environment.config.propertyOrNull("twigs.database.type")?.getString() ?: "sqlite"
val dbHost = environment.config.propertyOrNull("twigs.database.host")?.getString() ?: "localhost"
val dbPort = environment.config.propertyOrNull("twigs.database.port")?.getString() ?: "5432"
val dbName = environment.config.propertyOrNull("twigs.database.name")?.getString() ?: "twigs"
val dbUser = environment.config.propertyOrNull("twigs.database.user")?.getString() ?: "twigs"
val dbPass = environment.config.propertyOrNull("twigs.database.password")?.getString() ?: "twigs"
val jdbcUrl = "jdbc:postgresql://$dbHost:$dbPort/$dbName?stringtype=unspecified"
(LoggerFactory.getLogger("com.zaxxer.hikari") as ch.qos.logback.classic.Logger).level = Level.ERROR
val jdbcUrl = when (dbType) {
"postgresql" -> {
"jdbc:$dbType://$dbHost:$dbPort/$dbName?stringtype=unspecified"
}
"sqlite" -> {
Class.forName("org.sqlite.JDBC")
"jdbc:$dbType:$dbName"
}
else -> {
throw RuntimeException("Unsupported DB type: $dbType")
}
}
(LoggerFactory.getLogger("com.zaxxer.hikari") as ch.qos.logback.classic.Logger).level = Level.DEBUG
HikariDataSource(HikariConfig().apply {
setJdbcUrl(jdbcUrl)
username = dbUser

View file

@ -10,15 +10,11 @@ ktor {
twigs {
database {
host = localhost
host = ${?TWIGS_DB_HOST}
port = 5432
port = ${?TWIGS_DB_PORT}
name = twigs
name = twigs.db
name = ${?TWIGS_DB_NAME}
user = twigs
user = ${?TWIGS_DB_USER}
password = twigs
password = ${?TWIGS_DB_PASS}
}
password {

View file

@ -8,7 +8,8 @@ val ktorVersion: String by rootProject.extra
dependencies {
implementation(kotlin("stdlib"))
api(project(":storage"))
implementation(libs.postgres)
runtimeOnly(libs.postgres)
runtimeOnly(libs.sqlite)
api(libs.hikari)
implementation(libs.logback)
testImplementation(libs.junit.jupiter.api)

View file

@ -15,6 +15,7 @@ class MetadataRepository(dataSource: DataSource) :
.getResource("/sql/$toVersion.sql")
?.readText()
?.split(";")
?.filterNot { it.isBlank() }
?: throw Error("No migration found for version $toVersion")
dataSource.connection.use { conn ->
queries.forEach { query ->

View file

@ -7,8 +7,9 @@ kotlinx-coroutines = "1.6.2"
ktor = "2.0.2"
logback = "1.2.11"
mail = "1.6.2"
postgres = "42.3.4"
postgres = "42.3.8"
shadow = "7.0.0"
sqlite = "3.42.0.0"
[libraries]
bcrypt = { module = "at.favre.lib:bcrypt", version.ref = "bcrypt" }
@ -30,6 +31,7 @@ ktor-server-sessions = { module = "io.ktor:ktor-server-sessions", version.ref =
logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }
mail = { module = "com.sun.mail:javax.mail", version.ref = "mail" }
postgres = { module = "org.postgresql:postgresql", version.ref = "postgres" }
sqlite = { module = "org.xerial:sqlite-jdbc", version.ref = "sqlite" }
[bundles]
ktor-server = [