Modify Patches to Transactions

This commit is contained in:
Rsl1122 2019-01-19 21:52:23 +02:00
parent 60835a339e
commit 14da0ca8b1
19 changed files with 60 additions and 49 deletions

View File

@ -523,6 +523,14 @@ public abstract class SQLDB extends AbstractDatabase {
return config; return config;
} }
public PluginLogger getLogger() {
return logger;
}
public Locale getLocale() {
return locale;
}
public NetworkContainer.Factory getNetworkContainerFactory() { public NetworkContainer.Factory getNetworkContainerFactory() {
return networkContainerFactory; return networkContainerFactory;
} }

View File

@ -135,4 +135,9 @@ public abstract class Transaction {
throw DBOpException.forCause(statement.getSql(), e); throw DBOpException.forCause(statement.getSql(), e);
} }
} }
@Deprecated
protected void setDb(SQLDB db) {
this.db = db;
}
} }

View File

@ -66,6 +66,6 @@ public class BadAFKThresholdValuePatch extends Patch {
SessionsTable.Col.AFK_TIME + SessionsTable.Col.AFK_TIME +
" - (" + SessionsTable.Col.SESSION_END + "-" + SessionsTable.Col.SESSION_START + " - (" + SessionsTable.Col.SESSION_END + "-" + SessionsTable.Col.SESSION_START +
")) < 5 AND " + SessionsTable.Col.AFK_TIME + "!=0"; ")) < 5 AND " + SessionsTable.Col.AFK_TIME + "!=0";
db.execute(sql); execute(sql);
} }
} }

View File

