Skip to content

Commit

Permalink
Lagre inntektsmelding som jsonb (#279)
Browse files Browse the repository at this point in the history
* Lagre inntektsmelding som jsonb

* Oppdater navn på migrering
  • Loading branch information
bjerga authored Sep 26, 2023
1 parent faa73aa commit cb29204
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 39 deletions.
3 changes: 1 addition & 2 deletions db/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ dependencies {
implementation("com.zaxxer:HikariCP:$hikariVersion")
implementation("org.flywaydb:flyway-core:$flywayVersion")
implementation("org.jetbrains.exposed:exposed-core:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-json:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-dao:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-jdbc:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-java-time:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-json:$exposedVersion")

runtimeOnly("org.postgresql:postgresql:$postgresqlVersion")

Expand Down
4 changes: 2 additions & 2 deletions db/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Dependency versions
exposedVersion=0.43.0
flywayVersion=9.16.3
flywayVersion=9.22.1
hikariVersion=5.0.1
postgresqlVersion=42.6.0
testcontainersPostgresqlVersion=1.18.0
testcontainersPostgresqlVersion=1.19.0
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ class InntektsmeldingRepository(private val db: Database) {
requestTimer.observeDuration()
}
}

fun hentNyeste(forespørselId: String): InntektsmeldingDokument? {
val requestTimer = requestLatency.labels("hentNyeste").startTimer()
return transaction(db) {
InntektsmeldingEntitet.run {
select { (forespoerselId eq forespørselId) and dokument.isNotNull() }.orderBy(innsendt, SortOrder.DESC)
}.firstOrNull()?.getOrNull(InntektsmeldingEntitet.dokument)
}
.firstOrNull()
?.getOrNull(InntektsmeldingEntitet.dokument)
}.also {
requestTimer.observeDuration()
}
Expand All @@ -67,7 +70,9 @@ class InntektsmeldingRepository(private val db: Database) {
fun oppdaterJournalpostId(journalpostId: String, forespørselId: String) {
val requestTimer = requestLatency.labels("oppdaterJournalpostId").startTimer()
transaction(db) {
InntektsmeldingEntitet.update({ (InntektsmeldingEntitet.forespoerselId eq forespørselId) and (InntektsmeldingEntitet.journalpostId eq null) }) {
InntektsmeldingEntitet.update(
where = { (InntektsmeldingEntitet.forespoerselId eq forespørselId) and (InntektsmeldingEntitet.journalpostId eq null) }
) {
it[InntektsmeldingEntitet.journalpostId] = journalpostId
}
}.also {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import no.nav.helsearbeidsgiver.felles.EksternInntektsmelding
import no.nav.helsearbeidsgiver.felles.inntektsmelding.felles.models.InntektsmeldingDokument
import no.nav.helsearbeidsgiver.felles.json.Jackson
import no.nav.helsearbeidsgiver.utils.json.jsonIgnoreUnknown
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.ColumnType
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.javatime.datetime
import org.jetbrains.exposed.sql.json.jsonb
Expand All @@ -15,34 +13,13 @@ object InntektsmeldingEntitet : Table("inntektsmelding") {
idSeqName = "inntektsmelding_id_seq"
)
val forespoerselId = varchar(name = "forespoersel_id", length = 40) references ForespoerselEntitet.forespoerselId
val dokument = json("dokument", InntektsmeldingDokument::class.java).nullable()
val dokument = jsonb<InntektsmeldingDokument>(
name = "dokument",
serialize = Jackson::toJson,
deserialize = Jackson::fromJson
).nullable()
val eksternInntektsmelding = jsonb<EksternInntektsmelding>("ekstern_inntektsmelding", jsonIgnoreUnknown).nullable()
val innsendt = datetime("innsendt")
val journalpostId = varchar("journalpostid", 30).nullable()
override val primaryKey = PrimaryKey(id, name = "id")
}

private fun <T : Any> Table.json(
name: String,
clazz: Class<T>
): Column<T> =
registerColumn(
name = name,
type = JsonColumnType(clazz)
)

class JsonColumnType<T : Any>(private val clazz: Class<T>) : ColumnType() {
override fun sqlType(): String =
"jsonb"

override fun valueFromDB(value: Any): T = Jackson.objectMapper.readValue(value as String, clazz)

override fun notNullValueToDB(value: Any): String =
Jackson.toJson(value)

override fun valueToString(value: Any?): String =
when (value) {
is Iterable<*> -> notNullValueToDB(value)
else -> super.valueToString(value)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE inntektsmelding
ALTER COLUMN dokument TYPE JSONB USING dokument::JSONB;
2 changes: 0 additions & 2 deletions integrasjonstest/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ dependencies {
implementation("com.zaxxer:HikariCP:$hikariVersion")
implementation("org.flywaydb:flyway-core:$flywayVersion")
implementation("org.jetbrains.exposed:exposed-core:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-dao:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-java-time:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-jdbc:$exposedVersion")
implementation("org.junit.jupiter:junit-jupiter:$junitJupiterVersion")

Expand Down
6 changes: 3 additions & 3 deletions integrasjonstest/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Dependency versions
exposedVersion=0.41.1
flywayVersion=9.16.3
exposedVersion=0.43.0
flywayVersion=9.22.1
hikariVersion=5.0.1
junitJupiterVersion=5.9.3
postgresqlVersion=42.6.0
testcontainersRedisJunitVersion=1.6.4
testcontainersVersion=1.18.0
testcontainersVersion=1.19.0

0 comments on commit cb29204

Please sign in to comment.