#457 Improve ForceFlatToSqlite conversion

- Change ForceFlatToSqlite converter to use a generic datasource destination (i.e. can be used for Flat2MySQL later)
- Add tests, including for FlatFile
- Check that user is not present in destination datasource before adding
- Persist last location from flatfile as well
This commit is contained in:
ljacqu
2016-02-27 11:24:47 +01:00
parent 27b1fa770f
commit 1b818bd833
8 changed files with 259 additions and 70 deletions
@@ -3,39 +3,51 @@ package fr.xephi.authme.converter;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.DataSourceType;
import fr.xephi.authme.datasource.SQLite;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.properties.DatabaseSettings;
import java.sql.SQLException;
import fr.xephi.authme.datasource.FlatFile;
import fr.xephi.authme.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
* Mandatory migration from the deprecated flat file datasource to SQLite.
*/
public class ForceFlatToSqlite {
public class ForceFlatToSqlite implements Converter {
private final DataSource database;
private final NewSetting settings;
private final DataSource source;
private final DataSource destination;
public ForceFlatToSqlite(DataSource database, NewSetting settings) {
this.database = database;
this.settings = settings;
/**
* Constructor.
*
* @param source The datasource to convert (flatfile)
* @param destination The datasource to copy the data to (sqlite)
*/
public ForceFlatToSqlite(FlatFile source, DataSource destination) {
this.source = source;
this.destination = destination;
}
public DataSource run() {
try {
DataSource sqlite = new SQLite(settings);
for (PlayerAuth auth : database.getAllAuths()) {
auth.setRealName("Player");
sqlite.saveAuth(auth);
/**
* Perform the conversion.
*/
@Override
public void run() {
List<String> skippedPlayers = new ArrayList<>();
for (PlayerAuth auth : source.getAllAuths()) {
if (destination.isAuthAvailable(auth.getNickname())) {
skippedPlayers.add(auth.getNickname());
} else {
destination.saveAuth(auth);
destination.updateQuitLoc(auth);
}
settings.setProperty(DatabaseSettings.BACKEND, DataSourceType.SQLITE);
settings.save();
ConsoleLogger.info("Database successfully converted to sqlite!");
return sqlite;
} catch (SQLException | ClassNotFoundException e) {
ConsoleLogger.logException("Could not convert from Flatfile to SQLite:", e);
}
return null;
if (!skippedPlayers.isEmpty()) {
ConsoleLogger.showError("Warning: skipped conversion for players which were already in SQLite: "
+ StringUtils.join(", ", skippedPlayers));
}
ConsoleLogger.info("Database successfully converted from " + source.getClass().getSimpleName()
+ " to " + destination.getClass().getSimpleName());
}
}