From 8e27e95fd990c07222d80a347fa656f45fcb2caa Mon Sep 17 00:00:00 2001 From: Kevin Belisle Date: Fri, 3 Sep 2021 11:10:26 -0400 Subject: [PATCH] Fix aggregates - again. --- app/src/main/kotlin/xyz/etztech/stonks/App.kt | 8 ++++++++ .../xyz/etztech/stonks/StatisticsImporter.kt | 16 ++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/app/src/main/kotlin/xyz/etztech/stonks/App.kt b/app/src/main/kotlin/xyz/etztech/stonks/App.kt index f0fb2ff..f0a2834 100644 --- a/app/src/main/kotlin/xyz/etztech/stonks/App.kt +++ b/app/src/main/kotlin/xyz/etztech/stonks/App.kt @@ -134,6 +134,14 @@ fun initH2Server( it[KeyValue.value] = 1 } } + + if (schemaVersion < 2) { + println("Migrating database to schemaVersion 2.") + + TransactionManager.current().exec("TRUNCATE TABLE AGGREGATESTATISTICS") + + KeyValue.update({ KeyValue.key eq "SchemaVersion" }) { it[KeyValue.value] = 2 } + } } return database diff --git a/app/src/main/kotlin/xyz/etztech/stonks/StatisticsImporter.kt b/app/src/main/kotlin/xyz/etztech/stonks/StatisticsImporter.kt index 09b08a0..82e67c8 100644 --- a/app/src/main/kotlin/xyz/etztech/stonks/StatisticsImporter.kt +++ b/app/src/main/kotlin/xyz/etztech/stonks/StatisticsImporter.kt @@ -144,7 +144,13 @@ object StatisticsImporter { WHERE array_contains(array['minecraft:mined'], Live."Type") GROUP BY Live."Type", Live."Name", Live."PlayerId" ) as LiveMax - LEFT JOIN AGGREGATESTATISTICS as Agg + LEFT JOIN ( + SELECT AGGREGATESTATISTICS."Type", + AGGREGATESTATISTICS."Name", + MAX(AGGREGATESTATISTICS."Value") as "Value" + FROM AGGREGATESTATISTICS + GROUP BY AGGREGATESTATISTICS."Type", AGGREGATESTATISTICS."Name" + ) as Agg ON LiveMax."Type" = Agg."Type" GROUP BY LiveMax."Type" HAVING sum(LiveMax."Value") <> max(Agg."Value") OR max(Agg."Value") IS NULL; @@ -163,7 +169,13 @@ object StatisticsImporter { OR array_contains(array['minecraft:killed', 'minecraft:killed_by'], Live."Type") GROUP BY Live."Type", Live."Name", Live."PlayerId" ) as LiveMax - LEFT JOIN AGGREGATESTATISTICS as Agg + LEFT JOIN ( + SELECT AGGREGATESTATISTICS."Type", + AGGREGATESTATISTICS."Name", + MAX(AGGREGATESTATISTICS."Value") as "Value" + FROM AGGREGATESTATISTICS + GROUP BY AGGREGATESTATISTICS."Type", AGGREGATESTATISTICS."Name" + ) as Agg ON LiveMax."Type" = Agg."Type" AND LiveMax."Name" = Agg."Name" GROUP BY LiveMax."Type", LiveMax."Name" HAVING SUM(LiveMax."Value") <> MAX(Agg."Value") OR MAX(Agg."Value") IS NULL;