Database Patching from project-organization branch (with fixes)

This commit is contained in:
Rsl1122 2018-07-28 09:32:05 +03:00
parent 349c556db4
commit 9e9d9e25b8
25 changed files with 771 additions and 644 deletions

View File

@ -1,20 +1,22 @@
package com.djrapitops.plan.system.database.databases.sql;
import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.database.databases.operation.*;
import com.djrapitops.plan.system.database.databases.sql.operation.*;
import com.djrapitops.plan.system.database.databases.sql.patches.*;
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement;
import com.djrapitops.plan.system.database.databases.sql.tables.*;
import com.djrapitops.plan.system.database.databases.sql.tables.move.Version8TransferTable;
import com.djrapitops.plan.system.settings.Settings;
import com.djrapitops.plugin.api.TimeAmount;
import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.task.ITask;
import com.djrapitops.plugin.task.RunnableFactory;
import com.djrapitops.plugin.utilities.Verify;
import java.sql.Connection;
import java.sql.PreparedStatement;
@ -41,7 +43,6 @@ public abstract class SQLDB extends Database {
private final GeoInfoTable geoInfoTable;
private final CommandUseTable commandUseTable;
private final TPSTable tpsTable;
private final VersionTable versionTable;
private final SecurityTable securityTable;
private final WorldTable worldTable;
private final WorldTimesTable worldTimesTable;
@ -64,7 +65,6 @@ public abstract class SQLDB extends Database {
public SQLDB() {
usingMySQL = getName().equals("MySQL");
versionTable = new VersionTable(this);
serverTable = new ServerTable(this);
securityTable = new SecurityTable(this);
@ -135,67 +135,45 @@ public abstract class SQLDB extends Database {
*/
public void setupDatabase() throws DBInitException {
try {
boolean newDatabase = versionTable.isNewDatabase();
versionTable.createTable();
createTables();
if (newDatabase) {
Log.info("New Database created.");
versionTable.setVersion(19);
}
Patch[] patches = new Patch[]{
new Version10Patch(this),
new GeoInfoLastUsedPatch(this),
new TransferPartitionPatch(this),
new SessionAFKTimePatch(this),
new KillsServerIDPatch(this),
new WorldTimesSeverIDPatch(this),
new WorldsServerIDPatch(this),
new IPHashPatch(this),
new IPAnonPatch(this),
new NicknameLastSeenPatch(this),
new VersionTableRemovalPatch(this)
};
int version = versionTable.getVersion();
final SQLDB db = this;
if (version < 10) {
RunnableFactory.createNew("DB v8 -> v10 Task", new AbsRunnable() {
@Override
public void run() {
try {
new Version8TransferTable(db).alterTablesToV10();
} catch (DBInitException | DBOpException e) {
Log.toLog(this.getClass(), e);
RunnableFactory.createNew("Database Patch", new AbsRunnable() {
@Override
public void run() {
try {
boolean applied = false;
for (Patch patch : patches) {
if (!patch.hasBeenApplied()) {
String patchName = patch.getClass().getSimpleName();
Log.info("Applying Patch: " + patchName + "..");
patch.apply();
applied = true;
}
}
Log.info(applied ? "All database patches applied successfully." : "All database patches already applied.");
} catch (Exception e) {
Log.error("----------------------------------------------------");
Log.error("Database Patching failed, plugin has to be disabled. Please report this issue");
Log.error("----------------------------------------------------");
Log.toLog(this.getClass(), e);
PlanPlugin.getInstance().onDisable();
}
}).runTaskLaterAsynchronously(TimeAmount.SECOND.ticks() * 5L);
}
if (version < 11) {
serverTable.alterTableV11();
versionTable.setVersion(11);
}
if (version < 12) {
geoInfoTable.alterTableV12();
versionTable.setVersion(12);
}
if (version < 13) {
geoInfoTable.alterTableV13();
versionTable.setVersion(13);
}
if (version < 14) {
transferTable.alterTableV14();
versionTable.setVersion(14);
}
if (version < 15) {
sessionsTable.alterTableV15();
versionTable.setVersion(15);
}
if (version < 16) {
killsTable.alterTableV16();
worldTimesTable.alterTableV16();
versionTable.setVersion(16);
}
if (version < 17) {
geoInfoTable.alterTableV17();
versionTable.setVersion(17);
}
if (version < 18) {
geoInfoTable.alterTableV18();
// version set in the runnable in above method
}
if (version < 19) {
nicknamesTable.alterTableV19();
}
}
}).runTaskLaterAsynchronously(TimeAmount.SECOND.ticks() * 5L);
} catch (DBOpException e) {
throw new DBInitException("Failed to set-up Database", e);
}
@ -250,14 +228,6 @@ public abstract class SQLDB extends Database {
}
}
public int getVersion() {
return versionTable.getVersion();
}
public void setVersion(int version) {
versionTable.setVersion(version);
}
private void clean() {
tpsTable.clean();
transferTable.clean();
@ -340,6 +310,28 @@ public abstract class SQLDB extends Database {
}
}
public boolean execute(String sql) {
return execute(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) {
}
});
}
public void executeUnsafe(String... statements) {
Verify.nullCheck(statements);
for (String statement : statements) {
try {
execute(statement);
} catch (DBOpException e) {
if (Settings.DEV_MODE.isTrue()) {
Log.toLog(this.getClass(), e);
}
}
}
}
public void executeBatch(ExecStatement statement) {
Connection connection = null;
try {

View File

@ -0,0 +1,23 @@
package com.djrapitops.plan.system.database.databases.sql.patches;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import com.djrapitops.plan.system.database.databases.sql.tables.GeoInfoTable;
public class GeoInfoLastUsedPatch extends Patch {
public GeoInfoLastUsedPatch(SQLDB db) {
super(db);
}
@Override
public boolean hasBeenApplied() {
return hasColumn(GeoInfoTable.TABLE_NAME, GeoInfoTable.Col.LAST_USED.get());
}
@Override
public void apply() {
addColumns(GeoInfoTable.TABLE_NAME,
GeoInfoTable.Col.LAST_USED + " bigint NOT NULL DEFAULT 0"
);
}
}

View File

@ -0,0 +1,91 @@
package com.djrapitops.plan.system.database.databases.sql.patches;
import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.data.container.GeoInfo;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement;
import com.djrapitops.plan.system.database.databases.sql.tables.GeoInfoTable;
import com.djrapitops.plan.system.database.databases.sql.tables.move.Version18TransferTable;
import com.djrapitops.plan.system.settings.Settings;
import com.djrapitops.plugin.api.utility.log.Log;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.NoSuchAlgorithmException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class IPAnonPatch extends Patch {
public IPAnonPatch(SQLDB db) {
super(db);
}
@Override
public boolean hasBeenApplied() {
String sql = "SELECT * FROM " + GeoInfoTable.TABLE_NAME +
" WHERE " + GeoInfoTable.Col.IP + " NOT LIKE ? LIMIT 1";
return query(new QueryStatement<Boolean>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, "%x%");
}
@Override
public Boolean processResults(ResultSet set) throws SQLException {
return !set.next();
}
});
}
@Override
public void apply() {
Map<UUID, List<GeoInfo>> allGeoInfo = db.getGeoInfoTable().getAllGeoInfo();
String sql = "UPDATE " + GeoInfoTable.TABLE_NAME + " SET " +
GeoInfoTable.Col.IP + "=?, " +
GeoInfoTable.Col.IP_HASH + "=? " +
"WHERE " + GeoInfoTable.Col.IP + "=?";
db.executeBatch(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (List<GeoInfo> geoInfos : allGeoInfo.values()) {
for (GeoInfo geoInfo : geoInfos) {
try {
if (geoInfo.getIp().endsWith(".xx.xx")) {
continue;
}
GeoInfo updatedInfo = new GeoInfo(
InetAddress.getByName(geoInfo.getIp()),
geoInfo.getGeolocation(),
geoInfo.getDate()
);
statement.setString(1, updatedInfo.getIp());
statement.setString(2, updatedInfo.getIpHash());
statement.setString(3, geoInfo.getIp());
statement.addBatch();
} catch (UnknownHostException | UnsupportedEncodingException | NoSuchAlgorithmException e) {
if (Settings.DEV_MODE.isTrue()) {
Log.toLog(this.getClass(), e);
}
}
}
}
}
});
try {
new Version18TransferTable(db).alterTableV18();
} catch (DBInitException e) {
throw new DBOpException(e.getMessage(), e);
}
}
}

View File

@ -0,0 +1,21 @@
package com.djrapitops.plan.system.database.databases.sql.patches;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import com.djrapitops.plan.system.database.databases.sql.tables.GeoInfoTable;
public class IPHashPatch extends Patch {
public IPHashPatch(SQLDB db) {
super(db);
}
@Override
public boolean hasBeenApplied() {
return hasColumn(GeoInfoTable.TABLE_NAME, GeoInfoTable.Col.IP_HASH.get());
}
@Override
public void apply() {
addColumns(GeoInfoTable.Col.IP_HASH.get() + " varchar(200) DEFAULT ''");
}
}

View File

@ -0,0 +1,64 @@
package com.djrapitops.plan.system.database.databases.sql.patches;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement;
import com.djrapitops.plan.system.database.databases.sql.tables.KillsTable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
public class KillsServerIDPatch extends Patch {
public KillsServerIDPatch(SQLDB db) {
super(db);
}
@Override
public boolean hasBeenApplied() {
String tableName = KillsTable.TABLE_NAME;
String columnName = KillsTable.Col.SERVER_ID.get();
return hasColumn(tableName, columnName) && allValuesHaveServerID(tableName, columnName);
}
private Boolean allValuesHaveServerID(String tableName, String columnName) {
String sql = "SELECT * FROM " + tableName + " WHERE " + columnName + "=? LIMIT 1";
return query(new QueryStatement<Boolean>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setInt(1, 0);
}
@Override
public Boolean processResults(ResultSet set) throws SQLException {
return !set.next();
}
});
}
@Override
public void apply() {
addColumns(KillsTable.Col.SERVER_ID + " integer NOT NULL DEFAULT 0");
Map<Integer, Integer> sessionIDServerIDRelation = db.getSessionsTable().getIDServerIDRelation();
String sql = "UPDATE " + KillsTable.TABLE_NAME + " SET " +
KillsTable.Col.SERVER_ID + "=?" +
" WHERE " + KillsTable.Col.SESSION_ID + "=?";
db.executeBatch(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<Integer, Integer> entry : sessionIDServerIDRelation.entrySet()) {
Integer sessionID = entry.getKey();
Integer serverID = entry.getValue();
statement.setInt(1, serverID);
statement.setInt(2, sessionID);
statement.addBatch();
}
}
});
}
}

