diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/SQLDB.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/SQLDB.java index 7e36cd7fe..16dca038f 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/SQLDB.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/SQLDB.java @@ -165,7 +165,7 @@ public abstract class SQLDB extends AbstractDatabase { new GeoInfoOptimizationPatch(), new TransferTableRemovalPatch(), new BadAFKThresholdValuePatch(), - new DeleteIPHashesPatch(), + new DeleteIPsPatch(), new ExtensionShowInPlayersTablePatch(), new ExtensionTableRowValueLengthPatch() }; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/DataStoreQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/DataStoreQueries.java index ac2b2a44f..5cbcb7e6b 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/DataStoreQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/DataStoreQueries.java @@ -136,9 +136,8 @@ public class DataStoreQueries { @Override public void prepare(PreparedStatement statement) throws SQLException { statement.setString(1, playerUUID.toString()); - statement.setString(2, geoInfo.getIp()); - statement.setString(3, geoInfo.getGeolocation()); - statement.setLong(4, geoInfo.getDate()); + statement.setString(2, geoInfo.getGeolocation()); + statement.setLong(3, geoInfo.getDate()); } }; } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/LargeStoreQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/LargeStoreQueries.java index fe64d5c65..d8fff9524 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/LargeStoreQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/LargeStoreQueries.java @@ -63,14 +63,12 @@ public class LargeStoreQueries { UUID playerUUID = playerEntry.getKey(); // Every GeoInfo for (GeoInfo info : playerEntry.getValue()) { - String ip = info.getIp(); String geoLocation = info.getGeolocation(); long lastUsed = info.getDate(); statement.setString(1, playerUUID.toString()); - statement.setString(2, ip); - statement.setString(3, geoLocation); - statement.setLong(4, lastUsed); + statement.setString(2, geoLocation); + statement.setLong(3, lastUsed); statement.addBatch(); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/GeoInfoQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/GeoInfoQueries.java index c1350f09f..b698a88f3 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/GeoInfoQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/objects/GeoInfoQueries.java @@ -48,7 +48,6 @@ public class GeoInfoQueries { */ public static Query>> fetchAllGeoInformation() { String sql = SELECT + - GeoInfoTable.IP + ',' + GeoInfoTable.GEOLOCATION + ',' + GeoInfoTable.LAST_USED + ',' + GeoInfoTable.USER_UUID + @@ -114,8 +113,7 @@ public class GeoInfoQueries { public static Query>> fetchServerGeoInformation(UUID serverUUID) { String sql = SELECT + GeoInfoTable.TABLE_NAME + '.' + GeoInfoTable.USER_UUID + ',' + GeoInfoTable.GEOLOCATION + ',' + - GeoInfoTable.LAST_USED + ',' + - GeoInfoTable.IP + + GeoInfoTable.LAST_USED + FROM + GeoInfoTable.TABLE_NAME + INNER_JOIN + UserInfoTable.TABLE_NAME + " on " + GeoInfoTable.TABLE_NAME + '.' + GeoInfoTable.USER_UUID + "=" + UserInfoTable.TABLE_NAME + '.' + UserInfoTable.USER_UUID + diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/sql/tables/GeoInfoTable.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/sql/tables/GeoInfoTable.java index d1a317649..9b25330e9 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/sql/tables/GeoInfoTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/sql/tables/GeoInfoTable.java @@ -19,7 +19,6 @@ package com.djrapitops.plan.storage.database.sql.tables; import com.djrapitops.plan.storage.database.DBType; import com.djrapitops.plan.storage.database.sql.parsing.CreateTableParser; import com.djrapitops.plan.storage.database.sql.parsing.Sql; -import com.djrapitops.plan.storage.database.transactions.patches.DeleteIPHashesPatch; import com.djrapitops.plan.storage.database.transactions.patches.GeoInfoLastUsedPatch; import com.djrapitops.plan.storage.database.transactions.patches.GeoInfoOptimizationPatch; import com.djrapitops.plan.storage.database.transactions.patches.Version10Patch; @@ -34,27 +33,23 @@ import static com.djrapitops.plan.storage.database.sql.parsing.Sql.WHERE; * {@link Version10Patch} * {@link GeoInfoLastUsedPatch} * {@link GeoInfoOptimizationPatch} - * {@link DeleteIPHashesPatch} * * @author Rsl1122 */ public class GeoInfoTable { - public static final String TABLE_NAME = "plan_ips"; + public static final String TABLE_NAME = "plan_geolocations"; public static final String ID = "id"; public static final String USER_UUID = "uuid"; - @Deprecated - public static final String IP = "ip"; public static final String GEOLOCATION = "geolocation"; public static final String LAST_USED = "last_used"; public static final String INSERT_STATEMENT = "INSERT INTO " + TABLE_NAME + " (" + USER_UUID + ',' - + IP + ',' + GEOLOCATION + ',' + LAST_USED - + ") VALUES (?, ?, ?, ?)"; + + ") VALUES (?, ?, ?)"; public static final String UPDATE_STATEMENT = "UPDATE " + TABLE_NAME + " SET " + LAST_USED + "=?" + @@ -69,7 +64,6 @@ public class GeoInfoTable { return CreateTableParser.create(TABLE_NAME, dbType) .column(ID, Sql.INT).primaryKey() .column(USER_UUID, Sql.varchar(36)).notNull() - .column(IP, Sql.varchar(39)).notNull() .column(GEOLOCATION, Sql.varchar(50)).notNull() .column(LAST_USED, Sql.LONG).notNull().defaultValue("0") .toString(); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/DeleteIPHashesPatch.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/DeleteIPHashesPatch.java deleted file mode 100644 index 5db82922e..000000000 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/DeleteIPHashesPatch.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * This file is part of Player Analytics (Plan). - * - * Plan is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License v3 as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Plan is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Plan. If not, see . - */ -package com.djrapitops.plan.storage.database.transactions.patches; - -import com.djrapitops.plan.storage.database.queries.HasMoreThanZeroQueryStatement; -import com.djrapitops.plan.storage.database.sql.tables.GeoInfoTable; -import com.djrapitops.plan.storage.database.transactions.ExecStatement; - -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.sql.Types; - -import static com.djrapitops.plan.storage.database.sql.parsing.Sql.*; - -/** - * Patch for removing ip_hash values from plan_ips table. - *

- * The patch is a response to a concern: - * "Hashed IP addresses are pseudonymised not anonymised and can be easily decoded using a rainbow table". - * - * @author Rsl1122 - */ -@Deprecated -public class DeleteIPHashesPatch extends Patch { - - private static final String IP_HASH = "ip_hash"; - - private boolean hasNoHashColumn; - - @Override - public boolean hasBeenApplied() { - hasNoHashColumn = !hasColumn(GeoInfoTable.TABLE_NAME, IP_HASH); - - String sql = SELECT + "COUNT(1) as c" + FROM + GeoInfoTable.TABLE_NAME + - WHERE + IP_HASH + IS_NOT_NULL; - - return hasNoHashColumn || !query(new HasMoreThanZeroQueryStatement(sql) { - @Override - public void prepare(PreparedStatement statement) { - /* No variables needed */ - } - }); - } - - @Override - protected void applyPatch() { - if (hasNoHashColumn) { - return; - } - - String sql = "UPDATE " + GeoInfoTable.TABLE_NAME + " SET ip_hash=?" + WHERE + IP_HASH + IS_NOT_NULL; - execute(new ExecStatement(sql) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setNull(1, Types.VARCHAR); - } - }); - } - -} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/DeleteIPsPatch.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/DeleteIPsPatch.java new file mode 100644 index 000000000..6d50fd499 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/DeleteIPsPatch.java @@ -0,0 +1,68 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.storage.database.transactions.patches; + +import com.djrapitops.plan.storage.database.sql.tables.GeoInfoTable; + +import static com.djrapitops.plan.storage.database.sql.parsing.Sql.DISTINCT; +import static com.djrapitops.plan.storage.database.sql.parsing.Sql.FROM; + +/** + * Patch that replaces plan_ips with plan_geolocations table. + * + * @author Rsl1122 + */ +public class DeleteIPsPatch extends Patch { + + private final String tempTableName; + + public DeleteIPsPatch() { + tempTableName = "temp_ips"; + } + + @Override + public boolean hasBeenApplied() { + return !hasTable("plan_ips"); + } + + @Override + protected void applyPatch() { + tempOldTable(); + + dropTable(GeoInfoTable.TABLE_NAME); + execute(GeoInfoTable.createTableSQL(dbType)); + + execute("INSERT INTO " + GeoInfoTable.TABLE_NAME + " (" + + GeoInfoTable.USER_UUID + ',' + + GeoInfoTable.LAST_USED + ',' + + GeoInfoTable.GEOLOCATION + + ") SELECT " + DISTINCT + + GeoInfoTable.USER_UUID + ',' + + GeoInfoTable.LAST_USED + ',' + + GeoInfoTable.GEOLOCATION + + FROM + tempTableName + ); + + dropTable(tempTableName); + } + + private void tempOldTable() { + if (!hasTable(tempTableName)) { + renameTable("plan_ips", tempTableName); + } + } +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/GeoInfoOptimizationPatch.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/GeoInfoOptimizationPatch.java index 18e9196ee..4fb29a00c 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/GeoInfoOptimizationPatch.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/patches/GeoInfoOptimizationPatch.java @@ -18,24 +18,25 @@ package com.djrapitops.plan.storage.database.transactions.patches; import com.djrapitops.plan.storage.database.sql.tables.GeoInfoTable; -import static com.djrapitops.plan.storage.database.sql.parsing.Sql.FROM; +import static com.djrapitops.plan.storage.database.sql.parsing.Sql.*; public class GeoInfoOptimizationPatch extends Patch { private String tempTableName; - private String tableName; + private String oldTableName; public GeoInfoOptimizationPatch() { - tableName = GeoInfoTable.TABLE_NAME; + oldTableName = "plan_ips"; tempTableName = "temp_ips"; } @Override public boolean hasBeenApplied() { - return hasColumn(tableName, GeoInfoTable.ID) - && hasColumn(tableName, GeoInfoTable.USER_UUID) - && !hasColumn(tableName, "user_id") - && !hasTable(tempTableName); // If this table exists the patch has failed to finish. + return !hasTable(oldTableName) + || (hasColumn(oldTableName, GeoInfoTable.ID) + && hasColumn(oldTableName, GeoInfoTable.USER_UUID) + && !hasColumn(oldTableName, "user_id") + && !hasTable(tempTableName)); // If this table exists the patch has failed to finish. } @Override @@ -43,14 +44,12 @@ public class GeoInfoOptimizationPatch extends Patch { tempOldTable(); execute(GeoInfoTable.createTableSQL(dbType)); - execute("INSERT INTO " + tableName + " (" + + execute("INSERT INTO " + GeoInfoTable.TABLE_NAME + " (" + GeoInfoTable.USER_UUID + ',' + - GeoInfoTable.IP + ',' + GeoInfoTable.LAST_USED + ',' + GeoInfoTable.GEOLOCATION + - ") SELECT " + + ") " + SELECT + DISTINCT + "(SELECT plan_users.uuid FROM plan_users WHERE plan_users.id = " + tempTableName + ".user_id LIMIT 1), " + - GeoInfoTable.IP + ',' + GeoInfoTable.LAST_USED + ',' + GeoInfoTable.GEOLOCATION + FROM + tempTableName @@ -61,7 +60,7 @@ public class GeoInfoOptimizationPatch extends Patch { private void tempOldTable() { if (!hasTable(tempTableName)) { - renameTable(tableName, tempTableName); + renameTable(oldTableName, tempTableName); } } }