Skip to content

Commit

Permalink
Make undefined join address id guesstimate based on existing data
Browse files Browse the repository at this point in the history
  • Loading branch information
AuroraLS3 committed Dec 30, 2022
1 parent a8dbc32 commit 8e88b3a
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import com.djrapitops.plan.storage.database.queries.objects.JoinAddressQueries;
import com.djrapitops.plan.storage.database.sql.tables.JoinAddressTable;
import com.djrapitops.plan.storage.database.sql.tables.SessionsTable;
import com.djrapitops.plan.storage.database.sql.tables.UserInfoTable;
import com.djrapitops.plan.storage.database.transactions.ExecBatchStatement;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Set;

import static com.djrapitops.plan.storage.database.sql.building.Sql.*;
Expand All @@ -40,27 +42,60 @@ public boolean hasBeenApplied() {

@Override
protected void applyPatch() {
Set<Integer> badAddressIds = getBadAddressIds();
Integer unknownId = getIdOfUnknownJoinAddress();
updateOldIds(badAddressIds, unknownId);
updateOldIds(getOldIdsToNewIds(unknownId));
}

private void updateOldIds(Set<Integer> badAddressIds, Integer unknownId) {
private void updateOldIds(List<IdRow> idRows) {
String sql = "UPDATE " + SessionsTable.TABLE_NAME +
" SET " + SessionsTable.JOIN_ADDRESS_ID + "=?" +
WHERE + SessionsTable.JOIN_ADDRESS_ID + "=?";
WHERE + SessionsTable.JOIN_ADDRESS_ID + "=?" +
AND + SessionsTable.USER_ID + "=?" +
AND + SessionsTable.SERVER_ID + "=?";
execute(new ExecBatchStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (Integer oldId : badAddressIds) {
statement.setInt(1, unknownId);
statement.setInt(2, oldId);
for (IdRow idRow : idRows) {
statement.setInt(1, idRow.newId);
statement.setInt(2, idRow.oldId);
statement.setInt(3, idRow.userId);
statement.setInt(4, idRow.serverId);
statement.addBatch();
}
}
});
}

List<IdRow> getOldIdsToNewIds(int unknownId) {
String sql = SELECT + DISTINCT +
"s." + SessionsTable.USER_ID + ',' +
"s." + SessionsTable.SERVER_ID + ',' +
SessionsTable.JOIN_ADDRESS_ID + " as old_id," +
"COALESCE(j." + JoinAddressTable.ID + ",?) as new_id" +
FROM + SessionsTable.TABLE_NAME + " s" +
LEFT_JOIN + UserInfoTable.TABLE_NAME + " u on " +
"u." + UserInfoTable.USER_ID + "=s." + SessionsTable.USER_ID +
AND + "u." + UserInfoTable.SERVER_ID + "=s." + SessionsTable.SERVER_ID +
LEFT_JOIN + JoinAddressTable.TABLE_NAME + " j on " +
"j." + JoinAddressTable.JOIN_ADDRESS + "=u." + UserInfoTable.JOIN_ADDRESS;

return query(db -> db.queryList(sql, result -> {
IdRow idRow = new IdRow();
idRow.userId = result.getInt(SessionsTable.USER_ID);
idRow.serverId = result.getInt(SessionsTable.SERVER_ID);
idRow.oldId = result.getInt("old_id");
idRow.newId = result.getInt("new_id");
return idRow;
}, unknownId));
}

private static class IdRow {
int userId;
int serverId;
int oldId;
int newId;
}

private Integer getIdOfUnknownJoinAddress() {
return query(JoinAddressQueries.getIdOfJoinAddress(JoinAddressTable.DEFAULT_VALUE_FOR_LOOKUP))
.orElseThrow(() -> new DBOpException("Could not get ID of join address properly"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,23 @@
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.sql.tables.SessionsTable;
import com.djrapitops.plan.storage.database.transactions.ExecStatement;
import com.djrapitops.plan.storage.database.transactions.events.StoreJoinAddressTransaction;
import com.djrapitops.plan.storage.database.transactions.events.StoreServerPlayerTransaction;
import com.djrapitops.plan.storage.database.transactions.events.StoreSessionTransaction;
import com.djrapitops.plan.storage.database.transactions.events.StoreWorldNameTransaction;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import utilities.RandomData;
import utilities.TestConstants;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Set;

import static com.djrapitops.plan.storage.database.sql.building.Sql.DELETE_FROM;
import static com.djrapitops.plan.storage.database.sql.building.Sql.WHERE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static com.djrapitops.plan.storage.database.sql.building.Sql.*;
import static org.junit.jupiter.api.Assertions.*;

public interface AfterBadJoinAddressDataCorrectionPatchTest extends DatabaseTestPreparer {

Expand Down Expand Up @@ -69,4 +71,92 @@ public void prepare(PreparedStatement statement) throws SQLException {
Set<String> result = db.query(JoinAddressQueries.latestJoinAddresses()).keySet();
assertEquals(expected, result);
}

@Test
@DisplayName("Join address id correction: Last known join address updated to all undefined ids, for specific server")
default void undefinedIdsAreUpdatedToLastKnownJoinAddressForThatServer() {
Database db = db();
String joinAddress = "correct_address";
db.executeTransaction(new StoreServerPlayerTransaction(TestConstants.PLAYER_ONE_UUID, 0L, TestConstants.PLAYER_ONE_NAME, serverUUID(), joinAddress));
db.executeTransaction(new StoreJoinAddressTransaction(joinAddress));
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(joinAddress));
executeTransactions(new StoreSessionTransaction(session));
execute(new ExecStatement("UPDATE " + SessionsTable.TABLE_NAME + " SET " + SessionsTable.JOIN_ADDRESS_ID + "=-1" +
WHERE + SessionsTable.JOIN_ADDRESS_ID + "=(" +
SELECT + JoinAddressTable.ID +
FROM + JoinAddressTable.TABLE_NAME +
WHERE + JoinAddressTable.JOIN_ADDRESS + "=?)") {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, joinAddress);
}
});

Set<String> preTestExpected = Set.of();
Set<String> preTestResult = db.query(JoinAddressQueries.latestJoinAddresses()).keySet();
assertEquals(preTestExpected, preTestResult);

AfterBadJoinAddressDataCorrectionPatch patch = new AfterBadJoinAddressDataCorrectionPatch();
db.executeTransaction(patch);
assertTrue(patch.wasApplied());

Set<String> expected = Set.of(joinAddress);
Set<String> result = db.query(JoinAddressQueries.latestJoinAddresses()).keySet();
assertEquals(expected, result);
}

@Test
@DisplayName("Join address id correction: Address updated to unknown when missing data for server")
default void undefinedIdsAreUpdatedToUnkownIfJoinAddressDoesntExist() {
Database db = db();
String joinAddress = "correct_address";
db.executeTransaction(new StoreJoinAddressTransaction(joinAddress));
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(joinAddress));
executeTransactions(new StoreSessionTransaction(session));
execute(new ExecStatement("UPDATE " + SessionsTable.TABLE_NAME + " SET " + SessionsTable.JOIN_ADDRESS_ID + "=-1" +
WHERE + SessionsTable.JOIN_ADDRESS_ID + "=(" +
SELECT + JoinAddressTable.ID +
FROM + JoinAddressTable.TABLE_NAME +
WHERE + JoinAddressTable.JOIN_ADDRESS + "=?)") {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, joinAddress);
}
});

Set<String> preTestExpected = Set.of();
Set<String> preTestResult = db.query(JoinAddressQueries.latestJoinAddresses()).keySet();
assertEquals(preTestExpected, preTestResult);

AfterBadJoinAddressDataCorrectionPatch patch = new AfterBadJoinAddressDataCorrectionPatch();
db.executeTransaction(patch);
assertTrue(patch.wasApplied());

Set<String> expected = Set.of(JoinAddressTable.DEFAULT_VALUE_FOR_LOOKUP);
Set<String> result = db.query(JoinAddressQueries.latestJoinAddresses()).keySet();
assertEquals(expected, result);
}

@Test
@DisplayName("Join address id correction: Address not updated if it is already defined")
default void definedIdsAreNotUpdated() {
Database db = db();
String joinAddress = "correct_address";
db.executeTransaction(new StoreJoinAddressTransaction(joinAddress));
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(joinAddress));
executeTransactions(new StoreSessionTransaction(session));

AfterBadJoinAddressDataCorrectionPatch patch = new AfterBadJoinAddressDataCorrectionPatch();
db.executeTransaction(patch);
assertFalse(patch.wasApplied());
}
}

0 comments on commit 8e88b3a

Please sign in to comment.