Skip to content

Commit 1b906b7

Browse files
committed
Migrate also timestamp from inntekt_v1_arena_mapping to inntekt_v1_person_mapping
navikt/dagpenger#397
1 parent 13253ef commit 1b906b7

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

src/main/kotlin/no/nav/dagpenger/inntekt/db/ArenaMappingMigrator.kt

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package no.nav.dagpenger.inntekt.db
22

33
import javax.sql.DataSource
4+
import kotliquery.TransactionalSession
45
import kotliquery.queryOf
56
import kotliquery.sessionOf
67
import kotliquery.using
@@ -16,49 +17,46 @@ internal class ArenaMappingMigrator(private val datasource: DataSource) {
1617

1718
@Language("sql")
1819
private val statement: String = """
19-
INSERT INTO inntekt_v1_person_mapping (inntektid, aktørid, vedtakid, beregningsdato)
20-
SELECT inntektid, aktørid, vedtakid, beregningsdato
20+
INSERT INTO inntekt_v1_person_mapping (inntektid, aktørid, vedtakid, beregningsdato, timestamp)
21+
SELECT inntektid, aktørid, vedtakid, beregningsdato, timestamp
2122
FROM inntekt_v1_arena_mapping
2223
WHERE inntektid NOT IN (SELECT inntektid from inntekt_v1_person_mapping)
2324
""".trimIndent()
2425

2526
fun migrate(): Int {
2627
try {
27-
val locked = lock()
28-
return if (locked) {
29-
logger.info { "Obtained lock for migrator" }
30-
val rowsMigrated = using(sessionOf(datasource)) { session ->
31-
session.transaction {
32-
it.run(queryOf(statement).asUpdate)
28+
val rowsMigrated = using(sessionOf(datasource)) { session ->
29+
session.transaction { transaction ->
30+
if (lock(transaction)) {
31+
logger.info { "Obtained lock for migrator" }
32+
transaction.run(queryOf(statement).asUpdate)
33+
} else {
34+
logger.info { "Could not obtain lock for migrator" }
35+
0
3336
}
3437
}
35-
36-
logger.info { "Migrated $rowsMigrated rows from inntekt_v1_arena_mapping to inntekt_v1_person_mapping" }
37-
rowsMigrated
38-
} else {
39-
logger.info { "Could not obtain lock for migrator" }
40-
0
4138
}
39+
40+
logger.info { "Migrated $rowsMigrated rows from inntekt_v1_arena_mapping to inntekt_v1_person_mapping" }
41+
return rowsMigrated
4242
} finally {
4343
val unlocked = unlock()
4444
logger.info { "Unlocked = $unlocked for migrator" }
4545
}
4646
}
4747

48-
private fun unlock(): Boolean {
49-
return using(sessionOf(datasource)) { session ->
50-
session.run(
51-
queryOf("select pg_advisory_unlock($lockKey)").map {
52-
it.string(1) == "t"
53-
}.asSingle
54-
) ?: false
55-
}
48+
private fun lock(session: TransactionalSession): Boolean {
49+
return session.run(
50+
queryOf("select pg_try_advisory_xact_lock($lockKey)").map {
51+
it.string(1) == "t"
52+
}.asSingle
53+
) ?: false
5654
}
5755

58-
private fun lock(): Boolean {
56+
private fun unlock(): Boolean {
5957
return using(sessionOf(datasource)) { session ->
6058
session.run(
61-
queryOf("select pg_try_advisory_lock($lockKey)").map {
59+
queryOf("select pg_advisory_unlock($lockKey)").map {
6260
it.string(1) == "t"
6361
}.asSingle
6462
) ?: false

src/test/kotlin/no/nav/dagpenger/inntekt/db/ArenaMappingMigratorTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import no.nav.dagpenger.inntekt.withMigratedDb
1717
import org.junit.jupiter.api.Test
1818
import org.postgresql.util.PGobject
1919

20-
internal class ArenaMappingMigratorTest {
20+
internal class ArenaMappingMigratorTest() {
2121

2222
companion object {
2323

0 commit comments

Comments
 (0)