-
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.
Merge pull request #4 from navikt/dev/cpa-db
Dev/cpa db
- Loading branch information
Showing
9 changed files
with
85 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,5 @@ build/ | |
# Ignore Gradle build output directory | ||
build | ||
buildSrc/ | ||
ebms-provider/out/ | ||
cpa-repo/out/ |
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
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.cpa | ||
|
||
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 | ||
} | ||
} |
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,7 @@ | ||
package no.nav.emottak.cpa | ||
|
||
fun getEnvVar(varName: String, defaultValue: String? = null) = | ||
System.getProperty(varName) ?: System.getenv(varName) ?: defaultValue ?: throw RuntimeException("Environment: Missing required variable \"$varName\"") | ||
|
||
fun String.fromEnv(): String = | ||
getEnvVar(this) |
25 changes: 25 additions & 0 deletions
25
cpa-repo/src/main/kotlin/no/nav/emottak/cpa/config/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.cpa.config | ||
|
||
import com.zaxxer.hikari.HikariConfig | ||
import no.nav.emottak.cpa.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 | ||
} | ||
} |
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,5 @@ | ||
CREATE TABLE cpa | ||
( | ||
cpa_id VARCHAR(256) NOT NULL UNIQUE, | ||
cpa TEXT NOT NULL | ||
); |
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