View File

@ -0,0 +1,99 @@
package com.djrapitops.plan.system.database.databases.sql.patches;
import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
import com.djrapitops.plan.system.database.databases.sql.processing.QueryAllStatement;
import com.djrapitops.plan.system.database.databases.sql.tables.GeoInfoTable;
import com.djrapitops.plan.system.database.databases.sql.tables.NicknamesTable;
import com.djrapitops.plan.system.database.databases.sql.tables.UserIDTable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
public class NicknameLastSeenPatch extends Patch {
public NicknameLastSeenPatch(SQLDB db) {
super(db);
}
@Override
public boolean hasBeenApplied() {
return hasColumn(GeoInfoTable.TABLE_NAME, GeoInfoTable.Col.LAST_USED.get());
}
@Override
public void apply() {
addColumns(NicknamesTable.TABLE_NAME,
NicknamesTable.Col.LAST_USED + " bigint NOT NULL DEFAULT '0'"
);
// Create table if has failed already
db.executeUnsafe("CREATE TABLE IF NOT EXISTS plan_actions " +
"(action_id integer, date bigint, server_id integer, user_id integer, additional_info varchar(1))");
Map<Integer, UUID> serverUUIDsByID = db.getServerTable().getServerUUIDsByID();
Map<UUID, Integer> serverIDsByUUID = new HashMap<>();
for (Map.Entry<Integer, UUID> entry : serverUUIDsByID.entrySet()) {
serverIDsByUUID.put(entry.getValue(), entry.getKey());
}
Map<Integer, Set<Nickname>> nicknames = getNicknamesByUserID(serverUUIDsByID);
updateLastUsed(serverIDsByUUID, nicknames);
db.executeUnsafe("DROP TABLE plan_actions");
}
private Map<Integer, Set<Nickname>> getNicknamesByUserID(Map<Integer, UUID> serverUUIDsByID) {
String fetchSQL = "SELECT * FROM plan_actions WHERE action_id=3 ORDER BY date DESC";
return query(new QueryAllStatement<Map<Integer, Set<Nickname>>>(fetchSQL, 10000) {
@Override
public Map<Integer, Set<Nickname>> processResults(ResultSet set) throws SQLException {
Map<Integer, Set<Nickname>> map = new HashMap<>();
while (set.next()) {
long date = set.getLong("date");
int userID = set.getInt(UserIDTable.Col.USER_ID.get());
int serverID = set.getInt("server_id");
UUID serverUUID = serverUUIDsByID.get(serverID);
Nickname nick = new Nickname(set.getString("additional_info"), date, serverUUID);
Set<Nickname> nicknames1 = map.getOrDefault(userID, new HashSet<>());
if (serverUUID == null || nicknames1.contains(nick)) {
continue;
}
nicknames1.add(nick);
map.put(userID, nicknames1);
}
return map;
}
});
}
private void updateLastUsed(Map<UUID, Integer> serverIDsByUUID, Map<Integer, Set<Nickname>> nicknames) {
String updateSQL = "UPDATE " + NicknamesTable.TABLE_NAME + " SET " + NicknamesTable.Col.LAST_USED + "=?" +
" WHERE " + NicknamesTable.Col.NICKNAME + "=?" +
" AND " + NicknamesTable.Col.USER_ID + "=?" +
" AND " + NicknamesTable.Col.SERVER_ID + "=?";
db.executeBatch(new ExecStatement(updateSQL) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<Integer, Set<Nickname>> entry : nicknames.entrySet()) {
Integer userId = entry.getKey();
Set<Nickname> nicks = entry.getValue();
for (Nickname nick : nicks) {
Integer serverID = serverIDsByUUID.get(nick.getServerUUID());
statement.setLong(1, nick.getDate());
statement.setString(2, nick.getName());
statement.setInt(3, userId);
statement.setInt(4, serverID);
statement.addBatch();
}
}
}
});
}
}

