-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sette opp database i ebms-provider appen
- Loading branch information
Showing
4 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
ebms-provider/src/main/kotlin/no/nav/emottak/ebms/db/Database.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package no.nav.emottak.ebms.db | ||
|
||
import com.zaxxer.hikari.HikariConfig | ||
import com.zaxxer.hikari.HikariDataSource | ||
import org.flywaydb.core.Flyway | ||
import org.jetbrains.exposed.sql.Database | ||
|
||
class Database( | ||
dbConfig: HikariConfig | ||
) { | ||
val dataSource by lazy { HikariDataSource(dbConfig) } | ||
val db by lazy { Database.connect(dataSource) } | ||
private val config = dbConfig | ||
fun migrate() { | ||
migrationConfig(config) | ||
.let(::HikariDataSource) | ||
.also { | ||
Flyway.configure() | ||
.dataSource(it) | ||
.lockRetryCount(50) | ||
.load() | ||
.migrate() | ||
}.close() | ||
} | ||
|
||
private fun migrationConfig(conf: HikariConfig): HikariConfig = | ||
HikariConfig().apply { | ||
jdbcUrl = conf.jdbcUrl | ||
username = conf.username | ||
password = conf.password | ||
maximumPoolSize = 3 | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
ebms-provider/src/main/kotlin/no/nav/emottak/ebms/db/DatabaseConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package no.nav.emottak.ebms.db | ||
|
||
import com.zaxxer.hikari.HikariConfig | ||
import no.nav.emottak.util.fromEnv | ||
|
||
private const val prefix = "NAIS_DATABASE_CPA_REPO_CPA_REPO_DB" | ||
|
||
data class DatabaseConfig( | ||
val host: String = "${prefix}_HOST".fromEnv(), | ||
val port: String = "${prefix}_PORT".fromEnv(), | ||
val name: String = "${prefix}_DATABASE".fromEnv(), | ||
val username: String = "${prefix}_USERNAME".fromEnv(), | ||
val password: String = "${prefix}_PASSWORD".fromEnv(), | ||
val url: String = "jdbc:postgresql://%s:%s/%s".format(host, port, name) | ||
) | ||
|
||
|
||
fun mapHikariConfig(databaseConfig: DatabaseConfig): HikariConfig { | ||
return HikariConfig().apply { | ||
jdbcUrl = databaseConfig.url | ||
username = databaseConfig.username | ||
password = databaseConfig.password | ||
maximumPoolSize = 5 | ||
} | ||
} |