Migrate all versions and disable on fail.

This should make it possible to theoretically upgrade from version 1 to version 3 databases.
This commit is contained in:
Phoenix616 2017-11-26 18:45:35 +01:00
parent 5d73320889
commit 0354258251
2 changed files with 16 additions and 13 deletions

View File

@ -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) {

View File

@ -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