View File

@ -0,0 +1,93 @@
package com.djrapitops.plan.system.database.databases.sql.patches;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import com.djrapitops.plan.system.database.databases.sql.processing.QueryAllStatement;
import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public abstract class Patch {
protected final SQLDB db;
protected final boolean usingMySQL;
public Patch(SQLDB db) {
this.db = db;
usingMySQL = db.isUsingMySQL();
}
public abstract boolean hasBeenApplied();
public abstract void apply();
public <T> T query(QueryStatement<T> query) {
return db.query(query);
}
public boolean hasTable(String tableName) {
String sql = usingMySQL ?
"SHOW TABLES LIKE ?" :
"SELECT tbl_name FROM sqlite_master WHERE tbl_name=?";
return query(new QueryStatement<Boolean>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, tableName);
}
@Override
public Boolean processResults(ResultSet set) throws SQLException {
return set.next();
}
});
}
protected boolean hasColumn(String tableName, String columnName) {
return usingMySQL ?
query(new QueryStatement<Boolean>("SHOW COLUMNS FROM ? LIKE ?") {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, tableName);
statement.setString(2, columnName);
}
@Override
public Boolean processResults(ResultSet set) {
return null;
}
}) :
query(new QueryAllStatement<Boolean>("PRAGMA table_info(" + tableName + ")") {
@Override
public Boolean processResults(ResultSet set) throws SQLException {
while (set.next()) {
if (columnName.equals(set.getString("name"))) {
return true;
}
}
return false;
}
});
}
protected void addColumns(String tableName, String... columnInfo) {
for (int i = 0; i < columnInfo.length; i++) {
columnInfo[i] = "ALTER TABLE " + tableName + " ADD " + (usingMySQL ? "" : "COLUMN ") + columnInfo[i];
}
db.executeUnsafe(columnInfo);
}
protected void dropTable(String name) {
String sql = "DROP TABLE " + name;
db.executeUnsafe(sql);
}
protected void renameTable(String from, String to) {
String sql = usingMySQL ?
"RENAME TABLE " + from + " TO " + to :
"ALTER TABLE " + from + " RENAME TO " + to;
db.execute(sql);
}
}

View File

@ -0,0 +1,23 @@
package com.djrapitops.plan.system.database.databases.sql.patches;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import com.djrapitops.plan.system.database.databases.sql.tables.SessionsTable;
public class SessionAFKTimePatch extends Patch {
public SessionAFKTimePatch(SQLDB db) {
super(db);
}
@Override
public boolean hasBeenApplied() {
return hasColumn(SessionsTable.TABLE_NAME, SessionsTable.Col.AFK_TIME.get());
}
@Override
public void apply() {
addColumns(SessionsTable.TABLE_NAME,
SessionsTable.Col.AFK_TIME + " bigint NOT NULL DEFAULT 0"
);
}
}

View File

@ -0,0 +1,21 @@
package com.djrapitops.plan.system.database.databases.sql.patches;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import com.djrapitops.plan.system.database.databases.sql.tables.TransferTable;
public class TransferPartitionPatch extends Patch {
public TransferPartitionPatch(SQLDB db) {
super(db);
}
@Override
public boolean hasBeenApplied() {
return hasColumn(TransferTable.TABLE_NAME, TransferTable.Col.PART.get());
}
@Override
public void apply() {
addColumns(TransferTable.TABLE_NAME, TransferTable.Col.PART + " bigint NOT NULL DEFAULT 0");
}
}

View File

