Start work on Migrators

This commit is contained in:
William committed 2022-08-27 00:46:24 +01:00
1 parent ca9e60394d
commit bee1c811b2
9 files changed
+416 -7

No files matched your search

@@ -21,6 +21,8 @@ import net.william278.huskhomes.listener.EventListener;
import net.william278.huskhomes.messenger.NetworkMessenger;
import net.william278.huskhomes.messenger.PluginMessenger;
import net.william278.huskhomes.messenger.RedisMessenger;
import net.william278.huskhomes.migrator.LegacyMigrator;
import net.william278.huskhomes.migrator.Migrator;
import net.william278.huskhomes.player.BukkitPlayer;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.position.Location;
@@ -72,6 +74,8 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
private Set<PluginHook> pluginHooks;
private List<CommandBase> registeredCommands;
private List<Migrator> migrators;
@Nullable
private NetworkMessenger networkMessenger;
@@ -100,11 +104,17 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
this.logger = new BukkitLogger(getLogger());
this.resourceReader = new BukkitResourceReader(this);
// Detect if an upgrade is needed
final BukkitUpgradeUtil upgradeData = BukkitUpgradeUtil.detect(this);
// Load settings and locales
getLoggingAdapter().log(Level.INFO, "Loading plugin configuration settings & locales...");
initialized.set(reload().join());
if (initialized.get()) {
logger.showDebugLogs(settings.debugLogging);
if (upgradeData != null) {
upgradeData.upgradeSettings(settings);
}
getLoggingAdapter().log(Level.INFO, "Successfully loaded plugin configuration settings & locales");
} else {
throw new HuskHomesInitializationException("Failed to load plugin configuration settings and/or locales");
@@ -229,6 +239,11 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
});
getLoggingAdapter().log(Level.INFO, "Successfully registered permissions & commands.");
// Prepare migrators
this.migrators = new ArrayList<>();
this.migrators.add(new LegacyMigrator(this));
//todo EssentialsX
// Hook into bStats metrics
try {
new Metrics(this, METRICS_ID);
@@ -244,6 +259,18 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
"An update is available for HuskHomes, v" + newVersion
+ " (Currently running v" + getPluginVersion() + ")")));
}
// Perform automatic upgrade if detected
if (upgradeData != null) {
getLoggingAdapter().log(Level.INFO, "Performing automatic upgrade...");
new LegacyMigrator(this, upgradeData).start().thenAccept(result -> {
if (result) {
getLoggingAdapter().log(Level.INFO, "Successfully performed automatic upgrade.");
} else {
getLoggingAdapter().log(Level.WARNING, "Failed to perform automatic upgrade.");
}
});
}
} catch (HuskHomesInitializationException exception) {
getLoggingAdapter().log(Level.SEVERE, exception.getMessage());
initialized.set(false);
@@ -348,6 +375,11 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes {
return eventDispatcher;
}
@Override
public List<Migrator> getMigrators() {
return migrators;
}
@Override
public Optional<Spawn> getServerSpawn() {
return Optional.ofNullable(serverSpawn);
@@ -0,0 +1,175 @@
package net.william278.huskhomes.migrator;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.config.Settings;
import net.william278.huskhomes.util.BukkitUpgradeUtil;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
public class LegacyMigrator extends Migrator {
public Settings.DatabaseType sourceDatabaseType;
public String sourceMySqlHost;
public int sourceMySqlPort;
public String sourceMySqlDatabase;
public String sourceMySqlUsername;
public String sourceMySqlPassword;
public String sourcePlayerDataTable = "huskhomes_player_data";
public String sourceLocationsDataTable = "huskhomes_location_data";
public String sourceHomesDataTable = "huskhomes_home_data";
public String sourceWarpsDataTable = "huskhomes_warp_data";
public LegacyMigrator(@NotNull HuskHomes implementor) {
super(implementor);
this.sourceDatabaseType = implementor.getSettings().databaseType;
this.sourceMySqlHost = implementor.getSettings().mySqlHost;
this.sourceMySqlPort = implementor.getSettings().mySqlPort;
this.sourceMySqlDatabase = implementor.getSettings().mySqlDatabase;
this.sourceMySqlUsername = implementor.getSettings().mySqlUsername;
this.sourceMySqlPassword = implementor.getSettings().mySqlPassword;
}
public LegacyMigrator(@NotNull HuskHomes implementor, @NotNull BukkitUpgradeUtil bukkitUpgradeUtil) {
super(implementor);
this.sourceDatabaseType = bukkitUpgradeUtil.databaseType;
this.sourceMySqlHost = bukkitUpgradeUtil.mySqlHost;
this.sourceMySqlPort = bukkitUpgradeUtil.mySqlPort;
this.sourceMySqlDatabase = bukkitUpgradeUtil.mySqlDatabase;
this.sourceMySqlUsername = bukkitUpgradeUtil.mySqlUsername;
this.sourceMySqlPassword = bukkitUpgradeUtil.mySqlPassword;
this.sourcePlayerDataTable = bukkitUpgradeUtil.sourcePlayerDataTable;
this.sourceLocationsDataTable = bukkitUpgradeUtil.sourceLocationsDataTable;
this.sourceHomesDataTable = bukkitUpgradeUtil.sourceHomesDataTable;
this.sourceWarpsDataTable = bukkitUpgradeUtil.sourceWarpsDataTable;
}
@Override
public CompletableFuture<Boolean> start() {
return CompletableFuture.supplyAsync(() -> {
if (sourceDatabaseType == Settings.DatabaseType.SQLITE) {
migrateSqLiteDatabase();
return true;
}
if (sourceDatabaseType == plugin.getSettings().databaseType
&& sourceMySqlHost.equals(plugin.getSettings().mySqlHost)
&& sourceMySqlPort == plugin.getSettings().mySqlPort) {
migrateMySqlDatabase();
return true;
}
plugin.getLoggingAdapter().log(Level.SEVERE, "Migration failed! Different source and target databases/types." +
"Please dump your data from your source database manually and then" +
" import it to your new database, then attempt migration again.");
return false;
});
}
private void migrateSqLiteDatabase() {
plugin.getLoggingAdapter().log(Level.INFO, "Migrating SQLite database...");
//todo write script
}
private void migrateMySqlDatabase() {
plugin.getLoggingAdapter().log(Level.INFO, "Migrating same MySQL database...");
//todo write script
}
@Override
public void handleConfigurationCommand(@NotNull String[] args) {
if (args.length == 2) {
if (switch (args[0].toLowerCase()) {
case "database" -> {
this.sourceMySqlDatabase = args[1];
yield true;
}
case "player_data_table" -> {
this.sourcePlayerDataTable = args[1];
yield true;
}
case "locations_data_table" -> {
this.sourceLocationsDataTable = args[1];
yield true;
}
case "homes_data_table" -> {
this.sourceHomesDataTable = args[1];
yield true;
}
case "warps_data_table" -> {
this.sourceWarpsDataTable = args[1];
yield true;
}
default -> false;
}) {
plugin.getLoggingAdapter().log(Level.INFO, getHelpMenu());
plugin.getLoggingAdapter().log(Level.INFO, "Successfully set " + args[0] + " to " +
obfuscateDataString(args[1]));
} else {
plugin.getLoggingAdapter().log(Level.INFO, "Invalid operation, could not set " + args[0] + " to " +
obfuscateDataString(args[1]) + " (is it a valid option?)");
}
} else {
plugin.getLoggingAdapter().log(Level.INFO, getHelpMenu());
}
}
@Override
public @NotNull String getIdentifier() {
return "legacy";
}
@Override
public @NotNull String getName() {
return "HuskHomes Legacy (v2.x)";
}
@NotNull
@Override
public String getHelpMenu() {
return """
=== HuskHomes v2.x --> v3.x Migration Wizard =========
This will migrate all user data from HuskHomes v2.x
to HuskHomes v3.x. The source and target databases
must be the same, please note.
[!] Existing data in the database will be wiped. [!]
STEP 1] Please ensure no players are on any servers
running HuskHomes. If you're running MySQL, you only
need to do this once - it's best to only have one
server running.
STEP 2] Ensure the following settings are correct:
Source table names:
- player_data: %source_players_table%
- locations_data: %source_locations_table%
- homes_data: %source_homes_table%
- warps_data: %source_warps_table%
Source MySQL database name (ignore if using SQLite):
- database: %source_mysql_database%
If any of these are not correct, please correct them
using the command:
"huskhomes migrate legacy set <parameter> <value>"
(e.g.: "huskhomes migrate legacy set database foo")
STEP 3] HuskHomes will migrate data into the database
tables configured in the config.yml file of this
server. Please make sure you're happy with this
before proceeding.
STEP 4] To start the migration, please run:
"huskhomes migrate legacy start"
""".replaceAll("%source_players_table%", sourcePlayerDataTable)
.replaceAll("%source_locations_table%", sourceLocationsDataTable)
.replaceAll("%source_homes_table%", sourceHomesDataTable)
.replaceAll("%source_warps_table%", sourceWarpsDataTable)
.replaceAll("%source_mysql_database%", sourceMySqlDatabase);
}
}
@@ -0,0 +1,82 @@
package net.william278.huskhomes.util;
import net.william278.annotaml.Annotaml;
import net.william278.huskhomes.BukkitHuskHomes;
import net.william278.huskhomes.config.Settings;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.logging.Level;
/**
* Utility class for upgrading important stuff from HuskHomes v2.x config files to v3.x.
*/
public class BukkitUpgradeUtil {
private final BukkitHuskHomes plugin;
public Settings.DatabaseType databaseType;
public String mySqlHost;
public int mySqlPort;
public String mySqlDatabase;
public String mySqlUsername;
public String mySqlPassword;
public String mySqlParams;
public String sourcePlayerDataTable;
public String sourceLocationsDataTable;
public String sourceHomesDataTable;
public String sourceWarpsDataTable;
private BukkitUpgradeUtil(@NotNull BukkitHuskHomes plugin) {
this.plugin = plugin;
}
/**
* Reads old settings from the config and writes them to the new config.
*/
private void readOldSettings() {
this.databaseType = Settings.DatabaseType.valueOf(plugin.getConfig()
.getString("data_storage_options.storage_type", "SQLITE").toUpperCase());
this.mySqlHost = plugin.getConfig().getString("data_storage_options.mysql_credentials.host", "localhost");
this.mySqlPort = plugin.getConfig().getInt("data_storage_options.mysql_credentials.port", 3306);
this.mySqlDatabase = plugin.getConfig().getString("data_storage_options.mysql_credentials.database", "HuskHomes");
this.mySqlUsername = plugin.getConfig().getString("data_storage_options.mysql_credentials.username", "root");
this.mySqlPassword = plugin.getConfig().getString("data_storage_options.mysql_credentials.password", "pa55w0rd");
this.mySqlParams = plugin.getConfig().getString("data_storage_options.mysql_credentials.params", "?autoReconnect=true&useSSL=false");
this.sourcePlayerDataTable = plugin.getConfig().getString("data_storage_options.table_names.player_data", "huskhomes_player_data");
this.sourceLocationsDataTable = plugin.getConfig().getString("data_storage_options.table_names.locations_data", "huskhomes_location_data");
this.sourceHomesDataTable = plugin.getConfig().getString("data_storage_options.table_names.homes_data", "huskhomes_home_data");
this.sourceWarpsDataTable = plugin.getConfig().getString("data_storage_options.table_names.warps_data", "huskhomes_warp_data");
}
/**
* Upgrade the config from an older version to the current version.
*
* @param settings The settings to upgrade
*/
public void upgradeSettings(@NotNull Settings settings) {
plugin.getLoggingAdapter().log(Level.WARNING, "Upgrading critical HuskHomes v2.x settings to v3.x.");
settings.databaseType = databaseType;
settings.mySqlHost = mySqlHost;
settings.mySqlPort = mySqlPort;
settings.mySqlDatabase = mySqlDatabase;
settings.mySqlUsername = mySqlUsername;
settings.mySqlPassword = mySqlPassword;
settings.mySqlConnectionParameters = mySqlParams;
Annotaml.save(settings, new File(plugin.getDataFolder(), "config.yml"));
}
@Nullable
public static BukkitUpgradeUtil detect(@NotNull BukkitHuskHomes plugin) {
if (!plugin.getConfig().contains("config_file_version")) {
return null;
}
final BukkitUpgradeUtil upgrade = new BukkitUpgradeUtil(plugin);
upgrade.readOldSettings();
return upgrade;
}
}