From d0030fc6e30826d25189401e023d870fad2474da Mon Sep 17 00:00:00 2001 From: AuroraLS3 <24460436+aurorals3@users.noreply.github.com> Date: Thu, 29 Dec 2022 20:31:23 +0200 Subject: [PATCH] Fix BadJoinAddressDataCorrectionPatch --- .../BadJoinAddressDataCorrectionPatch.java | 9 +++-- ...BadJoinAddressDataCorrectionPatchTest.java | 33 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/BadJoinAddressDataCorrectionPatch.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/BadJoinAddressDataCorrectionPatch.java index a78bb9024..0614840e0 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/BadJoinAddressDataCorrectionPatch.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/BadJoinAddressDataCorrectionPatch.java @@ -80,14 +80,17 @@ public class BadJoinAddressDataCorrectionPatch extends Patch { } private void updateOldIds(Map oldToNewIds) { - String sql = "UPDATE " + SessionsTable.TABLE_NAME + " SET " + SessionsTable.JOIN_ADDRESS_ID + "=?" + + String sql = "UPDATE " + SessionsTable.TABLE_NAME + + " SET " + SessionsTable.JOIN_ADDRESS_ID + "=?" + WHERE + SessionsTable.JOIN_ADDRESS_ID + "=?"; execute(new ExecBatchStatement(sql) { @Override public void prepare(PreparedStatement statement) throws SQLException { for (Map.Entry entry : oldToNewIds.entrySet()) { - statement.setInt(1, entry.getKey()); - statement.setInt(2, entry.getValue()); + Integer newId = entry.getValue(); + Integer oldId = entry.getKey(); + statement.setInt(1, newId); + statement.setInt(2, oldId); statement.addBatch(); } } diff --git a/Plan/common/src/test/java/com/djrapitops/plan/storage/database/transactions/patches/BadJoinAddressDataCorrectionPatchTest.java b/Plan/common/src/test/java/com/djrapitops/plan/storage/database/transactions/patches/BadJoinAddressDataCorrectionPatchTest.java index ff022645f..1f0b63094 100644 --- a/Plan/common/src/test/java/com/djrapitops/plan/storage/database/transactions/patches/BadJoinAddressDataCorrectionPatchTest.java +++ b/Plan/common/src/test/java/com/djrapitops/plan/storage/database/transactions/patches/BadJoinAddressDataCorrectionPatchTest.java @@ -16,12 +16,16 @@ */ package com.djrapitops.plan.storage.database.transactions.patches; +import com.djrapitops.plan.gathering.domain.FinishedSession; +import com.djrapitops.plan.gathering.domain.event.JoinAddress; import com.djrapitops.plan.storage.database.Database; import com.djrapitops.plan.storage.database.DatabaseTestPreparer; import com.djrapitops.plan.storage.database.queries.objects.JoinAddressQueries; import com.djrapitops.plan.storage.database.sql.tables.JoinAddressTable; import com.djrapitops.plan.storage.database.transactions.Transaction; import com.djrapitops.plan.storage.database.transactions.events.StoreJoinAddressTransaction; +import com.djrapitops.plan.storage.database.transactions.events.StoreSessionTransaction; +import com.djrapitops.plan.storage.database.transactions.events.StoreWorldNameTransaction; import org.junit.jupiter.api.Test; import utilities.RandomData; @@ -74,6 +78,35 @@ public interface BadJoinAddressDataCorrectionPatchTest extends DatabaseTestPrepa assertEquals(expected, result); } + @Test + default void joinAddressWithBadDataIsCorrectedWithOriginalPlusSessions() { + Database db = db(); + String correct = "correct_address"; + String bad = "correct_address\u000062.6.…zwyzyty0zmnlowzmmtqynmm"; + db.executeTransaction(new StoreJoinAddressTransaction(correct)); + db.executeTransaction(new StoreJoinAddressTransaction(bad)); + + executeTransactions(new StoreWorldNameTransaction(serverUUID(), worlds[0])); + executeTransactions(new StoreWorldNameTransaction(serverUUID(), worlds[1])); + FinishedSession session = RandomData.randomSession(serverUUID(), worlds, playerUUID, player2UUID); + session.getExtraData().put(JoinAddress.class, new JoinAddress(bad)); + executeTransactions(new StoreSessionTransaction(session)); + + Set preTestExpected = Set.of(bad); + Set preTestResult = db.query(JoinAddressQueries.latestJoinAddresses()).keySet(); + assertEquals(preTestExpected, preTestResult); + + System.out.println(db.queryList("SELECT join_address_id FROM plan_sessions", set -> set.getInt(1))); + BadJoinAddressDataCorrectionPatch patch = new BadJoinAddressDataCorrectionPatch(); + db.executeTransaction(patch); + assertTrue(patch.wasApplied()); + System.out.println(db.queryList("SELECT join_address_id FROM plan_sessions", set -> set.getInt(1))); + + Set expected = Set.of(correct); + Set result = db.query(JoinAddressQueries.latestJoinAddresses()).keySet(); + assertEquals(expected, result); + } + @Test default void joinAddressWithBadDataIsCorrectedWithoutOriginal() { Database db = db();