Removed IP column from plan_ips

This commit is contained in:
Rsl1122 2019-09-28 11:53:42 +03:00
parent 21d255b611
commit 5af4fea2b4
8 changed files with 87 additions and 105 deletions

View File

@ -165,7 +165,7 @@ public abstract class SQLDB extends AbstractDatabase {
new GeoInfoOptimizationPatch(), new GeoInfoOptimizationPatch(),
new TransferTableRemovalPatch(), new TransferTableRemovalPatch(),
new BadAFKThresholdValuePatch(), new BadAFKThresholdValuePatch(),
new DeleteIPHashesPatch(), new DeleteIPsPatch(),
new ExtensionShowInPlayersTablePatch(), new ExtensionShowInPlayersTablePatch(),
new ExtensionTableRowValueLengthPatch() new ExtensionTableRowValueLengthPatch()
}; };

View File

@ -136,9 +136,8 @@ public class DataStoreQueries {
@Override @Override
public void prepare(PreparedStatement statement) throws SQLException { public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, playerUUID.toString()); statement.setString(1, playerUUID.toString());
statement.setString(2, geoInfo.getIp()); statement.setString(2, geoInfo.getGeolocation());
statement.setString(3, geoInfo.getGeolocation()); statement.setLong(3, geoInfo.getDate());
statement.setLong(4, geoInfo.getDate());
} }
}; };
} }

View File

@ -63,14 +63,12 @@ public class LargeStoreQueries {
UUID playerUUID = playerEntry.getKey(); UUID playerUUID = playerEntry.getKey();
// Every GeoInfo // Every GeoInfo
for (GeoInfo info : playerEntry.getValue()) { for (GeoInfo info : playerEntry.getValue()) {
String ip = info.getIp();
String geoLocation = info.getGeolocation(); String geoLocation = info.getGeolocation();
long lastUsed = info.getDate(); long lastUsed = info.getDate();
statement.setString(1, playerUUID.toString()); statement.setString(1, playerUUID.toString());
statement.setString(2, ip); statement.setString(2, geoLocation);
statement.setString(3, geoLocation); statement.setLong(3, lastUsed);
statement.setLong(4, lastUsed);
statement.addBatch(); statement.addBatch();
} }

View File

@ -48,7 +48,6 @@ public class GeoInfoQueries {
*/ */
public static Query<Map<UUID, List<GeoInfo>>> fetchAllGeoInformation() { public static Query<Map<UUID, List<GeoInfo>>> fetchAllGeoInformation() {
String sql = SELECT + String sql = SELECT +
GeoInfoTable.IP + ',' +
GeoInfoTable.GEOLOCATION + ',' + GeoInfoTable.GEOLOCATION + ',' +
GeoInfoTable.LAST_USED + ',' + GeoInfoTable.LAST_USED + ',' +
GeoInfoTable.USER_UUID + GeoInfoTable.USER_UUID +
@ -114,8 +113,7 @@ public class GeoInfoQueries {
public static Query<Map<UUID, List<GeoInfo>>> fetchServerGeoInformation(UUID serverUUID) { public static Query<Map<UUID, List<GeoInfo>>> fetchServerGeoInformation(UUID serverUUID) {
String sql = SELECT + GeoInfoTable.TABLE_NAME + '.' + GeoInfoTable.USER_UUID + ',' + String sql = SELECT + GeoInfoTable.TABLE_NAME + '.' + GeoInfoTable.USER_UUID + ',' +
GeoInfoTable.GEOLOCATION + ',' + GeoInfoTable.GEOLOCATION + ',' +
GeoInfoTable.LAST_USED + ',' + GeoInfoTable.LAST_USED +
GeoInfoTable.IP +
FROM + GeoInfoTable.TABLE_NAME + FROM + GeoInfoTable.TABLE_NAME +
INNER_JOIN + UserInfoTable.TABLE_NAME + " on " + INNER_JOIN + UserInfoTable.TABLE_NAME + " on " +
GeoInfoTable.TABLE_NAME + '.' + GeoInfoTable.USER_UUID + "=" + UserInfoTable.TABLE_NAME + '.' + UserInfoTable.USER_UUID + GeoInfoTable.TABLE_NAME + '.' + GeoInfoTable.USER_UUID + "=" + UserInfoTable.TABLE_NAME + '.' + UserInfoTable.USER_UUID +

View File

@ -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.DBType;
import com.djrapitops.plan.storage.database.sql.parsing.CreateTableParser; import com.djrapitops.plan.storage.database.sql.parsing.CreateTableParser;
import com.djrapitops.plan.storage.database.sql.parsing.Sql; 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.GeoInfoLastUsedPatch;
import com.djrapitops.plan.storage.database.transactions.patches.GeoInfoOptimizationPatch; import com.djrapitops.plan.storage.database.transactions.patches.GeoInfoOptimizationPatch;
import com.djrapitops.plan.storage.database.transactions.patches.Version10Patch; 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 Version10Patch}
* {@link GeoInfoLastUsedPatch} * {@link GeoInfoLastUsedPatch}
* {@link GeoInfoOptimizationPatch} * {@link GeoInfoOptimizationPatch}
* {@link DeleteIPHashesPatch}
* *
* @author Rsl1122 * @author Rsl1122
*/ */
public class GeoInfoTable { 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 ID = "id";
public static final String USER_UUID = "uuid"; public static final String USER_UUID = "uuid";
@Deprecated
public static final String IP = "ip";
public static final String GEOLOCATION = "geolocation"; public static final String GEOLOCATION = "geolocation";
public static final String LAST_USED = "last_used"; public static final String LAST_USED = "last_used";
public static final String INSERT_STATEMENT = "INSERT INTO " + TABLE_NAME + " (" public static final String INSERT_STATEMENT = "INSERT INTO " + TABLE_NAME + " ("
+ USER_UUID + ',' + USER_UUID + ','
+ IP + ','
+ GEOLOCATION + ',' + GEOLOCATION + ','
+ LAST_USED + LAST_USED
+ ") VALUES (?, ?, ?, ?)"; + ") VALUES (?, ?, ?)";
public static final String UPDATE_STATEMENT = "UPDATE " + TABLE_NAME + " SET " public static final String UPDATE_STATEMENT = "UPDATE " + TABLE_NAME + " SET "
+ LAST_USED + "=?" + + LAST_USED + "=?" +
@ -69,7 +64,6 @@ public class GeoInfoTable {
return CreateTableParser.create(TABLE_NAME, dbType) return CreateTableParser.create(TABLE_NAME, dbType)
.column(ID, Sql.INT).primaryKey() .column(ID, Sql.INT).primaryKey()
.column(USER_UUID, Sql.varchar(36)).notNull() .column(USER_UUID, Sql.varchar(36)).notNull()
.column(IP, Sql.varchar(39)).notNull()
.column(GEOLOCATION, Sql.varchar(50)).notNull() .column(GEOLOCATION, Sql.varchar(50)).notNull()
.column(LAST_USED, Sql.LONG).notNull().defaultValue("0") .column(LAST_USED, Sql.LONG).notNull().defaultValue("0")
.toString(); .toString();

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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.
* <p>
* 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);
}
});
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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);
}
}
}