@ -1,10 +1,7 @@
/*
* License is provided in the jar as LICENSE also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/LICENSE
*/
package com.djrapitops.plan.system.database.databases.sql.tables.move;
package com.djrapitops.plan.system.database.databases.sql.patches;
import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import com.djrapitops.plan.system.database.databases.sql.tables.*;
import com.djrapitops.plan.system.info.server.ServerInfo;
@ -12,24 +9,31 @@ import com.djrapitops.plugin.api.Benchmark;
import java.util.Optional;
/**
* Class used for executing transfer queries when the database has version 8.
* <p>
* Changes the DB Schema to 10.
*
* @author Rsl1122
*/
public class Version8TransferTable extends TransferTable {
public class Version10Patch extends Patch {
private final int serverID;
private Integer serverID;
public Version8TransferTable(SQLDB db) {
public Version10Patch(SQLDB 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.");
}
@Override
public boolean hasBeenApplied() {
return !hasTable("plan_gamemodetimes");
}
@Override
public void apply() {
try {
Optional<Integer> serverID = db.getServerTable().getServerID(ServerInfo.getServerUUID());
if (!serverID.isPresent()) {
throw new IllegalStateException("Server UUID was not registered, try rebooting the plugin.");
}
this.serverID = serverID.get();
alterTablesToV10();
} catch (DBInitException e) {
throw new DBOpException(e.getMessage(), e);
}
this.serverID = serverID.get();
}
public void alterTablesToV10() throws DBInitException {
@ -52,8 +56,6 @@ public class Version8TransferTable extends TransferTable {
dropTable("temp_nicks");
dropTable("temp_kills");
dropTable("temp_users");
db.setVersion(10);
Benchmark.stop("Schema copy from 8 to 10");
}
@ -85,24 +87,24 @@ public class Version8TransferTable extends TransferTable {
") SELECT " +
"id, uuid, registered, name" +
" FROM " + tempTableName;
execute(statement);
db.execute(statement);
statement = "INSERT INTO plan_user_info " +
"(" +
"user_id, registered, opped, banned, server_id" +
") SELECT " +
"id, registered, opped, banned, '" + serverID + "'" +
" FROM " + tempTableName;
execute(statement);
db.execute(statement);
statement = "INSERT INTO plan_nicknames " +
"(" +
"user_id, nickname, server_id" +
") SELECT " +
"user_id, nickname, '" + serverID + "'" +
" FROM " + tempNickTableName;
execute(statement);
db.execute(statement);
try {
if (usingMySQL) {
execute("SET foreign_key_checks = 0");
db.execute("SET foreign_key_checks = 0");
}
statement = "INSERT INTO plan_kills " +
"(" +
@ -110,10 +112,10 @@ public class Version8TransferTable extends TransferTable {
") SELECT " +
"killer_id, victim_id, weapon, date, '0'" +
" FROM " + tempKillsTableName;
execute(statement);
db.execute(statement);
} finally {
if (usingMySQL) {
execute("SET foreign_key_checks = 1");
db.execute("SET foreign_key_checks = 1");
}
}
}
@ -132,7 +134,7 @@ public class Version8TransferTable extends TransferTable {
") SELECT " +
"command, times_used, '" + serverID + "'" +
" FROM " + tempTableName;
execute(statement);
db.execute(statement);
dropTable(tempTableName);
}
@ -151,7 +153,7 @@ public class Version8TransferTable extends TransferTable {
") SELECT " +
"date, tps, players_online, cpu_usage, ram_usage, entities, chunks_loaded, '" + serverID + "'" +
" FROM " + tempTableName;
execute(statement);
db.execute(statement);
dropTable(tempTableName);
}

View File

@ -0,0 +1,20 @@
package com.djrapitops.plan.system.database.databases.sql.patches;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
public class VersionTableRemovalPatch extends Patch {
public VersionTableRemovalPatch(SQLDB db) {
super(db);
}
@Override
public boolean hasBeenApplied() {
return hasTable("plan_version");
}
@Override
public void apply() {
dropTable("plan_version");
}
}

View File

@ -0,0 +1,63 @@
package com.djrapitops.plan.system.database.databases.sql.patches;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement;
import com.djrapitops.plan.system.database.databases.sql.tables.WorldTimesTable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
public class WorldTimesSeverIDPatch extends Patch {
public WorldTimesSeverIDPatch(SQLDB db) {
super(db);
}
@Override
public boolean hasBeenApplied() {
String tableName = WorldTimesTable.TABLE_NAME;
String columnName = WorldTimesTable.Col.SERVER_ID.get();
return hasColumn(tableName, columnName)
&& allValuesHaveServerID(tableName, columnName);
}
private Boolean allValuesHaveServerID(String tableName, String columnName) {
String sql = "SELECT * FROM " + tableName + " WHERE " + columnName + "=? LIMIT 1";
return query(new QueryStatement<Boolean>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setInt(1, 0);
}
@Override
public Boolean processResults(ResultSet set) throws SQLException {
return !set.next();
}
});
}
@Override
public void apply() {
Map<Integer, Integer> sessionIDServerIDRelation = db.getSessionsTable().getIDServerIDRelation();
String sql = "UPDATE " + WorldTimesTable.TABLE_NAME + " SET " +
WorldTimesTable.Col.SERVER_ID + "=?" +
" WHERE " + WorldTimesTable.Col.SESSION_ID + "=?";
db.executeBatch(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<Integer, Integer> entry : sessionIDServerIDRelation.entrySet()) {
Integer sessionID = entry.getKey();
Integer serverID = entry.getValue();
statement.setInt(1, serverID);
statement.setInt(2, sessionID);
statement.addBatch();
}
}
});
}
}

View File

