Add support to migrate SQL storage to files storage

This commit is contained in:
GeorgH93 2018-07-25 23:27:19 +02:00
parent b59e2bd368
commit 358ddf1b77
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
5 changed files with 101 additions and 10 deletions

View File

@ -114,4 +114,7 @@ permissions:
default: op
backpack.reload:
description: Allows to reload the config.
default: op
backpack.migrate:
description: Allows to migrate data from one format to another.
default: op

View File

@ -17,24 +17,25 @@
package at.pcgamingfreaks.Minepacks.Bukkit.Database;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.logging.Logger;
import at.pcgamingfreaks.Minepacks.Bukkit.API.Callback;
import at.pcgamingfreaks.Minepacks.Bukkit.Backpack;
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;
import at.pcgamingfreaks.UUIDConverter;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.logging.Logger;
public class Files extends Database
{
protected static final String EXT = ".backpack", EXT_REGEX = "\\.backpack";
protected static final String EXT = ".backpack", EXT_REGEX = "\\.backpack";
private final File saveFolder;

View File

@ -19,6 +19,7 @@
import at.pcgamingfreaks.ConsoleColor;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Database;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Files;
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;
import at.pcgamingfreaks.Reflection;
@ -50,6 +51,7 @@ public void migrateDB(final String targetDatabaseType, final MigrationCallback c
Reflection.setValue(plugin, "database", null); // Hack to prevent the unload of the database
Reflection.getMethod(Minepacks.class, "unload").invoke(plugin); // Unload plugin
HandlerList.unregisterAll(db); // Disable events for database
Reflection.setValue(plugin, "database", db);
}
catch(Exception e)
{
@ -70,9 +72,9 @@ public void migrateDB(final String targetDatabaseType, final MigrationCallback c
}
catch(Exception e)
{
plugin.getLogger().warning(ConsoleColor.RED + "There was a problem migrating from " + plugin.getDatabase().getClass().getName() + " to " + targetDatabaseType + ConsoleColor.RESET);
e.printStackTrace();
callback.onResult(new MigrationResult("There was a problem migrating from " + plugin.getDatabase().getClass().getName() + " to " + targetDatabaseType + ". Please check the console for details.", MigrationResult.MigrationResultType.ERROR));
plugin.getLogger().warning(ConsoleColor.RED + "There was a problem migrating from " + db.getClass().getName() + " to " + targetDatabaseType + ConsoleColor.RESET);
callback.onResult(new MigrationResult("There was a problem migrating from " + db.getClass().getName() + " to " + targetDatabaseType + ". Please check the console for details.", MigrationResult.MigrationResultType.ERROR));
}
//region Start the plugin again
@ -100,6 +102,21 @@ public void migrateDB(final String targetDatabaseType, final MigrationCallback c
public Migration getMigrationPerformer(final String targetDatabaseType)
{
try
{
switch(targetDatabaseType.toLowerCase())
{
case "flat":
case "file":
case "files":
if(plugin.getDatabase() instanceof Files) return null;
return new SQLtoFilesMigration(plugin, plugin.getDatabase());
}
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
}

View File

@ -0,0 +1,71 @@
/*
* Copyright (C) 2018 GeorgH93
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package at.pcgamingfreaks.Minepacks.Bukkit.Database.Migration;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.Database;
import at.pcgamingfreaks.Minepacks.Bukkit.Database.SQL;
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;
import at.pcgamingfreaks.Reflection;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class SQLtoFilesMigration extends Migration
{
private final String sqlQuery;
private final File saveFolder;
protected SQLtoFilesMigration(@NotNull Minepacks plugin, @NotNull Database oldDb) throws InvocationTargetException, IllegalAccessException
{
super(plugin, oldDb);
@Language("SQL") String query = "SELECT " + (plugin.getConfiguration().getUseUUIDs() ? "{FieldUUID}" : "{FieldName}") + ",{FieldBPITS},{FieldBPVersion} FROM {TablePlayers} INNER JOIN {TableBackpacks} ON {FieldPlayerID}={FieldBPOwner};";
sqlQuery = (String) Reflection.getMethod(SQL.class, "replacePlaceholders", String.class).invoke(plugin.getDatabase(), query);
saveFolder = new File(this.plugin.getDataFolder(), "backpacks");
if(!saveFolder.exists() && !saveFolder.mkdirs())
{
plugin.getLogger().warning("Failed to create save folder (" + saveFolder.getAbsolutePath() + ").");
}
}
@Override
public @Nullable MigrationResult migrate() throws Exception
{
int migrated = 0;
try(Connection connection = (Connection) Reflection.getMethod(SQL.class, "getConnection").invoke(plugin.getDatabase()); Statement st = connection.createStatement(); ResultSet rs = st.executeQuery(sqlQuery))
{
while(rs.next())
{
try(FileOutputStream fos = new FileOutputStream(new File(saveFolder, rs.getString(1) + ".backpack")))
{
fos.write(rs.getInt(3));
fos.write(rs.getBytes(2));
}
migrated++;
}
}
return new MigrationResult("Migrated " + migrated + " backpacks from SQL to files.", MigrationResult.MigrationResultType.SUCCESS);
}
}

View File

@ -193,10 +193,9 @@ private void unload()
{
commandManager.close();
if(collector != null) collector.cancel();
if(database != null) database.close();
if(database != null) database.close(); // Close the DB connection, we won't need them any longer
HandlerList.unregisterAll(this); // Stop the listeners
getServer().getScheduler().cancelTasks(this); // Kill all running task
database.close(); // Close the DB connection, we won't need them any longer
if(cooldownManager != null) cooldownManager.close();
cooldownManager = null;
}