View File

@ -18,24 +18,25 @@ package com.djrapitops.plan.storage.database.transactions.patches;
import com.djrapitops.plan.storage.database.sql.tables.GeoInfoTable; 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 { public class GeoInfoOptimizationPatch extends Patch {
private String tempTableName; private String tempTableName;
private String tableName; private String oldTableName;
public GeoInfoOptimizationPatch() { public GeoInfoOptimizationPatch() {
tableName = GeoInfoTable.TABLE_NAME; oldTableName = "plan_ips";
tempTableName = "temp_ips"; tempTableName = "temp_ips";
} }
@Override @Override
public boolean hasBeenApplied() { public boolean hasBeenApplied() {
return hasColumn(tableName, GeoInfoTable.ID) return !hasTable(oldTableName)
&& hasColumn(tableName, GeoInfoTable.USER_UUID) || (hasColumn(oldTableName, GeoInfoTable.ID)
&& !hasColumn(tableName, "user_id") && hasColumn(oldTableName, GeoInfoTable.USER_UUID)
&& !hasTable(tempTableName); // If this table exists the patch has failed to finish. && !hasColumn(oldTableName, "user_id")
&& !hasTable(tempTableName)); // If this table exists the patch has failed to finish.
} }
@Override @Override
@ -43,14 +44,12 @@ public class GeoInfoOptimizationPatch extends Patch {
tempOldTable(); tempOldTable();
execute(GeoInfoTable.createTableSQL(dbType)); execute(GeoInfoTable.createTableSQL(dbType));
execute("INSERT INTO " + tableName + " (" + execute("INSERT INTO " + GeoInfoTable.TABLE_NAME + " (" +
GeoInfoTable.USER_UUID + ',' + GeoInfoTable.USER_UUID + ',' +
GeoInfoTable.IP + ',' +
GeoInfoTable.LAST_USED + ',' + GeoInfoTable.LAST_USED + ',' +
GeoInfoTable.GEOLOCATION + GeoInfoTable.GEOLOCATION +
") SELECT " + ") " + SELECT + DISTINCT +
"(SELECT plan_users.uuid FROM plan_users WHERE plan_users.id = " + tempTableName + ".user_id LIMIT 1), " + "(SELECT plan_users.uuid FROM plan_users WHERE plan_users.id = " + tempTableName + ".user_id LIMIT 1), " +
GeoInfoTable.IP + ',' +
GeoInfoTable.LAST_USED + ',' + GeoInfoTable.LAST_USED + ',' +
GeoInfoTable.GEOLOCATION + GeoInfoTable.GEOLOCATION +
FROM + tempTableName FROM + tempTableName
@ -61,7 +60,7 @@ public class GeoInfoOptimizationPatch extends Patch {
private void tempOldTable() { private void tempOldTable() {
if (!hasTable(tempTableName)) { if (!hasTable(tempTableName)) {
renameTable(tableName, tempTableName); renameTable(oldTableName, tempTableName);
} }
} }
} }