Skip to content

Commit 09fb636

Browse files
committed
Catch errors in migration
navikt/dagpenger#397
1 parent 1b906b7 commit 09fb636

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ internal class ArenaMappingMigrator(private val datasource: DataSource) {
2424
""".trimIndent()
2525

2626
fun migrate(): Int {
27+
val empty = 0
2728
try {
2829
val rowsMigrated = using(sessionOf(datasource)) { session ->
2930
session.transaction { transaction ->
@@ -32,13 +33,16 @@ internal class ArenaMappingMigrator(private val datasource: DataSource) {
3233
transaction.run(queryOf(statement).asUpdate)
3334
} else {
3435
logger.info { "Could not obtain lock for migrator" }
35-
0
36+
empty
3637
}
3738
}
3839
}
3940

4041
logger.info { "Migrated $rowsMigrated rows from inntekt_v1_arena_mapping to inntekt_v1_person_mapping" }
4142
return rowsMigrated
43+
} catch (err: Throwable) {
44+
logger.error(err) { "Failed to migrate from inntekt_v1_arena_mapping to inntekt_v1_person_mapping" }
45+
return empty
4246
} finally {
4347
val unlocked = unlock()
4448
logger.info { "Unlocked = $unlocked for migrator" }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ internal class PostgresInntektStore(private val dataSource: DataSource) : Inntek
8989
}
9090
}
9191

92-
private fun fetchInntektIdFromPersonMappingTable(inntektparametre: Inntektparametre): InntektId? {
92+
internal fun fetchInntektIdFromPersonMappingTable(inntektparametre: Inntektparametre): InntektId? {
9393
@Language("sql")
9494
val statement: String = """
9595
SELECT inntektId

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,19 @@ internal class ArenaMappingMigratorTest() {
4242
val inntektStore = PostgresInntektStore(TestDataSource.instance)
4343

4444
val ids = commands.mapNotNull { (_, command) ->
45+
inntektStore.fetchInntektIdFromPersonMappingTable(
46+
inntektparametre = command.inntektparametre
47+
)
48+
}
49+
50+
val fromOld = commands.mapNotNull { (_, command) ->
4551
inntektStore.getInntektId(
4652
inntektparametre = command.inntektparametre
4753
)
4854
}
4955

56+
fromOld shouldContainAll ids
57+
5058
rowsMigrated shouldBeExactly numberOfInserts
5159
ids.size shouldBeExactly numberOfInserts
5260
ids shouldContainAll commands.map { it.first }
@@ -68,7 +76,7 @@ internal class ArenaMappingMigratorTest() {
6876
val inntektStore = PostgresInntektStore(TestDataSource.instance)
6977

7078
val ids = commandsWithSameVedtakId.mapNotNull { (_, command) ->
71-
inntektStore.getInntektId(
79+
inntektStore.fetchInntektIdFromPersonMappingTable(
7280
inntektparametre = command.inntektparametre
7381
)
7482
}
@@ -102,7 +110,7 @@ internal class ArenaMappingMigratorTest() {
102110
val inntektStore = PostgresInntektStore(TestDataSource.instance)
103111

104112
val ids = commands.mapNotNull { (_, command) ->
105-
inntektStore.getInntektId(
113+
inntektStore.fetchInntektIdFromPersonMappingTable(
106114
inntektparametre = command.inntektparametre
107115
)
108116
}

0 commit comments

Comments
 (0)