@ -0,0 +1,152 @@
package com.djrapitops.plan.system.database.databases.sql.patches;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
import com.djrapitops.plan.system.database.databases.sql.processing.QueryAllStatement;
import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement;
import com.djrapitops.plan.system.database.databases.sql.tables.WorldTable;
import com.djrapitops.plan.system.database.databases.sql.tables.WorldTimesTable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public class WorldsServerIDPatch extends Patch {
public WorldsServerIDPatch(SQLDB db) {
super(db);
}
@Override
public boolean hasBeenApplied() {
String tableName = WorldTable.TABLE_NAME;
String columnName = WorldTable.Col.SERVER_ID.get();
return hasColumn(tableName, columnName)
&& allValuesHaveServerID(tableName, columnName);
}
private Boolean allValuesHaveServerID(String tableName, String columnName) {
String sql = "SELECT * FROM " + tableName + " WHERE " + columnName + "=? LIMIT 1";
return query(new QueryStatement<Boolean>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setInt(1, 0);
}
@Override
public Boolean processResults(ResultSet set) throws SQLException {
return !set.next();
}
});
}
@Override
public void apply() {
WorldTable worldTable = db.getWorldTable();
List<UUID> serverUUIDs = db.getServerTable().getServerUUIDs();
Map<UUID, Set<String>> worldsPerServer = new HashMap<>();
for (UUID serverUUID : serverUUIDs) {
worldsPerServer.put(serverUUID, worldTable.getWorldNamesOld(serverUUID));
}
for (Map.Entry<UUID, Set<String>> entry : worldsPerServer.entrySet()) {
UUID serverUUID = entry.getKey();
Set<String> worlds = entry.getValue();
worldTable.saveWorlds(worlds, serverUUID);
}
updateWorldTimesTableWorldIDs();
db.executeUnsafe("DELETE FROM " + WorldTable.TABLE_NAME + " WHERE " + WorldTable.Col.SERVER_ID + "=0");
}
private void updateWorldTimesTableWorldIDs() {
List<WorldObj> worldObjects = getWorldObjects();
Map<WorldObj, List<WorldObj>> oldToNewMap =
worldObjects.stream()
.filter(worldObj -> worldObj.serverId == 0)
.collect(Collectors.toMap(
Function.identity(),
oldWorld -> worldObjects.stream()
.filter(worldObj -> worldObj.serverId != 0)
.filter(worldObj -> worldObj.equals(oldWorld))
.collect(Collectors.toList()
)));
WorldTimesTable worldTimesTable = db.getWorldTimesTable();
String sql = "UPDATE " + worldTimesTable + " SET " +
WorldTimesTable.Col.WORLD_ID + "=?" +
" WHERE " + WorldTimesTable.Col.WORLD_ID + "=?" +
" AND " + WorldTimesTable.Col.SERVER_ID + "=?";
db.executeBatch(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<WorldObj, List<WorldObj>> entry : oldToNewMap.entrySet()) {
WorldObj old = entry.getKey();
for (WorldObj newWorld : entry.getValue()) {
statement.setInt(1, newWorld.id);
statement.setInt(2, old.id);
statement.setInt(3, newWorld.serverId);
statement.addBatch();
}
}
}
});
}
public List<WorldObj> getWorldObjects() {
String sql = "SELECT * FROM " + WorldTable.TABLE_NAME;
return query(new QueryAllStatement<List<WorldObj>>(sql, 100) {
@Override
public List<WorldObj> processResults(ResultSet set) throws SQLException {
List<WorldObj> objects = new ArrayList<>();
while (set.next()) {
int worldID = set.getInt(WorldTable.Col.ID.get());
int serverID = set.getInt(WorldTable.Col.SERVER_ID.get());
String worldName = set.getString(WorldTable.Col.NAME.get());
objects.add(new WorldObj(worldID, serverID, worldName));
}
return objects;
}
});
}
}
class WorldObj {
final int id;
final int serverId;
final String name;
public WorldObj(int id, int serverId, String name) {
this.id = id;
this.serverId = serverId;
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
WorldObj worldObj = (WorldObj) o;
return Objects.equals(name, worldObj.name);
}
@Override
public int hashCode() {
return Objects.hashCode(name);
}
@Override
public String toString() {
return "{" +
"id=" + id +
", serverId=" + serverId +
", name='" + name + '\'' +
'}';
}
}

View File

@ -1,7 +1,6 @@
package com.djrapitops.plan.system.database.databases.sql.tables;
import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.data.container.GeoInfo;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
@ -11,18 +10,9 @@ 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;
import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.task.RunnableFactory;
import com.djrapitops.plugin.utilities.Verify;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.NoSuchAlgorithmException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -39,8 +29,10 @@ import java.util.*;
*/
public class GeoInfoTable extends UserIDTable {
public static final String TABLE_NAME = "plan_ips";
public GeoInfoTable(SQLDB db) {
super("plan_ips", db);
super(TABLE_NAME, db);
insertStatement = "INSERT INTO " + tableName + " ("
+ Col.USER_ID + ", "
+ Col.IP + ", "
@ -67,67 +59,6 @@ public class GeoInfoTable extends UserIDTable {
);
}
public void alterTableV12() {
if (usingMySQL) {
executeUnsafe("ALTER TABLE " + tableName + " MODIFY " + Col.IP + " VARCHAR(39) NOT NULL");
}
}
public void alterTableV13() {
addColumns(Col.LAST_USED + " bigint NOT NULL DEFAULT 0");
}
public void alterTableV17() {
addColumns(Col.IP_HASH.get() + " varchar(200) DEFAULT ''");
}
public void alterTableV18() {
RunnableFactory.createNew("DB Version 17->18", new AbsRunnable() {
@Override
public void run() {
try {
Map<UUID, List<GeoInfo>> allGeoInfo = getAllGeoInfo();
String sql = "UPDATE " + tableName + " SET " +
Col.IP + "=?, " +
Col.IP_HASH + "=? " +
"WHERE " + Col.IP + "=?";
executeBatch(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (List<GeoInfo> geoInfos : allGeoInfo.values()) {
for (GeoInfo geoInfo : geoInfos) {
try {
if (geoInfo.getIp().endsWith(".xx.xx")) {
continue;
}
GeoInfo updatedInfo = new GeoInfo(
InetAddress.getByName(geoInfo.getIp()),
geoInfo.getGeolocation(),
geoInfo.getDate()
);
statement.setString(1, updatedInfo.getIp());
statement.setString(2, updatedInfo.getIpHash());
statement.setString(3, geoInfo.getIp());
statement.addBatch();
} catch (UnknownHostException | UnsupportedEncodingException | NoSuchAlgorithmException e) {
if (Settings.DEV_MODE.isTrue()) {
Log.toLog(this.getClass(), e);
}
}
}
}
}
});
new Version18TransferTable(db).alterTableV18();
db.setVersion(18);
} catch (DBOpException | DBInitException e) {
Log.toLog(this.getClass(), e);
}
}
}).runTaskAsynchronously();
}
public void clean() {
}

View File

