Add health check
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package be.simplenotes.persistance
|
||||
|
||||
import be.simplenotes.persistance.utils.DbType
|
||||
import be.simplenotes.persistance.utils.type
|
||||
import be.simplenotes.shared.config.DataSourceConfig
|
||||
import me.liuwj.ktorm.database.Database
|
||||
import me.liuwj.ktorm.database.asIterable
|
||||
import java.sql.SQLTransientException
|
||||
|
||||
interface DbHealthCheck {
|
||||
fun isOk(): Boolean
|
||||
}
|
||||
|
||||
internal class DbHealthCheckImpl(
|
||||
private val db: Database,
|
||||
private val dataSourceConfig: DataSourceConfig,
|
||||
) : DbHealthCheck {
|
||||
override fun isOk() = if (dataSourceConfig.type() == DbType.H2) true
|
||||
else try {
|
||||
db.useConnection { connection ->
|
||||
connection.prepareStatement("""SHOW DATABASES""").use {
|
||||
it.executeQuery().asIterable().map { it.getString(1) }
|
||||
}
|
||||
}.any { it in dataSourceConfig.jdbcUrl }
|
||||
} catch (e: SQLTransientException) {
|
||||
false
|
||||
}
|
||||
}
|
||||
+10
-4
@@ -1,18 +1,24 @@
|
||||
package be.simplenotes.persistance
|
||||
|
||||
import be.simplenotes.persistance.utils.DbType
|
||||
import be.simplenotes.persistance.utils.type
|
||||
import be.simplenotes.shared.config.DataSourceConfig
|
||||
import org.flywaydb.core.Flyway
|
||||
import javax.sql.DataSource
|
||||
|
||||
interface DbMigrations {
|
||||
fun migrate()
|
||||
}
|
||||
|
||||
internal class DbMigrationsImpl(
|
||||
private val dataSource: DataSource,
|
||||
private val dataSourceConfig: DataSourceConfig
|
||||
private val dataSourceConfig: DataSourceConfig,
|
||||
) : DbMigrations {
|
||||
override fun migrate() {
|
||||
|
||||
val migrationDir = when {
|
||||
dataSourceConfig.jdbcUrl.contains("mariadb") -> "db/migration/mariadb"
|
||||
else -> "db/migration/other"
|
||||
val migrationDir = when (dataSourceConfig.type()) {
|
||||
DbType.H2 -> "db/migration/other"
|
||||
DbType.MariaDb -> "db/migration/mariadb"
|
||||
}
|
||||
|
||||
Flyway.configure()
|
||||
@@ -13,10 +13,6 @@ import org.koin.dsl.module
|
||||
import org.koin.dsl.onClose
|
||||
import javax.sql.DataSource
|
||||
|
||||
interface DbMigrations {
|
||||
fun migrate()
|
||||
}
|
||||
|
||||
private fun hikariDataSource(conf: DataSourceConfig): HikariDataSource {
|
||||
val hikariConfig = HikariConfig().also {
|
||||
it.jdbcUrl = conf.jdbcUrl
|
||||
@@ -41,4 +37,5 @@ val persistanceModule = module {
|
||||
get<DbMigrations>().migrate()
|
||||
Database.connect(get<DataSource>())
|
||||
}
|
||||
single<DbHealthCheck> { DbHealthCheckImpl(get(), get()) }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package be.simplenotes.persistance.utils
|
||||
|
||||
import be.simplenotes.shared.config.DataSourceConfig
|
||||
|
||||
enum class DbType { H2, MariaDb }
|
||||
|
||||
fun DataSourceConfig.type(): DbType = if (jdbcUrl.contains("mariadb")) DbType.MariaDb
|
||||
else DbType.H2
|
||||
Reference in New Issue
Block a user