@ -46,7 +46,7 @@ public class GeoInfoOptimizationPatch extends Patch {
tempOldTable(); tempOldTable();
db.getGeoInfoTable().createTable(); db.getGeoInfoTable().createTable();
db.execute("INSERT INTO " + tableName + " (" + execute("INSERT INTO " + tableName + " (" +
Col.UUID + ", " + Col.UUID + ", " +
Col.IP + ", " + Col.IP + ", " +
Col.IP_HASH + ", " + Col.IP_HASH + ", " +

View File

@ -80,7 +80,7 @@ public class IPAnonPatch extends Patch {
GeoInfoTable.Col.IP_HASH + "=? " + GeoInfoTable.Col.IP_HASH + "=? " +
"WHERE " + GeoInfoTable.Col.IP + "=?"; "WHERE " + GeoInfoTable.Col.IP + "=?";
db.executeBatch(new ExecStatement(sql) { executeBatch(new ExecStatement(sql) {
@Override @Override
public void prepare(PreparedStatement statement) throws SQLException { public void prepare(PreparedStatement statement) throws SQLException {
for (List<GeoInfo> geoInfos : allGeoInfo.values()) { for (List<GeoInfo> geoInfos : allGeoInfo.values()) {
@ -122,7 +122,7 @@ public class IPAnonPatch extends Patch {
boolean hasUserIdColumn = hasColumn(tempTableName, "user_id"); boolean hasUserIdColumn = hasColumn(tempTableName, "user_id");
String identifiers = hasUserIdColumn ? "user_id" : "id, uuid"; String identifiers = hasUserIdColumn ? "user_id" : "id, uuid";
db.execute("INSERT INTO plan_ips (" + execute("INSERT INTO plan_ips (" +
identifiers + ", ip, ip_hash, geolocation, last_used" + identifiers + ", ip, ip_hash, geolocation, last_used" +
") SELECT " + ") SELECT " +
identifiers + ", ip, ip_hash, geolocation, MAX(last_used) FROM plan_ips_temp GROUP BY ip_hash, " + identifiers + ", ip, ip_hash, geolocation, MAX(last_used) FROM plan_ips_temp GROUP BY ip_hash, " +

View File

@ -49,7 +49,7 @@ public class KillsOptimizationPatch extends Patch {
tempOldTable(); tempOldTable();
db.getKillsTable().createTable(); db.getKillsTable().createTable();
db.execute("INSERT INTO " + tableName + " (" + execute("INSERT INTO " + tableName + " (" +
Col.VICTIM_UUID + ", " + Col.VICTIM_UUID + ", " +
Col.KILLER_UUID + ", " + Col.KILLER_UUID + ", " +
Col.SERVER_UUID + ", " + Col.SERVER_UUID + ", " +

View File

@ -69,7 +69,7 @@ public class KillsServerIDPatch extends Patch {
String sql = "UPDATE " + KillsTable.TABLE_NAME + " SET server_id=? WHERE " + KillsTable.Col.SESSION_ID + "=?"; String sql = "UPDATE " + KillsTable.TABLE_NAME + " SET server_id=? WHERE " + KillsTable.Col.SESSION_ID + "=?";
db.executeBatch(new ExecStatement(sql) { executeBatch(new ExecStatement(sql) {
@Override @Override
public void prepare(PreparedStatement statement) throws SQLException { public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<Integer, Integer> entry : sessionIDServerIDRelation.entrySet()) { for (Map.Entry<Integer, Integer> entry : sessionIDServerIDRelation.entrySet()) {

View File

@ -52,7 +52,7 @@ public class NicknameLastSeenPatch extends Patch {
} }
// Create table if has failed already // Create table if has failed already
db.executeUnsafe("CREATE TABLE IF NOT EXISTS plan_actions " + executeSwallowingExceptions("CREATE TABLE IF NOT EXISTS plan_actions " +
"(action_id integer, date bigint, server_id integer, user_id integer, additional_info varchar(1))"); "(action_id integer, date bigint, server_id integer, user_id integer, additional_info varchar(1))");
Map<Integer, UUID> serverUUIDsByID = getServerUUIDsByID(); Map<Integer, UUID> serverUUIDsByID = getServerUUIDsByID();
@ -64,7 +64,7 @@ public class NicknameLastSeenPatch extends Patch {
Map<Integer, Set<Nickname>> nicknames = getNicknamesByUserID(serverUUIDsByID); Map<Integer, Set<Nickname>> nicknames = getNicknamesByUserID(serverUUIDsByID);
updateLastUsed(serverIDsByUUID, nicknames); updateLastUsed(serverIDsByUUID, nicknames);
db.executeUnsafe("DROP TABLE plan_actions"); executeSwallowingExceptions("DROP TABLE plan_actions");
} }
private Map<Integer, UUID> getServerUUIDsByID() { private Map<Integer, UUID> getServerUUIDsByID() {
@ -117,7 +117,7 @@ public class NicknameLastSeenPatch extends Patch {
" AND user_id=?" + " AND user_id=?" +
" AND server_id=?"; " AND server_id=?";
db.executeBatch(new ExecStatement(updateSQL) { executeBatch(new ExecStatement(updateSQL) {
@Override @Override
public void prepare(PreparedStatement statement) throws SQLException { public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<Integer, Set<Nickname>> entry : nicknames.entrySet()) { for (Map.Entry<Integer, Set<Nickname>> entry : nicknames.entrySet()) {

View File

@ -47,7 +47,7 @@ public class NicknamesOptimizationPatch extends Patch {
tempOldTable(); tempOldTable();
db.getNicknamesTable().createTable(); db.getNicknamesTable().createTable();
db.execute("INSERT INTO " + tableName + " (" + execute("INSERT INTO " + tableName + " (" +
Col.UUID + ", " + Col.UUID + ", " +
Col.SERVER_UUID + ", " + Col.SERVER_UUID + ", " +
Col.NICKNAME + ", " + Col.NICKNAME + ", " +

View File

@ -21,6 +21,7 @@ import com.djrapitops.plan.db.DBType;
import com.djrapitops.plan.db.SQLDB; import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.QueryAllStatement; import com.djrapitops.plan.db.access.QueryAllStatement;
import com.djrapitops.plan.db.access.QueryStatement; import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.access.transactions.Transaction;
import com.djrapitops.plan.db.sql.parsing.TableSqlParser; import com.djrapitops.plan.db.sql.parsing.TableSqlParser;
import com.djrapitops.plan.db.sql.queries.MySQLSchemaQueries; import com.djrapitops.plan.db.sql.queries.MySQLSchemaQueries;
import com.djrapitops.plan.system.settings.paths.DatabaseSettings; import com.djrapitops.plan.system.settings.paths.DatabaseSettings;
@ -32,12 +33,13 @@ import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
public abstract class Patch { public abstract class Patch extends Transaction {
protected final SQLDB db; protected final SQLDB db;
protected final DBType dbType; protected final DBType dbType;
public Patch(SQLDB db) { public Patch(SQLDB db) {
setDb(db);
this.db = db; this.db = db;
this.dbType = db.getType(); this.dbType = db.getType();
} }
@ -46,22 +48,26 @@ public abstract class Patch {
protected abstract void applyPatch(); protected abstract void applyPatch();
public void apply() { @Override
protected void execute() {
// if (!hasBeenApplied()) { TODO Uncomment after moving patches to the execution service
if (dbType == DBType.MYSQL) disableForeignKeyChecks(); if (dbType == DBType.MYSQL) disableForeignKeyChecks();
applyPatch(); applyPatch();
if (dbType == DBType.MYSQL) enableForeignKeyChecks(); if (dbType == DBType.MYSQL) enableForeignKeyChecks();
// }
}
@Deprecated
public void apply() {
db.executeTransaction(this);
} }
private void enableForeignKeyChecks() { private void enableForeignKeyChecks() {
db.execute("SET FOREIGN_KEY_CHECKS=1"); execute("SET FOREIGN_KEY_CHECKS=1");
} }
private void disableForeignKeyChecks() { private void disableForeignKeyChecks() {
db.execute("SET FOREIGN_KEY_CHECKS=0"); execute("SET FOREIGN_KEY_CHECKS=0");
}
public <T> T query(QueryStatement<T> query) {
return db.query(query);
} }
protected boolean hasTable(String tableName) { protected boolean hasTable(String tableName) {
@ -138,15 +144,15 @@ public abstract class Patch {
} }
protected void addColumn(String tableName, String columnInfo) { protected void addColumn(String tableName, String columnInfo) {
db.execute("ALTER TABLE " + tableName + " ADD " + (dbType.supportsMySQLQueries() ? "" : "COLUMN ") + columnInfo); execute("ALTER TABLE " + tableName + " ADD " + (dbType.supportsMySQLQueries() ? "" : "COLUMN ") + columnInfo);
} }
protected void dropTable(String name) { protected void dropTable(String name) {
db.execute(TableSqlParser.dropTable(name)); execute(TableSqlParser.dropTable(name));
} }
protected void renameTable(String from, String to) { protected void renameTable(String from, String to) {
db.execute(getRenameTableSQL(from, to)); execute(getRenameTableSQL(from, to));
} }
private String getRenameTableSQL(String from, String to) { private String getRenameTableSQL(String from, String to) {
@ -172,7 +178,7 @@ public abstract class Patch {
for (MySQLSchemaQueries.ForeignKeyConstraint constraint : constraints) { for (MySQLSchemaQueries.ForeignKeyConstraint constraint : constraints) {
// Uses information from https://stackoverflow.com/a/34574758 // Uses information from https://stackoverflow.com/a/34574758
db.execute("ALTER TABLE " + constraint.getTable() + execute("ALTER TABLE " + constraint.getTable() +
" DROP FOREIGN KEY " + constraint.getConstraintName()); " DROP FOREIGN KEY " + constraint.getConstraintName());
} }
} }

View File

@ -47,7 +47,7 @@ public class PingOptimizationPatch extends Patch {
tempOldTable(); tempOldTable();
db.getPingTable().createTable(); db.getPingTable().createTable();
db.execute("INSERT INTO " + tableName + " (" + execute("INSERT INTO " + tableName + " (" +
Col.UUID + ", " + Col.UUID + ", " +
Col.SERVER_UUID + ", " + Col.SERVER_UUID + ", " +
Col.ID + ", " + Col.ID + ", " +

View File

@ -51,7 +51,7 @@ public class SessionsOptimizationPatch extends Patch {
db.getSessionsTable().createTable(); db.getSessionsTable().createTable();
db.execute("INSERT INTO " + tableName + " (" + execute("INSERT INTO " + tableName + " (" +
Col.UUID + ", " + Col.UUID + ", " +
Col.SERVER_UUID + ", " + Col.SERVER_UUID + ", " +
Col.ID + ", " + Col.ID + ", " +

View File

@ -47,7 +47,7 @@ public class UserInfoOptimizationPatch extends Patch {
tempOldTable(); tempOldTable();
db.getUserInfoTable().createTable(); db.getUserInfoTable().createTable();
db.execute("INSERT INTO " + tableName + " (" + execute("INSERT INTO " + tableName + " (" +
Col.UUID + ", " + Col.UUID + ", " +
Col.SERVER_UUID + ", " + Col.SERVER_UUID + ", " +
Col.REGISTERED + ", " + Col.REGISTERED + ", " +

View File

@ -97,31 +97,22 @@ public class Version10Patch extends Patch {
"(id, uuid, registered, name)" + "(id, uuid, registered, name)" +
" SELECT id, uuid, registered, name" + " SELECT id, uuid, registered, name" +
" FROM " + tempTableName; " FROM " + tempTableName;
db.execute(statement); execute(statement);
statement = "INSERT INTO plan_user_info " + statement = "INSERT INTO plan_user_info " +
"(user_id, registered, opped, banned, server_id)" + "(user_id, registered, opped, banned, server_id)" +
" SELECT id, registered, opped, banned, '" + serverID + "'" + " SELECT id, registered, opped, banned, '" + serverID + "'" +
" FROM " + tempTableName; " FROM " + tempTableName;
db.execute(statement); execute(statement);
statement = "INSERT INTO plan_nicknames " + statement = "INSERT INTO plan_nicknames " +
"(user_id, nickname, server_id)" + "(user_id, nickname, server_id)" +
" SELECT user_id, nickname, '" + serverID + "'" + " SELECT user_id, nickname, '" + serverID + "'" +
" FROM " + tempNickTableName; " FROM " + tempNickTableName;
db.execute(statement); execute(statement);
try { statement = "INSERT INTO plan_kills " +
if (dbType.supportsMySQLQueries()) { "(killer_id, victim_id, weapon, date, session_id)" +
db.execute("SET foreign_key_checks = 0"); " SELECT killer_id, victim_id, weapon, date, '0'" +
} " FROM " + tempKillsTableName;
statement = "INSERT INTO plan_kills " + execute(statement);
"(killer_id, victim_id, weapon, date, session_id)" +
" SELECT killer_id, victim_id, weapon, date, '0'" +
" FROM " + tempKillsTableName;
db.execute(statement);
} finally {
if (dbType.supportsMySQLQueries()) {
db.execute("SET foreign_key_checks = 1");
}
}
} }
private void copyCommandUsage() throws DBInitException { private void copyCommandUsage() throws DBInitException {
@ -136,7 +127,7 @@ public class Version10Patch extends Patch {
"(command, times_used, server_id)" + "(command, times_used, server_id)" +
" SELECT command, times_used, '" + serverID + "'" + " SELECT command, times_used, '" + serverID + "'" +
" FROM " + tempTableName; " FROM " + tempTableName;
db.execute(statement); execute(statement);
dropTable(tempTableName); dropTable(tempTableName);
} }
@ -153,7 +144,7 @@ public class Version10Patch extends Patch {
"(date, tps, players_online, cpu_usage, ram_usage, entities, chunks_loaded, server_id)" + "(date, tps, players_online, cpu_usage, ram_usage, entities, chunks_loaded, server_id)" +
" SELECT date, tps, players_online, cpu_usage, ram_usage, entities, chunks_loaded, '" + serverID + "'" + " SELECT date, tps, players_online, cpu_usage, ram_usage, entities, chunks_loaded, '" + serverID + "'" +
" FROM " + tempTableName; " FROM " + tempTableName;
db.execute(statement); execute(statement);
dropTable(tempTableName); dropTable(tempTableName);
} }

View File

@ -48,7 +48,7 @@ public class WorldTimesOptimizationPatch extends Patch {
tempOldTable(); tempOldTable();
db.getWorldTimesTable().createTable(); db.getWorldTimesTable().createTable();
db.execute("INSERT INTO " + tableName + " (" + execute("INSERT INTO " + tableName + " (" +
Col.UUID + ", " + Col.UUID + ", " +
Col.SERVER_UUID + ", " + Col.SERVER_UUID + ", " +
Col.ADVENTURE + ", " + Col.ADVENTURE + ", " +

View File

@ -66,7 +66,7 @@ public class WorldTimesSeverIDPatch extends Patch {
"server_id=?" + "server_id=?" +
" WHERE " + WorldTimesTable.Col.SESSION_ID + "=?"; " WHERE " + WorldTimesTable.Col.SESSION_ID + "=?";
db.executeBatch(new ExecStatement(sql) { executeBatch(new ExecStatement(sql) {
@Override @Override
public void prepare(PreparedStatement statement) throws SQLException { public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<Integer, Integer> entry : sessionIDServerIDRelation.entrySet()) { for (Map.Entry<Integer, Integer> entry : sessionIDServerIDRelation.entrySet()) {

View File

@ -49,7 +49,7 @@ public class WorldsOptimizationPatch extends Patch {
tempOldTable(); tempOldTable();
db.getWorldTable().createTable(); db.getWorldTable().createTable();
db.execute("INSERT INTO " + tableName + " (" + execute("INSERT INTO " + tableName + " (" +
Col.ID + ", " + Col.ID + ", " +
Col.SERVER_UUID + ", " + Col.SERVER_UUID + ", " +
Col.NAME + Col.NAME +

View File

@ -82,7 +82,7 @@ public class WorldsServerIDPatch extends Patch {
} }
updateWorldTimesTableWorldIDs(); updateWorldTimesTableWorldIDs();
db.executeUnsafe("DELETE FROM " + WorldTable.TABLE_NAME + " WHERE server_id=0"); executeSwallowingExceptions("DELETE FROM " + WorldTable.TABLE_NAME + " WHERE server_id=0");
} }
private Set<String> getWorldNamesOld(UUID serverUUID) { private Set<String> getWorldNamesOld(UUID serverUUID) {
@ -136,7 +136,7 @@ public class WorldsServerIDPatch extends Patch {
WorldTimesTable.Col.WORLD_ID + "=?" + WorldTimesTable.Col.WORLD_ID + "=?" +
" WHERE " + WorldTimesTable.Col.WORLD_ID + "=?" + " WHERE " + WorldTimesTable.Col.WORLD_ID + "=?" +
" AND " + "server_id=?"; " AND " + "server_id=?";
db.executeBatch(new ExecStatement(sql) { executeBatch(new ExecStatement(sql) {
@Override @Override
public void prepare(PreparedStatement statement) throws SQLException { public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<WorldObj, List<WorldObj>> entry : oldToNewMap.entrySet()) { for (Map.Entry<WorldObj, List<WorldObj>> entry : oldToNewMap.entrySet()) {

View File

@ -29,6 +29,7 @@ import com.djrapitops.plugin.task.AbsRunnable;
* *
* @author Rsl1122 * @author Rsl1122
*/ */
@Deprecated
public class PatchTask extends AbsRunnable { public class PatchTask extends AbsRunnable {
private final Patch[] patches; private final Patch[] patches;