From 0354258251481202b60556c7eb568f149469c319 Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Sun, 26 Nov 2017 18:45:35 +0100 Subject: [PATCH] Migrate all versions and disable on fail. This should make it possible to theoretically upgrade from version 1 to version 3 databases. --- .../java/com/Acrobot/ChestShop/ChestShop.java | 13 ++++++++++--- .../Acrobot/ChestShop/Database/Migrations.java | 16 ++++++---------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/Acrobot/ChestShop/ChestShop.java b/src/main/java/com/Acrobot/ChestShop/ChestShop.java index 74b0915..ef1827f 100644 --- a/src/main/java/com/Acrobot/ChestShop/ChestShop.java +++ b/src/main/java/com/Acrobot/ChestShop/ChestShop.java @@ -91,7 +91,9 @@ public class ChestShop extends JavaPlugin { Configuration.pairFileAndClass(loadFile("local.yml"), Messages.class); turnOffDatabaseLogging(); - handleMigrations(); + if (!handleMigrations()) { + return; + } itemDatabase = new ItemDatabase(); @@ -164,7 +166,7 @@ public class ChestShop extends JavaPlugin { }); } - private void handleMigrations() { + private boolean handleMigrations() { File versionFile = loadFile("version"); YamlConfiguration previousVersion = YamlConfiguration.loadConfiguration(versionFile); @@ -181,7 +183,11 @@ public class ChestShop extends JavaPlugin { int lastVersion = previousVersion.getInt("version"); int newVersion = Migrations.migrate(lastVersion); - if (lastVersion != newVersion) { + if (newVersion == -1) { + plugin.getLogger().log(java.util.logging.Level.SEVERE, "Error while migrating! ChestShop can not run with a broken/outdated database..."); + plugin.getServer().getPluginManager().disablePlugin(this); + return false; + } else if (lastVersion != newVersion) { previousVersion.set("version", newVersion); try { @@ -190,6 +196,7 @@ public class ChestShop extends JavaPlugin { e.printStackTrace(); } } + return true; } public static File loadFile(String string) { diff --git a/src/main/java/com/Acrobot/ChestShop/Database/Migrations.java b/src/main/java/com/Acrobot/ChestShop/Database/Migrations.java index a8c3382..abe2d24 100644 --- a/src/main/java/com/Acrobot/ChestShop/Database/Migrations.java +++ b/src/main/java/com/Acrobot/ChestShop/Database/Migrations.java @@ -33,22 +33,18 @@ public class Migrations { switch (currentVersion) { case 1: - boolean migrated = migrateTo2(); - - if (migrated) { + if (migrateTo2()) { currentVersion++; + } else { + return -1; } - break; case 2: - - boolean migrated3 = migrateTo3(); - - if (migrated3) { + if (migrateTo3()) { currentVersion++; + } else { + return -1; } - break; case 3: - break; default: break; //do nothing