🚧 Påbegynt database konfigurering

Co-authored-by: Hege Haavaldsen <hege.haavaldsen@nav.no>
Co-authored-by: Helene Arnesen <helene.arnesen@nav.no>
Co-authored-by: Sindre Kjelsrud <sindre.kjelsrud@nav.no>
Co-authored-by: Amalie Mansåker <amalie.erdal.mansaker@nav.no>
This commit is contained in:
Markus Johansen 2023-07-10 14:06:49 +02:00
parent 73a9490a4d
commit 23233121d0
4 changed files with 59 additions and 4 deletions

View file

@ -1,12 +1,17 @@
val ktor_version: String by project val ktor_version: String by project
val kotlin_version: String by project val kotlin_version: String by project
val logback_version: String by project val logback_version: String by project
val flyway_core_version: String by project
val postgresql_version: String by project
val hikariCP_version: String by project
val exposed_version: String by project
val testcontainers_postgresql_version: String by project
plugins { plugins {
kotlin("jvm") version "1.8.22" kotlin("jvm") version "1.8.22"
id("io.ktor.plugin") version "2.3.2" id("io.ktor.plugin") version "2.3.2"
kotlin("plugin.serialization") version "1.8.21" kotlin("plugin.serialization") version "1.8.21"
} }
group = "no.nav.helse.sprik" group = "no.nav.helse.sprik"
@ -26,11 +31,18 @@ dependencies {
implementation("io.ktor:ktor-server-core-jvm:$ktor_version") implementation("io.ktor:ktor-server-core-jvm:$ktor_version")
implementation("io.ktor:ktor-server-netty-jvm:$ktor_version") implementation("io.ktor:ktor-server-netty-jvm:$ktor_version")
implementation("ch.qos.logback:logback-classic:$logback_version") implementation("ch.qos.logback:logback-classic:$logback_version")
testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
implementation("io.ktor:ktor-server-cors:$ktor_version") implementation("io.ktor:ktor-server-cors:$ktor_version")
implementation("io.ktor:ktor-utils:$ktor_version") implementation("io.ktor:ktor-utils:$ktor_version")
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version") implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version")
implementation("io.ktor:ktor-server-content-negotiation:$ktor_version") implementation("io.ktor:ktor-server-content-negotiation:$ktor_version")
implementation("io.ktor:ktor-client-content-negotiation:$ktor_version") implementation("io.ktor:ktor-client-content-negotiation:$ktor_version")
implementation("org.flywaydb:flyway-core:$flyway_core_version")
implementation("org.postgresql:postgresql:$postgresql_version")
implementation("com.zaxxer:HikariCP:$hikariCP_version")
implementation("org.jetbrains.exposed:exposed-core:$exposed_version")
implementation("org.jetbrains.exposed:exposed-jdbc:$exposed_version")
implementation("org.jetbrains.exposed:exposed-java-time:$exposed_version")
testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
testImplementation("org.testcontainers:postgresql:$testcontainers_postgresql_version")
} }

View file

@ -2,3 +2,8 @@ ktor_version=2.3.2
kotlin_version=1.8.22 kotlin_version=1.8.22
logback_version=1.2.11 logback_version=1.2.11
kotlin.code.style=official kotlin.code.style=official
flyway_core_version=9.20.0
postgresql_version=42.6.0
hikariCP_version=5.0.1
exposed_version=0.41.1
testcontainers_postgresql_version=1.18.3

View file

@ -0,0 +1,26 @@
package no.nav.helse.sprik.db
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import no.nav.helse.sprik.db.Environment.Database.host
import no.nav.helse.sprik.db.Environment.Database.name
import no.nav.helse.sprik.db.Environment.Database.port
class Database(dbconfig: HikariConfig) {
val dataSource by lazy { HikariDataSource(dbconfig) }
}
private fun dbconfig() = HikariConfig().apply {
jdbcUrl = DB_URL
}
val DB_URL = "jdbc:postgresql://%s:%s/%s".format(host, port, name)
object Environment {
object Database {
private val env = System.getenv()
val host = requireNotNull(env["DATABASE_HOST"]) { "Host må settes" }
val port = requireNotNull(env["DATABASE_PORT"]) { "Port må settes" }
val name = requireNotNull(env["DATABASE_DATABASE"]) { "Databasenavn må settes" }
}
}

View file

@ -0,0 +1,12 @@
DO
$$
BEGIN
IF EXISTS
(SELECT 1 FROM pg_roles WHERE rolname = 'cloudsqliamuser')
THEN
GRANT ALL PRIVILEGES ON TABLE public.flyway_schema_history TO cloudsqliamuser;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO cloudsqliamuser;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO cloudsqliamuser;
END IF;
END
$$;