diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/RemoveDanglingServerDataPatch.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/RemoveDanglingServerDataPatch.java index 023345a1e..d2d98601a 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/RemoveDanglingServerDataPatch.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/RemoveDanglingServerDataPatch.java @@ -18,10 +18,7 @@ package com.djrapitops.plan.storage.database.transactions.patches; import com.djrapitops.plan.storage.database.queries.Query; import com.djrapitops.plan.storage.database.queries.QueryAllStatement; -import com.djrapitops.plan.storage.database.sql.tables.PingTable; -import com.djrapitops.plan.storage.database.sql.tables.SessionsTable; -import com.djrapitops.plan.storage.database.sql.tables.UserInfoTable; -import com.djrapitops.plan.storage.database.sql.tables.WorldTimesTable; +import com.djrapitops.plan.storage.database.sql.tables.*; import com.djrapitops.plan.storage.database.transactions.ExecBatchStatement; import com.djrapitops.plan.storage.database.transactions.Executable; @@ -68,19 +65,23 @@ public class RemoveDanglingServerDataPatch extends Patch { @Override protected void applyPatch() { - if (!userInfoTableOk) fixTable(UserInfoTable.TABLE_NAME); - if (!pingTableOk) fixTable(PingTable.TABLE_NAME); - if (!worldTimesTableOk) fixTable(WorldTimesTable.TABLE_NAME); - if (!sessionsTableOk) fixTable(SessionsTable.TABLE_NAME); + Set serverUuids = query(getServerUuids()); - if (pingOptimizationFailed) fixTable("temp_ping"); - if (userInfoOptimizationFailed) fixTable("temp_user_info"); - if (worldTimesOptimizationFailed) fixTable("temp_world_times"); - if (sessionsOptimizationFailed) fixTable("temp_sessions"); + if (!userInfoTableOk) fixTable(UserInfoTable.TABLE_NAME, serverUuids); + if (!pingTableOk) fixTable(PingTable.TABLE_NAME, serverUuids); + if (!worldTimesTableOk) fixTable(WorldTimesTable.TABLE_NAME, serverUuids); + if (!sessionsTableOk) fixTable(SessionsTable.TABLE_NAME, serverUuids); + + if (pingOptimizationFailed) fixTable("temp_ping", serverUuids); + if (userInfoOptimizationFailed) fixTable("temp_user_info", serverUuids); + if (worldTimesOptimizationFailed) fixTable("temp_world_times", serverUuids); + if (sessionsOptimizationFailed) fixTable("temp_sessions", serverUuids); } - private void fixTable(String tableName) { - Set badUuids = query(getBadUuids(tableName)); + private void fixTable(String tableName, Set serverUuids) { + Set badUuids = query(getServerUuids(tableName)); + badUuids.removeAll(serverUuids); + if (!badUuids.isEmpty()) { execute(deleteBadUuids(tableName, badUuids)); } @@ -99,10 +100,23 @@ public class RemoveDanglingServerDataPatch extends Patch { }; } - private Query> getBadUuids(String tableName) { - String sql = "SELECT g.uuid FROM " + tableName + " g " + - "LEFT JOIN plan_servers s on s.uuid=g.server_uuid " + - "WHERE s.uuid IS NULL"; + private Query> getServerUuids() { + String sql = "SELECT uuid FROM " + ServerTable.TABLE_NAME; + + return new QueryAllStatement>(sql) { + @Override + public Set processResults(ResultSet set) throws SQLException { + HashSet uuids = new HashSet<>(); + while (set.next()) { + uuids.add(set.getString("uuid")); + } + return uuids; + } + }; + } + + private Query> getServerUuids(String tableName) { + String sql = "SELECT DISTINCT server_uuid FROM " + tableName; return new QueryAllStatement>(sql) { @Override diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/RemoveDanglingUserDataPatch.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/RemoveDanglingUserDataPatch.java index 5803371b2..e4bb5eef9 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/RemoveDanglingUserDataPatch.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/RemoveDanglingUserDataPatch.java @@ -71,21 +71,25 @@ public class RemoveDanglingUserDataPatch extends Patch { @Override protected void applyPatch() { - if (!userInfoTableOk) fixTable(UserInfoTable.TABLE_NAME); - if (!geolocationsTableOk) fixTable(GeoInfoTable.TABLE_NAME); - if (!pingTableOk) fixTable(PingTable.TABLE_NAME); - if (!worldTimesTableOk) fixTable(WorldTimesTable.TABLE_NAME); - if (!sessionsTableOk) fixTable(SessionsTable.TABLE_NAME); + Set uuids = query(getUuids(UsersTable.TABLE_NAME)); - if (pingOptimizationFailed) fixTable("temp_ping"); - if (userInfoOptimizationFailed) fixTable("temp_user_info"); - if (worldTimesOptimizationFailed) fixTable("temp_world_times"); - if (sessionsOptimizationFailed) fixTable("temp_sessions"); - if (geolocationOptimizationFailed) fixTable("temp_geoinformation"); + if (!userInfoTableOk) fixTable(UserInfoTable.TABLE_NAME, uuids); + if (!geolocationsTableOk) fixTable(GeoInfoTable.TABLE_NAME, uuids); + if (!pingTableOk) fixTable(PingTable.TABLE_NAME, uuids); + if (!worldTimesTableOk) fixTable(WorldTimesTable.TABLE_NAME, uuids); + if (!sessionsTableOk) fixTable(SessionsTable.TABLE_NAME, uuids); + + if (pingOptimizationFailed) fixTable("temp_ping", uuids); + if (userInfoOptimizationFailed) fixTable("temp_user_info", uuids); + if (worldTimesOptimizationFailed) fixTable("temp_world_times", uuids); + if (sessionsOptimizationFailed) fixTable("temp_sessions", uuids); + if (geolocationOptimizationFailed) fixTable("temp_geoinformation", uuids); } - private void fixTable(String tableName) { - Set badUuids = query(getBadUuids(tableName)); + private void fixTable(String tableName, Set uuids) { + Set badUuids = query(getUuids(tableName)); + badUuids.removeAll(uuids); + if (!badUuids.isEmpty()) { execute(deleteBadUuids(tableName, badUuids)); } @@ -104,10 +108,8 @@ public class RemoveDanglingUserDataPatch extends Patch { }; } - private Query> getBadUuids(String tableName) { - String sql = "SELECT g.uuid FROM " + tableName + " g " + - "LEFT JOIN plan_users u on u.uuid=g.uuid " + - "WHERE u.uuid IS NULL"; + private Query> getUuids(String tableName) { + String sql = "SELECT DISTINCT uuid FROM " + tableName; return new QueryAllStatement>(sql) { @Override