Removed duplicates from plan_ips table, properly anonymised ips.

This commit is contained in:
Rsl1122 2018-05-31 09:17:40 +03:00
parent 3213b5518b
commit 7a80d52142
5 changed files with 100 additions and 23 deletions

View File

@ -139,7 +139,7 @@ public abstract class SQLDB extends Database {
if (newDatabase) {
Log.info("New Database created.");
versionTable.setVersion(17);
versionTable.setVersion(18);
}
int version = versionTable.getVersion();
@ -187,6 +187,10 @@ public abstract class SQLDB extends Database {
geoInfoTable.alterTableV17();
versionTable.setVersion(17);
}
if (version < 18) {
geoInfoTable.alterTableV18();
// version set in the runnable in above method
}
} catch (SQLException e) {
throw new DBInitException("Failed to set-up Database", e);
}
@ -256,6 +260,7 @@ public abstract class SQLDB extends Database {
private void clean() throws SQLException {
tpsTable.clean();
transferTable.clean();
geoInfoTable.clean();
long now = System.currentTimeMillis();
long keepActiveAfter = now - TimeAmount.DAY.ms() * Settings.KEEP_INACTIVE_PLAYERS_DAYS.getNumber();

View File

@ -10,6 +10,7 @@ import com.djrapitops.plan.system.database.databases.sql.statements.Column;
import com.djrapitops.plan.system.database.databases.sql.statements.Select;
import com.djrapitops.plan.system.database.databases.sql.statements.Sql;
import com.djrapitops.plan.system.database.databases.sql.statements.TableSqlParser;
import com.djrapitops.plan.system.database.databases.sql.tables.move.Version18TransferTable;
import com.djrapitops.plan.system.settings.Settings;
import com.djrapitops.plan.utilities.comparators.GeoInfoComparator;
import com.djrapitops.plugin.api.utility.log.Log;
@ -75,8 +76,10 @@ public class GeoInfoTable extends UserIDTable {
public void alterTableV17() {
addColumns(Col.IP_HASH.get() + " varchar(200) DEFAULT ''");
}
RunnableFactory.createNew("DB Version 16->17", new AbsRunnable() {
public void alterTableV18() {
RunnableFactory.createNew("DB Version 17->18", new AbsRunnable() {
@Override
public void run() {
try {
@ -92,14 +95,20 @@ public class GeoInfoTable extends UserIDTable {
for (List<GeoInfo> geoInfos : allGeoInfo.values()) {
for (GeoInfo geoInfo : geoInfos) {
try {
if (geoInfo.getIp().endsWith(".xx.xx")) {
continue;
}
GeoInfo updatedInfo = new GeoInfo(
geoInfo.getIp(),
geoInfo.getGeolocation(),
geoInfo.getLastUsed()
);
System.out.println(geoInfo.getIp());
System.out.println(updatedInfo.getIp());
statement.setString(1, updatedInfo.getIp());
statement.setString(2, updatedInfo.getIpHash());
statement.setString(3, geoInfo.getIp());
statement.addBatch();
} catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
if (Settings.DEV_MODE.isTrue()) {
Log.toLog(this.getClass(), e);
@ -109,11 +118,17 @@ public class GeoInfoTable extends UserIDTable {
}
}
});
} catch (SQLException e) {
new Version18TransferTable(db).alterTableV18();
db.setVersion(18);
} catch (SQLException | DBInitException e) {
Log.toLog(this.getClass(), e);
}
}
});
}).runTaskAsynchronously();
}
public void clean() {
}
public List<GeoInfo> getGeoInfo(UUID uuid) throws SQLException {

View File

@ -0,0 +1,36 @@
package com.djrapitops.plan.system.database.databases.sql.tables.move;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import com.djrapitops.plan.system.database.databases.sql.tables.Table;
import java.sql.SQLException;
/**
* Abstract table used for transferring a whole table to a new table.
*
* @author Rsl1122
*/
public class TransferTable extends Table {
public TransferTable(SQLDB db) {
super("", db);
}
@Override
public void createTable() {
throw new IllegalStateException("Method not supposed to be used on this table.");
}
protected void renameTable(String from, String to) throws SQLException {
String sql = usingMySQL ?
"RENAME TABLE " + from + " TO " + to :
"ALTER TABLE " + from + " RENAME TO " + to;
execute(sql);
}
protected void dropTable(String name) throws SQLException {
String sql = "DROP TABLE " + name;
execute(sql);
}
}

View File

@ -0,0 +1,38 @@
package com.djrapitops.plan.system.database.databases.sql.tables.move;
import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import java.sql.SQLException;
/**
* DB Schema v17 -> 18 table.
* <p>
* Required due to a bug where duplicate rows were inserted.
*
* @author Rsl1122
*/
public class Version18TransferTable extends TransferTable {
public Version18TransferTable(SQLDB db) throws SQLException {
super(db);
}
public void alterTableV18() throws SQLException, DBInitException {
String tempTableName = "plan_ips_temp";
String ipTableName = "plan_ips";
try {
renameTable(ipTableName, tempTableName);
} catch (SQLException e) {
// Temp table already exists
if (!e.getMessage().contains("plan_ips_temp")) {
throw e;
}
}
db.getGeoInfoTable().createTable();
execute("INSERT INTO plan_ips (" +
"user_id, ip, ip_hash, geolocation, last_used" +
") SELECT user_id, ip, ip_hash, geolocation, MAX(last_used) FROM plan_ips_temp GROUP BY ip_hash, user_id");
dropTable(tempTableName);
}
}

View File

@ -20,12 +20,12 @@ import java.util.Optional;
*
* @author Rsl1122
*/
public class Version8TransferTable extends Table {
public class Version8TransferTable extends TransferTable {
private final int serverID;
public Version8TransferTable(SQLDB db) throws SQLException {
super("", db);
super(db);
Optional<Integer> serverID = db.getServerTable().getServerID(ServerInfo.getServerUUID());
if (!serverID.isPresent()) {
throw new IllegalStateException("Server UUID was not registered, try rebooting the plugin.");
@ -33,23 +33,6 @@ public class Version8TransferTable extends Table {
this.serverID = serverID.get();
}
@Override
public void createTable() {
throw new IllegalStateException("Method not supposed to be used on this table.");
}
private void renameTable(String from, String to) throws SQLException {
String sql = usingMySQL ?
"RENAME TABLE " + from + " TO " + to :
"ALTER TABLE " + from + " RENAME TO " + to;
execute(sql);
}
private void dropTable(String name) throws SQLException {
String sql = "DROP TABLE " + name;
execute(sql);
}
public void alterTablesToV10() throws SQLException, DBInitException {
Benchmark.start("Schema copy from 8 to 10");
copyCommandUsage();