diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLDB.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLDB.java index 2f395df1f..bdc6ccb70 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLDB.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLDB.java @@ -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(); diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/GeoInfoTable.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/GeoInfoTable.java index 9c761cf32..78a322d47 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/GeoInfoTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/GeoInfoTable.java @@ -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 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 getGeoInfo(UUID uuid) throws SQLException { diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/move/TransferTable.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/move/TransferTable.java new file mode 100644 index 000000000..2f46464eb --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/move/TransferTable.java @@ -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); + } + +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/move/Version18TransferTable.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/move/Version18TransferTable.java new file mode 100644 index 000000000..e6494b0d4 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/move/Version18TransferTable.java @@ -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. + *

+ * 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); + } +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/move/Version8TransferTable.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/move/Version8TransferTable.java index e3f3fc0bf..c6aa9425b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/move/Version8TransferTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/move/Version8TransferTable.java @@ -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 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();