mirror of
https://github.com/ChestShop-authors/ChestShop-3.git
synced 2024-11-23 18:45:31 +01:00
Make accounts v3 migration more robust to errors (Fixes #58)
This commit is contained in:
parent
70fa6292b1
commit
f4e060071d
@ -1,9 +1,14 @@
|
||||
package com.Acrobot.ChestShop.Database;
|
||||
|
||||
import com.Acrobot.ChestShop.ChestShop;
|
||||
import com.j256.ormlite.dao.CloseableIterator;
|
||||
import com.j256.ormlite.dao.Dao;
|
||||
import com.j256.ormlite.dao.GenericRawResults;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
* File handling the database migrations
|
||||
@ -67,10 +72,39 @@ public class Migrations {
|
||||
private static boolean migrateTo3() {
|
||||
try {
|
||||
Dao<Account, String> accountsOld = DaoCreator.getDao(Account.class);
|
||||
accountsOld.executeRaw("ALTER TABLE `accounts` RENAME TO `accounts-old`");
|
||||
accountsOld.executeRawNoArgs("ALTER TABLE `accounts` RENAME TO `accounts-old`");
|
||||
|
||||
Dao<Account, String> accounts = DaoCreator.getDaoAndCreateTable(Account.class);
|
||||
accounts.executeRaw("INSERT INTO `accounts` (name, shortName, uuid) SELECT name, shortName, uuid FROM `accounts-old`");
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
try {
|
||||
accounts.executeRawNoArgs("INSERT INTO `accounts` (name, shortName, uuid) SELECT name, shortName, uuid FROM `accounts-old`");
|
||||
} catch (SQLException e) {
|
||||
ChestShop.getBukkitLogger().log(Level.WARNING, "Fast accounts migration failed! (" + e.getMessage() + "). Starting slow migration...");
|
||||
accounts.executeRawNoArgs("DELETE FROM `accounts`");
|
||||
|
||||
GenericRawResults<String[]> results = accounts.queryRaw("SELECT name, shortName, uuid FROM `accounts-old`");
|
||||
Date zero = new Date(0);
|
||||
int success = 0;
|
||||
int error = 0;
|
||||
CloseableIterator<String[]> resultIterator = results.closeableIterator();
|
||||
while (resultIterator.hasNext()) {
|
||||
String[] strings = resultIterator.next();
|
||||
Account account = new Account(strings[0], UUID.fromString(strings[2]));
|
||||
account.setShortName(strings[1]);
|
||||
account.setLastSeen(zero);
|
||||
try {
|
||||
accounts.create(account);
|
||||
success++;
|
||||
} catch (SQLException x) {
|
||||
error++;
|
||||
ChestShop.getBukkitLogger().log(Level.SEVERE, "Could not load account " + account.getName() + "/" + account.getShortName() + "/" + account.getUuid() + " to new database format", x);
|
||||
}
|
||||
}
|
||||
results.close();
|
||||
ChestShop.getBukkitLogger().log(Level.INFO, success + " accounts successfully migrated. " + error + " accounts failed to migrate!");
|
||||
}
|
||||
ChestShop.getBukkitLogger().log(Level.INFO, "Migration of accounts table finished in " + (System.currentTimeMillis() - start) / 1000.0 + "s!");
|
||||
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
|
Loading…
Reference in New Issue
Block a user