@ -31,10 +31,11 @@ import java.util.*;
*/
public class KillsTable extends UserIDTable {
public static final String TABLE_NAME = "plan_kills";
private final ServerTable serverTable;
public KillsTable(SQLDB db) {
super("plan_kills", db);
super(TABLE_NAME, db);
sessionsTable = db.getSessionsTable();
serverTable = db.getServerTable();
insertStatement = "INSERT INTO " + tableName + " ("
@ -304,29 +305,6 @@ public class KillsTable extends UserIDTable {
});
}
public void alterTableV16() {
addColumns(Col.SERVER_ID + " integer NOT NULL DEFAULT 0");
Map<Integer, Integer> sessionIDServerIDRelation = sessionsTable.getIDServerIDRelation();
String sql = "UPDATE " + tableName + " SET " +
Col.SERVER_ID + "=?" +
" WHERE " + Col.SESSION_ID + "=?";
executeBatch(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<Integer, Integer> entry : sessionIDServerIDRelation.entrySet()) {
Integer sessionID = entry.getKey();
Integer serverID = entry.getValue();
statement.setInt(1, serverID);
statement.setInt(2, sessionID);
statement.addBatch();
}
}
});
}
public enum Col implements Column {
KILLER_ID("killer_id"),
VICTIM_ID("victim_id"),

View File

@ -10,8 +10,6 @@ import com.djrapitops.plan.system.database.databases.sql.statements.Column;
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.info.server.ServerInfo;
import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.task.RunnableFactory;
import com.djrapitops.plugin.utilities.Verify;
import java.sql.PreparedStatement;
@ -30,12 +28,13 @@ import java.util.*;
*/
public class NicknamesTable extends UserIDTable {
public static final String TABLE_NAME = "plan_nicknames";
private final ServerTable serverTable;
private String insertStatement;
private final String updateStatement;
public NicknamesTable(SQLDB db) {
super("plan_nicknames", db);
super(TABLE_NAME, db);
serverTable = db.getServerTable();
insertStatement = "INSERT INTO " + tableName + " (" +
Col.USER_ID + ", " +
@ -65,75 +64,6 @@ public class NicknamesTable extends UserIDTable {
);
}
public void alterTableV19() {
addColumns(Col.LAST_USED + " bigint NOT NULL DEFAULT '0'");
RunnableFactory.createNew(new AbsRunnable("DB version 18->19") {
@Override
public void run() {
// Create actions table if version 18 transfer is run concurrently.
execute("CREATE TABLE IF NOT EXISTS plan_actions " +
"(action_id integer, date bigint, server_id integer, user_id integer, additional_info varchar(1))");
Map<Integer, UUID> serverUUIDsByID = serverTable.getServerUUIDsByID();
Map<UUID, Integer> serverIDsByUUID = new HashMap<>();
for (Map.Entry<Integer, UUID> entry : serverUUIDsByID.entrySet()) {
serverIDsByUUID.put(entry.getValue(), entry.getKey());
}
String fetchSQL = "SELECT * FROM plan_actions WHERE action_id=3 ORDER BY date DESC";
Map<Integer, Set<Nickname>> nicknames = query(new QueryAllStatement<Map<Integer, Set<Nickname>>>(fetchSQL, 10000) {
@Override
public Map<Integer, Set<Nickname>> processResults(ResultSet set) throws SQLException {
Map<Integer, Set<Nickname>> map = new HashMap<>();
while (set.next()) {
long date = set.getLong("date");
int userID = set.getInt(UserIDTable.Col.USER_ID.get());
int serverID = set.getInt("server_id");
UUID serverUUID = serverUUIDsByID.get(serverID);
Nickname nick = new Nickname(set.getString("additional_info"), date, serverUUID);
Set<Nickname> nicknames = map.getOrDefault(userID, new HashSet<>());
if (serverUUID == null || nicknames.contains(nick)) {
continue;
}
nicknames.add(nick);
map.put(userID, nicknames);
}
return map;
}
});
String updateSQL = "UPDATE " + tableName + " SET " + Col.LAST_USED + "=?" +
" WHERE " + Col.NICKNAME + "=?" +
" AND " + Col.USER_ID + "=?" +
" AND " + Col.SERVER_ID + "=?";
executeBatch(new ExecStatement(updateSQL) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<Integer, Set<Nickname>> entry : nicknames.entrySet()) {
Integer userId = entry.getKey();
Set<Nickname> nicks = entry.getValue();
for (Nickname nick : nicks) {
Integer serverID = serverIDsByUUID.get(nick.getServerUUID());
statement.setLong(1, nick.getDate());
statement.setString(2, nick.getName());
statement.setInt(3, userId);
statement.setInt(4, serverID);
statement.addBatch();
}
}
}
});
db.setVersion(19);
executeUnsafe("DROP TABLE plan_actions");
}
}).runTaskAsynchronously();
}
/**
* Get ALL nicknames of the user by Server UUID.
* <p>

View File

@ -62,12 +62,6 @@ public class ServerTable extends Table {
);
}
public void alterTableV11() {
if (usingMySQL) {
executeUnsafe("ALTER TABLE " + tableName + " MODIFY " + Col.MAX_PLAYERS + " INTEGER NOT NULL DEFAULT -1");
}
}
private void updateServerInfo(Server info) {
String sql = Update.values(tableName,
Col.SERVER_UUID,

View File

@ -729,10 +729,6 @@ public class SessionsTable extends UserIDTable {
return sessionsByStart;
}
public void alterTableV15() {
addColumns(Col.AFK_TIME + " bigint NOT NULL DEFAULT 0");
}
public Map<Integer, Integer> getIDServerIDRelation() {
String sql = "SELECT " +
Col.ID + ", " +

View File

@ -5,14 +5,10 @@ import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement;
import com.djrapitops.plan.system.settings.Settings;
import com.djrapitops.plan.utilities.MiscUtils;
import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.utilities.Verify;
import com.google.common.base.Objects;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
@ -67,17 +63,6 @@ public abstract class Table {
return db.getConnection();
}
/**
* Get the Database Schema version from VersionTable.
*
* @return Database Schema version.
* @deprecated Use db.getVersion - db is protected variable.
*/
@Deprecated
public int getVersion() {
return db.getVersion();
}
/**
* Executes an SQL Statement
*
@ -85,12 +70,7 @@ public abstract class Table {
* @return true if rows were updated.
*/
protected boolean execute(String statementString) {
return execute(new ExecStatement(statementString) {
@Override
public void prepare(PreparedStatement statement) {
/* No preparations necessary */
}
});
return db.execute(statementString);
}
/**
@ -99,16 +79,7 @@ public abstract class Table {
* @param statements SQL statements to setUp
*/
protected void executeUnsafe(String... statements) {
Verify.nullCheck(statements);
for (String statement : statements) {
try {
execute(statement);
} catch (DBOpException e) {
if (Settings.DEV_MODE.isTrue()) {
Log.toLog(this.getClass(), e);
}
}
}
db.executeUnsafe(statements);
}
/**

View File

@ -30,13 +30,14 @@ import java.util.Optional;
*/
public class TransferTable extends Table {
public static final String TABLE_NAME = "plan_transfer";
private final String insertStatementNoParts;
private final ServerTable serverTable;
private final String selectStatement;
public TransferTable(SQLDB db) {
super("plan_transfer", db);
super(TABLE_NAME, db);
serverTable = db.getServerTable();
insertStatementNoParts = "REPLACE INTO " + tableName + " (" +
@ -70,10 +71,6 @@ public class TransferTable extends Table {
);
}
public void alterTableV14() {
addColumns(Col.PART + " bigint NOT NULL DEFAULT 0");
}
public void clean() {
String sql = "DELETE FROM " + tableName +
" WHERE " + Col.EXPIRY + " < ?" +

View File

@ -1,86 +0,0 @@
package com.djrapitops.plan.system.database.databases.sql.tables;
import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import com.djrapitops.plan.system.database.databases.sql.processing.ExecStatement;
import com.djrapitops.plan.system.database.databases.sql.processing.QueryAllStatement;
import com.djrapitops.plan.system.database.databases.sql.processing.QueryStatement;
import com.djrapitops.plan.system.database.databases.sql.statements.Sql;
import com.djrapitops.plan.system.database.databases.sql.statements.TableSqlParser;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @author Rsl1122
*/
public class VersionTable extends Table {
public VersionTable(SQLDB db) {
super("plan_version", db);
}
@Override
public void createTable() throws DBInitException {
createTable(TableSqlParser.createTable(tableName)
.column("version", Sql.INT).notNull()
.toString()
);
}
public boolean isNewDatabase() {
String sql = usingMySQL ?
"SHOW TABLES LIKE ?" :
"SELECT tbl_name FROM sqlite_master WHERE tbl_name=?";
return query(new QueryStatement<Boolean>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, tableName);
}
@Override
public Boolean processResults(ResultSet set) throws SQLException {
return !set.next();
}
});
}
/**
* @return @throws SQLException
*/
@Override
public int getVersion() {
String sql = "SELECT * FROM " + tableName;
return query(new QueryAllStatement<Integer>(sql) {
@Override
public Integer processResults(ResultSet set) throws SQLException {
int version = 0;
if (set.next()) {
version = set.getInt("version");
}
return version;
}
});
}
/**
* Set the DB Schema version
*
* @param version DB Schema version
*/
public void setVersion(int version) {
removeAllData();
String sql = "INSERT INTO " + tableName + " (version) VALUES (?)";
execute(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setInt(1, version);
}
});
}
}

