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;
}
public PluginLogger getLogger() {
return logger;
}
public Locale getLocale() {
return locale;
}
public NetworkContainer.Factory getNetworkContainerFactory() {
return networkContainerFactory;
}

View File

@ -135,4 +135,9 @@ public abstract class Transaction {
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.SESSION_END + "-" + SessionsTable.Col.SESSION_START +
")) < 5 AND " + SessionsTable.Col.AFK_TIME + "!=0";
db.execute(sql);
execute(sql);
}
}

View File

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

View File

@ -80,7 +80,7 @@ public class IPAnonPatch extends Patch {
GeoInfoTable.Col.IP_HASH + "=? " +
"WHERE " + GeoInfoTable.Col.IP + "=?";
db.executeBatch(new ExecStatement(sql) {
executeBatch(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (List<GeoInfo> geoInfos : allGeoInfo.values()) {
@ -122,7 +122,7 @@ public class IPAnonPatch extends Patch {
boolean hasUserIdColumn = hasColumn(tempTableName, "user_id");
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" +
") SELECT " +
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();
db.getKillsTable().createTable();
db.execute("INSERT INTO " + tableName + " (" +
execute("INSERT INTO " + tableName + " (" +
Col.VICTIM_UUID + ", " +
Col.KILLER_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 + "=?";
db.executeBatch(new ExecStatement(sql) {
executeBatch(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
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
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))");
Map<Integer, UUID> serverUUIDsByID = getServerUUIDsByID();
@ -64,7 +64,7 @@ public class NicknameLastSeenPatch extends Patch {
Map<Integer, Set<Nickname>> nicknames = getNicknamesByUserID(serverUUIDsByID);
updateLastUsed(serverIDsByUUID, nicknames);
db.executeUnsafe("DROP TABLE plan_actions");
executeSwallowingExceptions("DROP TABLE plan_actions");
}
private Map<Integer, UUID> getServerUUIDsByID() {
@ -117,7 +117,7 @@ public class NicknameLastSeenPatch extends Patch {
" AND user_id=?" +
" AND server_id=?";
db.executeBatch(new ExecStatement(updateSQL) {
executeBatch(new ExecStatement(updateSQL) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<Integer, Set<Nickname>> entry : nicknames.entrySet()) {

View File

@ -47,7 +47,7 @@ public class NicknamesOptimizationPatch extends Patch {
tempOldTable();
db.getNicknamesTable().createTable();
db.execute("INSERT INTO " + tableName + " (" +
execute("INSERT INTO " + tableName + " (" +
Col.UUID + ", " +
Col.SERVER_UUID + ", " +
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.access.QueryAllStatement;
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.queries.MySQLSchemaQueries;
import com.djrapitops.plan.system.settings.paths.DatabaseSettings;
@ -32,12 +33,13 @@ import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
public abstract class Patch {
public abstract class Patch extends Transaction {
protected final SQLDB db;
protected final DBType dbType;
public Patch(SQLDB db) {
setDb(db);
this.db = db;
this.dbType = db.getType();
}
@ -46,22 +48,26 @@ public abstract class Patch {
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();
applyPatch();
if (dbType == DBType.MYSQL) enableForeignKeyChecks();
// }
}
@Deprecated
public void apply() {
db.executeTransaction(this);
}
private void enableForeignKeyChecks() {
db.execute("SET FOREIGN_KEY_CHECKS=1");
execute("SET FOREIGN_KEY_CHECKS=1");
}
private void disableForeignKeyChecks() {
db.execute("SET FOREIGN_KEY_CHECKS=0");
}
public <T> T query(QueryStatement<T> query) {
return db.query(query);
execute("SET FOREIGN_KEY_CHECKS=0");
}
protected boolean hasTable(String tableName) {
@ -138,15 +144,15 @@ public abstract class Patch {
}
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) {
db.execute(TableSqlParser.dropTable(name));
execute(TableSqlParser.dropTable(name));
}
protected void renameTable(String from, String to) {
db.execute(getRenameTableSQL(from, to));
execute(getRenameTableSQL(from, to));
}
private String getRenameTableSQL(String from, String to) {
@ -172,7 +178,7 @@ public abstract class Patch {
for (MySQLSchemaQueries.ForeignKeyConstraint constraint : constraints) {
// Uses information from https://stackoverflow.com/a/34574758
db.execute("ALTER TABLE " + constraint.getTable() +
execute("ALTER TABLE " + constraint.getTable() +
" DROP FOREIGN KEY " + constraint.getConstraintName());
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -82,7 +82,7 @@ public class WorldsServerIDPatch extends Patch {
}
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) {
@ -136,7 +136,7 @@ public class WorldsServerIDPatch extends Patch {
WorldTimesTable.Col.WORLD_ID + "=?" +
" WHERE " + WorldTimesTable.Col.WORLD_ID + "=?" +
" AND " + "server_id=?";
db.executeBatch(new ExecStatement(sql) {
executeBatch(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<WorldObj, List<WorldObj>> entry : oldToNewMap.entrySet()) {

View File

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