Registered plan db hotswap command

This commit is contained in:
Risto Lahtela 2020-06-21 20:11:10 +03:00
parent 56341465d9
commit 9b599f13de
3 changed files with 40 additions and 105 deletions

View File

@ -263,6 +263,7 @@ public class PlanCommand {
.subcommand(backupCommand()) .subcommand(backupCommand())
.subcommand(restoreCommand()) .subcommand(restoreCommand())
.subcommand(moveCommand()) .subcommand(moveCommand())
.subcommand(hotswapCommand())
.subcommand(clearCommand()) .subcommand(clearCommand())
.subcommand(removeCommand()) .subcommand(removeCommand())
.subcommand(uninstalledCommand()) .subcommand(uninstalledCommand())
@ -305,6 +306,17 @@ public class PlanCommand {
.build(); .build();
} }
private Subcommand hotswapCommand() {
return Subcommand.builder()
.aliases("hotswap")
.requirePermission("plan.data.hotswap")
.requiredArgument("MySQL/SQlite/H2", "Type of the database to start using.")
.description("Move data between databases")
.inDepthDescription("Reloads the plugin with the other database and changes the config to match.")
.onCommand(databaseCommands::onHotswap)
.build();
}
private Subcommand clearCommand() { private Subcommand clearCommand() {
return Subcommand.builder() return Subcommand.builder()
.aliases("clear") .aliases("clear")

View File

@ -25,6 +25,8 @@ import com.djrapitops.plan.identification.Identifiers;
import com.djrapitops.plan.identification.Server; import com.djrapitops.plan.identification.Server;
import com.djrapitops.plan.identification.ServerInfo; import com.djrapitops.plan.identification.ServerInfo;
import com.djrapitops.plan.query.QuerySvc; import com.djrapitops.plan.query.QuerySvc;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.DatabaseSettings;
import com.djrapitops.plan.settings.locale.Locale; import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.locale.lang.CommandLang; import com.djrapitops.plan.settings.locale.lang.CommandLang;
import com.djrapitops.plan.settings.locale.lang.ManageLang; import com.djrapitops.plan.settings.locale.lang.ManageLang;
@ -46,6 +48,7 @@ import com.djrapitops.plugin.logging.L;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@ -57,6 +60,7 @@ public class DatabaseCommands {
private final Confirmation confirmation; private final Confirmation confirmation;
private final ColorScheme colors; private final ColorScheme colors;
private final PlanFiles files; private final PlanFiles files;
private final PlanConfig config;
private final DBSystem dbSystem; private final DBSystem dbSystem;
private final SQLiteDB.Factory sqliteFactory; private final SQLiteDB.Factory sqliteFactory;
private final QuerySvc queryService; private final QuerySvc queryService;
@ -73,6 +77,7 @@ public class DatabaseCommands {
Confirmation confirmation, Confirmation confirmation,
ColorScheme colors, ColorScheme colors,
PlanFiles files, PlanFiles files,
PlanConfig config,
DBSystem dbSystem, DBSystem dbSystem,
SQLiteDB.Factory sqliteFactory, SQLiteDB.Factory sqliteFactory,
QuerySvc queryService, QuerySvc queryService,
@ -86,6 +91,7 @@ public class DatabaseCommands {
this.confirmation = confirmation; this.confirmation = confirmation;
this.colors = colors; this.colors = colors;
this.files = files; this.files = files;
this.config = config;
this.dbSystem = dbSystem; this.dbSystem = dbSystem;
this.sqliteFactory = sqliteFactory; this.sqliteFactory = sqliteFactory;
this.queryService = queryService; this.queryService = queryService;
@ -390,4 +396,26 @@ public class DatabaseCommands {
sender.send(locale.getString(ManageLang.PROGRESS_SUCCESS)); sender.send(locale.getString(ManageLang.PROGRESS_SUCCESS));
sender.send("§aIf the server is still installed, it will automatically set itself as installed in the database."); sender.send("§aIf the server is still installed, it will automatically set itself as installed in the database.");
} }
public void onHotswap(CMDSender sender, Arguments arguments) {
DBType toDB = arguments.get(0).flatMap(DBType::getForName)
.orElseThrow(() -> new IllegalArgumentException(locale.getString(ManageLang.FAIL_INCORRECT_DB, arguments.get(0).orElse("<MySQL/SQLite/H2>"))));
try {
Database database = dbSystem.getActiveDatabaseByType(toDB);
database.init();
if (database.getState() == Database.State.CLOSED) {
return;
}
config.set(DatabaseSettings.TYPE, toDB.getName());
config.save();
} catch (DBOpException | IOException e) {
errorLogger.log(L.WARN, e, ErrorContext.builder().related(toDB).build());
sender.send(locale.getString(ManageLang.PROGRESS_FAIL, e.getMessage()));
return;
}
statusCommands.onReload(sender, new Arguments(Collections.emptyList()));
}
} }

View File

@ -1,105 +0,0 @@
/*
* 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.commands.subcommands.manage;
import com.djrapitops.plan.PlanPlugin;
import com.djrapitops.plan.settings.Permissions;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.DatabaseSettings;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.locale.lang.CmdHelpLang;
import com.djrapitops.plan.settings.locale.lang.CommandLang;
import com.djrapitops.plan.settings.locale.lang.ManageLang;
import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.storage.database.DBType;
import com.djrapitops.plan.storage.database.Database;
import com.djrapitops.plan.utilities.logging.ErrorLogger;
import com.djrapitops.plugin.command.CommandNode;
import com.djrapitops.plugin.command.CommandType;
import com.djrapitops.plugin.command.Sender;
import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.utilities.Verify;
import javax.inject.Inject;
import java.io.IOException;
import java.util.Arrays;
/**
* This manage SubCommand is used to swap to a different database and reload the
* plugin if the connection to the new database can be established.
*
* @author Rsl1122
*/
public class ManageHotSwapCommand extends CommandNode {
private final PlanPlugin plugin;
private final Locale locale;
private final DBSystem dbSystem;
private final PlanConfig config;
private final ErrorLogger errorLogger;
@Inject
public ManageHotSwapCommand(PlanPlugin plugin, Locale locale, DBSystem dbSystem, PlanConfig config, ErrorLogger errorLogger) {
super("hotswap", Permissions.MANAGE.getPermission(), CommandType.PLAYER_OR_ARGS);
this.plugin = plugin;
this.locale = locale;
this.dbSystem = dbSystem;
this.config = config;
this.errorLogger = errorLogger;
setArguments("<DB>");
setShortHelp(locale.getString(CmdHelpLang.MANAGE_HOTSWAP));
}
@Override
public void onCommand(Sender sender, String commandLabel, String[] args) {
Verify.isTrue(args.length >= 1,
() -> new IllegalArgumentException(locale.getString(CommandLang.FAIL_REQ_ONE_ARG, Arrays.toString(this.getArguments()))));
String dbName = args[0].toLowerCase();
boolean isCorrectDB = DBType.exists(dbName);
Verify.isTrue(isCorrectDB,
() -> new IllegalArgumentException(locale.getString(ManageLang.FAIL_INCORRECT_DB, dbName)));
Verify.isFalse(dbName.equals(dbSystem.getDatabase().getType().getConfigName()),
() -> new IllegalArgumentException(locale.getString(ManageLang.FAIL_SAME_DB)));
try {
Database database = dbSystem.getActiveDatabaseByName(dbName);
database.init();
if (database.getState() == Database.State.CLOSED) {
return;
}
} catch (Exception e) {
errorLogger.log(L.ERROR, this.getClass(), e);
sender.sendMessage(locale.getString(ManageLang.PROGRESS_FAIL, e.getMessage()));
return;
}
try {
config.set(DatabaseSettings.TYPE, dbName);
config.save();
} catch (IOException e) {
errorLogger.log(L.ERROR, this.getClass(), e);
return;
}
plugin.reloadPlugin(true);
}
}