Refactored RemoveOperations#everything into a Transaction

This commit is contained in:
Rsl1122 2019-01-23 18:34:24 +02:00
parent 87a61380f1
commit 766749fe41
9 changed files with 64 additions and 17 deletions

View File

@ -20,6 +20,7 @@ import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.api.exceptions.database.DBOpException; import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.db.DBType; import com.djrapitops.plan.db.DBType;
import com.djrapitops.plan.db.Database; import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.access.transactions.RemoveEverythingTransaction;
import com.djrapitops.plan.system.database.DBSystem; import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.locale.Locale; import com.djrapitops.plan.system.locale.Locale;
import com.djrapitops.plan.system.locale.lang.CmdHelpLang; import com.djrapitops.plan.system.locale.lang.CmdHelpLang;
@ -101,7 +102,7 @@ public class ManageClearCommand extends CommandNode {
try { try {
sender.sendMessage(locale.getString(ManageLang.PROGRESS_START)); sender.sendMessage(locale.getString(ManageLang.PROGRESS_START));
database.remove().everything(); database.executeTransaction(new RemoveEverythingTransaction());
sender.sendMessage(locale.getString(ManageLang.PROGRESS_SUCCESS)); sender.sendMessage(locale.getString(ManageLang.PROGRESS_SUCCESS));
} catch (DBOpException e) { } catch (DBOpException e) {

View File

@ -0,0 +1,51 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.db.access.transactions;
import com.djrapitops.plan.db.sql.tables.*;
/**
* Transaction that removes everything from the database.
*
* @author Rsl1122
*/
public class RemoveEverythingTransaction extends Transaction {
@Override
protected void execute() {
// Delete statements are run in a specific order as some tables have foreign keys,
// or had at some point in the past.
clearTable(SettingsTable.TABLE_NAME);
clearTable(GeoInfoTable.TABLE_NAME);
clearTable(NicknamesTable.TABLE_NAME);
clearTable(KillsTable.TABLE_NAME);
clearTable(WorldTimesTable.TABLE_NAME);
clearTable(SessionsTable.TABLE_NAME);
clearTable(WorldTable.TABLE_NAME);
clearTable(PingTable.TABLE_NAME);
clearTable(UserInfoTable.TABLE_NAME);
clearTable(UsersTable.TABLE_NAME);
clearTable(CommandUseTable.TABLE_NAME);
clearTable(TPSTable.TABLE_NAME);
clearTable(SecurityTable.TABLE_NAME);
clearTable(ServerTable.TABLE_NAME);
}
private void clearTable(String tableName) {
execute("DELETE FROM " + tableName);
}
}

View File

@ -17,6 +17,7 @@
package com.djrapitops.plan.db.sql.tables.move; package com.djrapitops.plan.db.sql.tables.move;
import com.djrapitops.plan.db.SQLDB; import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.transactions.RemoveEverythingTransaction;
import com.djrapitops.plan.db.sql.queries.LargeFetchQueries; import com.djrapitops.plan.db.sql.queries.LargeFetchQueries;
import com.djrapitops.plan.db.sql.tables.Table; import com.djrapitops.plan.db.sql.tables.Table;
import com.djrapitops.plan.db.sql.tables.UsersTable; import com.djrapitops.plan.db.sql.tables.UsersTable;
@ -67,7 +68,7 @@ public class BatchOperationTable extends Table {
@Override @Override
public void removeAllData() { public void removeAllData() {
db.remove().everything(); db.executeTransaction(new RemoveEverythingTransaction());
} }
public void copyEverything(BatchOperationTable toDB) { public void copyEverything(BatchOperationTable toDB) {

View File

@ -18,7 +18,5 @@ package com.djrapitops.plan.system.database.databases.operation;
public interface RemoveOperations { public interface RemoveOperations {
void everything();
void webUser(String name); void webUser(String name);
} }

View File

@ -17,7 +17,6 @@
package com.djrapitops.plan.system.database.databases.sql.operation; package com.djrapitops.plan.system.database.databases.sql.operation;
import com.djrapitops.plan.db.SQLDB; import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.sql.tables.Table;
import com.djrapitops.plan.system.database.databases.operation.RemoveOperations; import com.djrapitops.plan.system.database.databases.operation.RemoveOperations;
public class SQLRemoveOps extends SQLOps implements RemoveOperations { public class SQLRemoveOps extends SQLOps implements RemoveOperations {
@ -26,13 +25,6 @@ public class SQLRemoveOps extends SQLOps implements RemoveOperations {
super(db); super(db);
} }
@Override
public void everything() {
for (Table table : db.getAllTablesInRemoveOrder()) {
table.removeAllData();
}
}
@Override @Override
public void webUser(String userName) { public void webUser(String userName) {
securityTable.removeUser(userName); securityTable.removeUser(userName);

View File

@ -29,6 +29,7 @@ import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.data.time.GMTimes; import com.djrapitops.plan.data.time.GMTimes;
import com.djrapitops.plan.data.time.WorldTimes; import com.djrapitops.plan.data.time.WorldTimes;
import com.djrapitops.plan.db.access.Query; import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.transactions.RemoveEverythingTransaction;
import com.djrapitops.plan.db.access.transactions.RemovePlayerTransaction; import com.djrapitops.plan.db.access.transactions.RemovePlayerTransaction;
import com.djrapitops.plan.db.patches.Patch; import com.djrapitops.plan.db.patches.Patch;
import com.djrapitops.plan.db.sql.queries.LargeFetchQueries; import com.djrapitops.plan.db.sql.queries.LargeFetchQueries;
@ -123,7 +124,7 @@ public abstract class CommonDBTest {
} }
}.apply(); }.apply();
db.createTables(); db.createTables();
db.remove().everything(); db.executeTransaction(new RemoveEverythingTransaction());
ServerTable serverTable = db.getServerTable(); ServerTable serverTable = db.getServerTable();
serverTable.saveCurrentServerInfo(new Server(-1, serverUUID, "ServerName", "", 20)); serverTable.saveCurrentServerInfo(new Server(-1, serverUUID, "ServerName", "", 20));
assertEquals(serverUUID, db.getServerUUIDSupplier().get()); assertEquals(serverUUID, db.getServerUUIDSupplier().get());
@ -553,7 +554,7 @@ public abstract class CommonDBTest {
public void testRemovalEverything() throws NoSuchAlgorithmException { public void testRemovalEverything() throws NoSuchAlgorithmException {
saveAllData(db); saveAllData(db);
db.remove().everything(); db.executeTransaction(new RemoveEverythingTransaction());
assertQueryIsEmpty(db, LargeFetchQueries.fetchAllCommonUserInformation()); assertQueryIsEmpty(db, LargeFetchQueries.fetchAllCommonUserInformation());
assertQueryIsEmpty(db, LargeFetchQueries.fetchPerServerUserInformation()); assertQueryIsEmpty(db, LargeFetchQueries.fetchPerServerUserInformation());

View File

@ -19,6 +19,7 @@ package com.djrapitops.plan.db;
import com.djrapitops.plan.api.exceptions.database.DBInitException; import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.data.store.containers.ServerContainer; import com.djrapitops.plan.data.store.containers.ServerContainer;
import com.djrapitops.plan.data.store.keys.ServerKeys; import com.djrapitops.plan.data.store.keys.ServerKeys;
import com.djrapitops.plan.db.access.transactions.RemoveEverythingTransaction;
import com.djrapitops.plan.db.tasks.PatchTask; import com.djrapitops.plan.db.tasks.PatchTask;
import com.djrapitops.plan.system.locale.Locale; import com.djrapitops.plan.system.locale.Locale;
import com.djrapitops.plan.system.settings.config.PlanConfig; import com.djrapitops.plan.system.settings.config.PlanConfig;
@ -124,6 +125,6 @@ public class DBPatchH2RegressionTest extends DBPatchRegressionTest {
OptionalAssert.equals(1, server.getValue(ServerKeys.PLAYER_KILL_COUNT)); OptionalAssert.equals(1, server.getValue(ServerKeys.PLAYER_KILL_COUNT));
// Make sure no foreign key checks fail on removal // Make sure no foreign key checks fail on removal
underTest.remove().everything(); underTest.executeTransaction(new RemoveEverythingTransaction());
} }
} }

View File

@ -20,6 +20,7 @@ import com.djrapitops.plan.api.exceptions.EnableException;
import com.djrapitops.plan.api.exceptions.database.DBInitException; import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.data.store.containers.ServerContainer; import com.djrapitops.plan.data.store.containers.ServerContainer;
import com.djrapitops.plan.data.store.keys.ServerKeys; import com.djrapitops.plan.data.store.keys.ServerKeys;
import com.djrapitops.plan.db.access.transactions.RemoveEverythingTransaction;
import com.djrapitops.plan.db.tasks.PatchTask; import com.djrapitops.plan.db.tasks.PatchTask;
import com.djrapitops.plan.system.PlanSystem; import com.djrapitops.plan.system.PlanSystem;
import com.djrapitops.plan.system.locale.Locale; import com.djrapitops.plan.system.locale.Locale;
@ -147,6 +148,6 @@ public class DBPatchMySQLRegressionTest extends DBPatchRegressionTest {
OptionalAssert.equals(1, server.getValue(ServerKeys.PLAYER_KILL_COUNT)); OptionalAssert.equals(1, server.getValue(ServerKeys.PLAYER_KILL_COUNT));
// Make sure no foreign key checks fail on removal // Make sure no foreign key checks fail on removal
underTest.remove().everything(); underTest.executeTransaction(new RemoveEverythingTransaction());
} }
} }

View File

@ -19,6 +19,7 @@ package com.djrapitops.plan.db;
import com.djrapitops.plan.api.exceptions.database.DBInitException; import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.data.store.containers.ServerContainer; import com.djrapitops.plan.data.store.containers.ServerContainer;
import com.djrapitops.plan.data.store.keys.ServerKeys; import com.djrapitops.plan.data.store.keys.ServerKeys;
import com.djrapitops.plan.db.access.transactions.RemoveEverythingTransaction;
import com.djrapitops.plan.db.tasks.PatchTask; import com.djrapitops.plan.db.tasks.PatchTask;
import com.djrapitops.plan.system.locale.Locale; import com.djrapitops.plan.system.locale.Locale;
import com.djrapitops.plugin.logging.L; import com.djrapitops.plugin.logging.L;
@ -117,6 +118,6 @@ public class DBPatchSQLiteRegressionTest extends DBPatchRegressionTest {
OptionalAssert.equals(1, server.getValue(ServerKeys.PLAYER_KILL_COUNT)); OptionalAssert.equals(1, server.getValue(ServerKeys.PLAYER_KILL_COUNT));
// Make sure no foreign key checks fail on removal // Make sure no foreign key checks fail on removal
underTest.remove().everything(); underTest.executeTransaction(new RemoveEverythingTransaction());
} }
} }