View File

@ -10,14 +10,11 @@ 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.info.server.ServerInfo;
import com.djrapitops.plugin.utilities.Verify;
import com.google.common.base.Objects;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Table class representing database table plan_worlds.
@ -244,61 +241,6 @@ public class WorldTable extends Table {
});
}
public void alterTableV16() {
addColumns(Col.SERVER_ID + " integer NOT NULL DEFAULT 0");
List<UUID> serverUUIDs = serverTable.getServerUUIDs();
Map<UUID, Set<String>> worldsPerServer = new HashMap<>();
for (UUID serverUUID : serverUUIDs) {
worldsPerServer.put(serverUUID, getWorldNamesOld(serverUUID));
}
for (Map.Entry<UUID, Set<String>> entry : worldsPerServer.entrySet()) {
UUID serverUUID = entry.getKey();
Set<String> worlds = entry.getValue();
saveWorlds(worlds, serverUUID);
}
updateWorldTimesTableWorldIDs();
execute("DELETE FROM " + tableName + " WHERE " + Col.SERVER_ID + "=0");
}
private void updateWorldTimesTableWorldIDs() {
List<WorldObj> worldObjects = getWorldObjects();
Map<WorldObj, List<WorldObj>> oldToNewMap =
worldObjects.stream()
.filter(worldObj -> worldObj.serverId == 0)
.collect(Collectors.toMap(
Function.identity(),
oldWorld -> worldObjects.stream()
.filter(worldObj -> worldObj.serverId != 0)
.filter(worldObj -> worldObj.equals(oldWorld))
.collect(Collectors.toList()
)));
WorldTimesTable worldTimesTable = db.getWorldTimesTable();
String sql = "UPDATE " + worldTimesTable + " SET " +
WorldTimesTable.Col.WORLD_ID + "=?" +
" WHERE " + WorldTimesTable.Col.WORLD_ID + "=?" +
" AND " + WorldTimesTable.Col.SERVER_ID + "=?";
executeBatch(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<WorldObj, List<WorldObj>> entry : oldToNewMap.entrySet()) {
WorldObj old = entry.getKey();
for (WorldObj newWorld : entry.getValue()) {
statement.setInt(1, newWorld.id);
statement.setInt(2, old.id);
statement.setInt(3, newWorld.serverId);
statement.addBatch();
}
}
}
});
}
public Map<Integer, List<Integer>> getWorldIDsByServerIDs() {
String sql = "SELECT " +
Col.ID + ", " +
@ -321,23 +263,6 @@ public class WorldTable extends Table {
});
}
public List<WorldObj> getWorldObjects() {
String sql = "SELECT * FROM " + tableName;
return query(new QueryAllStatement<List<WorldObj>>(sql, 100) {
@Override
public List<WorldObj> processResults(ResultSet set) throws SQLException {
List<WorldObj> objects = new ArrayList<>();
while (set.next()) {
int worldID = set.getInt(Col.ID.get());
int serverID = set.getInt(Col.SERVER_ID.get());
String worldName = set.getString(Col.NAME.get());
objects.add(new WorldObj(worldID, serverID, worldName));
}
return objects;
}
});
}
public enum Col implements Column {
ID("id"),
SERVER_ID("server_id"),
@ -361,36 +286,3 @@ public class WorldTable extends Table {
}
}
class WorldObj {
final int id;
final int serverId;
final String name;
public WorldObj(int id, int serverId, String name) {
this.id = id;
this.serverId = serverId;
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
WorldObj worldObj = (WorldObj) o;
return Objects.equal(name, worldObj.name);
}
@Override
public int hashCode() {
return Objects.hashCode(name);
}
@Override
public String toString() {
return "{" +
"id=" + id +
", serverId=" + serverId +
", name='" + name + '\'' +
'}';
}
}

View File

