Skip to content

Commit

Permalink
Revert "Endre Database.Config til å bruke JDBC_URL"
Browse files Browse the repository at this point in the history
This reverts commit 73f4cc8.
  • Loading branch information
kenglxn committed Jan 15, 2025
1 parent aa798d8 commit 3b18870
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ import kotlinx.coroutines.*
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.json.laxObjectMapper
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.json.writeValueAsStringSupportingTypeInfoInCollections
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.unblocking.NonBlockingDataSource
import org.apache.http.client.utils.URIBuilder
import org.apache.http.message.BasicNameValuePair
import org.flywaydb.core.Flyway
import org.flywaydb.core.api.configuration.FluentConfiguration
import org.intellij.lang.annotations.Language
import java.io.Closeable
import java.net.URI
import java.sql.*
import java.time.*
import java.time.temporal.ChronoUnit
Expand All @@ -27,25 +24,16 @@ class Database private constructor(
private val dataSource: NonBlockingDataSource<*>,
) : Closeable {
data class Config(
private val jdbcUrl: String,
val host: String,
val port: String,
val database: String,
val username: String,
val password: String,
val migrationLocations: String,
val jdbcOpts: Map<String, String> = mapOf()
val jdbcOpts: Map<String, Any> = mapOf()
) {
val url: JdbcUrl = JdbcUrl(jdbcUrl, jdbcOpts)

val username: String
get() = url.username
val password: String
get() = url.password
val database: String
get() = url.database

/**
* make a copy but change the database name
*/
fun withDatabase(database: String) = copy(
jdbcUrl = url.withDatabase(database).toString()
)
val jdbcUrl: String
get() = "jdbc:postgresql://$host:$port/$database?${jdbcOpts.entries.joinToString("&")}"
}

override fun close() {
Expand All @@ -55,21 +43,22 @@ class Database private constructor(
companion object {
private val log = logger()

fun config(name: String, envPrefix: String = "DB", jdbcOpts: Map<String, String> = emptyMap()) = Config(
jdbcUrl = System.getenv("${envPrefix}_JDBC_URL") ?: "jdbc:postgresql://localhost:1337/${
name.replace(
'_',
'-'
)
}?password=postgres&user=postgres",
fun config(name: String, envPrefix: String = "DB", jdbcOpts: Map<String, Any> = mapOf()) = Config(
host = System.getenv("${envPrefix}_HOST") ?: "localhost",
port = System.getenv("${envPrefix}_PORT") ?: "1337",
username = System.getenv("${envPrefix}_USERNAME") ?: "postgres",
password = System.getenv("${envPrefix}_PASSWORD") ?: "postgres",
database = System.getenv("${envPrefix}_DATABASE") ?: name.replace('_', '-'),
migrationLocations = "db/migration/$name",
jdbcOpts = jdbcOpts,
)

private fun Config.asHikariConfig(): HikariConfig {
val config = this
return HikariConfig().apply {
jdbcUrl = config.url.toString()
jdbcUrl = config.jdbcUrl
username = config.username
password = config.password
driverClassName = "org.postgresql.Driver"
metricRegistry = Metrics.meterRegistry
minimumIdle = 1
Expand Down Expand Up @@ -367,51 +356,3 @@ fun <T> measureSql(
@Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
return timer.recordCallable(action)
}

/**
* Utility class to aid in constructing and manipulating a jdbc url.
*/
class JdbcUrl(
url: String,
additionalOptions: Map<String, String> = emptyMap()
) {

/**
* we need to strip the jdbc: part by using schemeSpecificPart
* so that URI is able to parse correctly.
* the jdbc: prefix is added back in toString()
*/
private val uri = URIBuilder(
URI(url).also {
require(it.scheme == "jdbc") { "not a jdbc url: $url" }
}.schemeSpecificPart
).also {
if (additionalOptions.isNotEmpty()) {
it.addParameters(additionalOptions.map { (k, v) -> BasicNameValuePair(k, v) })
}
}.build()

private val urlParameters = uri.query.split('&').associate {
val parts = it.split('=')
val name = parts.firstOrNull() ?: ""
val value = parts.drop(1).firstOrNull() ?: ""
Pair(name, value)
}

val username: String
get() = urlParameters["user"]!!
val password: String
get() = urlParameters["password"]!!
val database: String
get() = uri.path.split('/').last()

override fun toString() = "jdbc:$uri"

/**
* make a copy but change the database name. used in tests
*/
fun withDatabase(database: String): JdbcUrl {
val newUri = URIBuilder(uri).setPath("/$database").build()
return JdbcUrl("jdbc:$newUri")
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package no.nav.arbeidsgiver.notifikasjon.executable

import kotlinx.coroutines.runBlocking
import no.nav.arbeidsgiver.notifikasjon.kafka_backup.KafkaBackup
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.Database
import no.nav.arbeidsgiver.notifikasjon.infrastruktur.json.laxObjectMapper
import no.nav.arbeidsgiver.notifikasjon.kafka_backup.BackupRepository
import no.nav.arbeidsgiver.notifikasjon.kafka_backup.KafkaBackup
import no.nav.arbeidsgiver.notifikasjon.util.EksempelHendelse
import org.apache.kafka.clients.consumer.ConsumerRecord
import java.time.Duration
Expand All @@ -16,7 +16,7 @@ fun main() = runBlocking {
Database.openDatabase(
KafkaBackup.databaseConfig.copy(
// https://github.com/flyway/flyway/issues/2323#issuecomment-804495818
jdbcOpts = mapOf("preparedStatementCacheQueries" to "0")
jdbcOpts = mapOf("preparedStatementCacheQueries" to 0)

)
).use { database ->
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,24 @@ private suspend fun createDbFromTemplate(config: Database.Config): Database.Conf
val database = mutex.withLock {
"${config.database}_test-${ids.next()}"
}

DriverManager.getConnection(config.url.toString(), config.username, config.password).use { conn ->
DriverManager.getConnection(config.jdbcUrl, config.username, config.password).use { conn ->
conn.createStatement().use { stmt ->
@Suppress("SqlSourceToSinkFlow")
stmt.executeUpdate("""create database "$database" template "$templateDb"; """)
}
}
return config.withDatabase(database)
return config.copy(database = database)
}

/**
* henter template database for gitt config, eller lager en ny hvis den ikke finnes.
* template databasen brukes til å lage ferske databaser for hver test.
*/
@Suppress("SqlSourceToSinkFlow")
private fun templateDb(config: Database.Config): String {
private suspend fun templateDb(config: Database.Config): String {
val templateDb = templateDbs.computeIfAbsent(config) {
"${config.database}_template".also { db ->
DriverManager.getConnection(config.url.toString(), config.username, config.password).use { conn ->
DriverManager.getConnection(config.jdbcUrl, config.username, config.password).use { conn ->
conn.createStatement().use { stmt ->
val resultSet =
stmt.executeQuery("SELECT datname FROM pg_database where datname like '${config.database}_test%';")
Expand All @@ -59,7 +58,10 @@ private fun templateDb(config: Database.Config): String {
}
runBlocking {
Database.openDatabase(
config = config.withDatabase(db),
config = config.copy(
port = "1337",
database = db,
),
flywayAction = {
migrate()
}
Expand All @@ -72,11 +74,13 @@ private fun templateDb(config: Database.Config): String {

fun TestConfiguration.testDatabase(config: Database.Config): Database =
runBlocking {
val testConfig = createDbFromTemplate(config)
val database = createDbFromTemplate(config).database
Database.openDatabase(
config = testConfig.copy(
config = config.copy(
// https://github.com/flyway/flyway/issues/2323#issuecomment-804495818
jdbcOpts = mapOf("preparedStatementCacheQueries" to "0"),
jdbcOpts = mapOf("preparedStatementCacheQueries" to 0),
port = "1337",
database = database,
),
flywayAction = {
/* noop. created from template. */
Expand Down

0 comments on commit 3b18870

Please sign in to comment.