@ -1,7 +1,6 @@
package com.djrapitops.plan.system.database.databases.sql.tables;
import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.data.container.Session;
import com.djrapitops.plan.data.store.keys.SessionKeys;
import com.djrapitops.plan.data.time.GMTimes;
@ -14,10 +13,6 @@ import com.djrapitops.plan.system.database.databases.sql.statements.Column;
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.info.server.ServerInfo;
import com.djrapitops.plugin.api.TimeAmount;
import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.task.RunnableFactory;
import com.djrapitops.plugin.utilities.Verify;
import java.sql.PreparedStatement;
@ -356,42 +351,6 @@ public class WorldTimesTable extends UserIDTable {
});
}
public void alterTableV16() {
addColumns(Col.SERVER_ID + " integer NOT NULL DEFAULT 0");
RunnableFactory.createNew("DB version -> 16", new AbsRunnable() {
@Override
public void run() {
try {
Map<Integer, Integer> sessionIDServerIDRelation = sessionsTable.getIDServerIDRelation();
String sql = "UPDATE " + tableName + " SET " +
Col.SERVER_ID + "=?" +
" WHERE " + Col.SESSION_ID + "=?";
executeBatch(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<Integer, Integer> entry : sessionIDServerIDRelation.entrySet()) {
Integer sessionID = entry.getKey();
Integer serverID = entry.getValue();
statement.setInt(1, serverID);
statement.setInt(2, sessionID);
statement.addBatch();
}
}
});
worldTable.alterTableV16();
} catch (DBOpException e) {
Log.toLog(this.getClass().getName(), e);
} finally {
cancel();
}
}
}).runTaskLaterAsynchronously(TimeAmount.SECOND.ticks() * 2);
}
public enum Col implements Column {
USER_ID(UserIDTable.Col.USER_ID.get()),
SERVER_ID("server_id"),

View File

@ -5,12 +5,10 @@
package com.djrapitops.plan.system.webserver.response.pages;
import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.data.store.mutators.formatting.Formatter;
import com.djrapitops.plan.data.store.mutators.formatting.Formatters;
import com.djrapitops.plan.data.store.objects.DateHolder;
import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import com.djrapitops.plan.system.info.connection.ConnectionLog;
import com.djrapitops.plan.system.info.connection.ConnectionSystem;
import com.djrapitops.plan.system.info.server.Server;
@ -123,15 +121,6 @@ public class DebugPageResponse extends ErrorResponse {
Database database = Database.getActive();
content.append("**Database:** ").append(database.getName());
if (database instanceof SQLDB) {
try {
content.append(" schema v").append(((SQLDB) database).getVersion());
} catch (DBOpException e) {
Log.toLog(this.getClass(), e);
}
}
content.append("<br><br>");
RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();

View File

@ -929,94 +929,6 @@ public class SQLiteTest {
assertTrue(geolocations.contains(secondInfo.getGeolocation()));
}
@Test
public void testWorldTableAlterV16() {
saveUserOne();
new Table("test", db) {
@Override
public void createTable() {
execute(
"INSERT INTO " + WorldTable.TABLE_NAME + " (" +
WorldTable.Col.NAME + ", " +
WorldTable.Col.SERVER_ID +
") VALUES ('Test', '0')"
);
execute(
"INSERT INTO " + SessionsTable.TABLE_NAME + " (" +
SessionsTable.Col.SESSION_START + ", " +
SessionsTable.Col.SESSION_END + ", " +
SessionsTable.Col.AFK_TIME + ", " +
SessionsTable.Col.DEATHS + ", " +
SessionsTable.Col.MOB_KILLS + ", " +
SessionsTable.Col.SERVER_ID + ", " +
SessionsTable.Col.USER_ID +
") VALUES ('0', '0', '0', '0', '0', '1', '1')"
);
execute(
"INSERT INTO " + WorldTimesTable.TABLE_NAME + " (" +
WorldTimesTable.Col.SERVER_ID + ", " +
WorldTimesTable.Col.SESSION_ID + ", " +
WorldTimesTable.Col.USER_ID + ", " +
WorldTimesTable.Col.WORLD_ID + ", " +
WorldTimesTable.Col.SURVIVAL + ", " +
WorldTimesTable.Col.CREATIVE + ", " +
WorldTimesTable.Col.SPECTATOR + ", " +
WorldTimesTable.Col.ADVENTURE +
") VALUES ('1', '1', '1', '1', '0','0','0','0')"
);
execute(
"INSERT INTO " + ServerTable.TABLE_NAME + " (" +
ServerTable.Col.SERVER_UUID + ", " +
ServerTable.Col.SERVER_ID + ", " +
ServerTable.Col.MAX_PLAYERS + ", " +
ServerTable.Col.WEBSERVER_ADDRESS + ", " +
ServerTable.Col.INSTALLED + ", " +
ServerTable.Col.NAME +
") VALUES ('" + UUID.randomUUID() + "', '2', '0', '0', '1', '2')"
);
execute(
"INSERT INTO " + SessionsTable.TABLE_NAME + " (" +
SessionsTable.Col.SESSION_START + ", " +
SessionsTable.Col.SESSION_END + ", " +
SessionsTable.Col.AFK_TIME + ", " +
SessionsTable.Col.DEATHS + ", " +
SessionsTable.Col.MOB_KILLS + ", " +
SessionsTable.Col.SERVER_ID + ", " +
SessionsTable.Col.USER_ID +
") VALUES ('0', '0', '0', '0', '0', '2', '1')"
);
execute(
"INSERT INTO " + WorldTimesTable.TABLE_NAME + " (" +
WorldTimesTable.Col.SERVER_ID + ", " +
WorldTimesTable.Col.SESSION_ID + ", " +
WorldTimesTable.Col.USER_ID + ", " +
WorldTimesTable.Col.WORLD_ID + ", " +
WorldTimesTable.Col.SURVIVAL + ", " +
WorldTimesTable.Col.CREATIVE + ", " +
WorldTimesTable.Col.SPECTATOR + ", " +
WorldTimesTable.Col.ADVENTURE +
") VALUES ('2', '2', '1', '1', '0','0','0','0')"
);
}
}.createTable();
WorldTable worldTable = db.getWorldTable();
Map<Integer, List<Integer>> before = worldTable.getWorldIDsByServerIDs();
System.out.println("\nBefore: " + before);
System.out.println("Before: " + worldTable.getWorldObjects() + "\n");
worldTable.alterTableV16();
Map<Integer, List<Integer>> after = worldTable.getWorldIDsByServerIDs();
System.out.println("\nAfter: " + after);
System.out.println("After: " + worldTable.getWorldObjects() + "\n");
assertNull(after.get(0));
assertNotNull(after.get(1));
assertEquals(1, after.get(1).size());
assertNotNull(after.get(2));
assertEquals(1, after.get(2).size());
}
@Test
public void testNewContainerForPlayer() throws UnsupportedEncodingException, NoSuchAlgorithmException {
saveAllData(db);