Skip to content

Commit

Permalink
Merge pull request #220 from BrainStone/feature/hsql-to-mysql
Browse files Browse the repository at this point in the history
Implement automatic migration from HSQL to MySQL
  • Loading branch information
DevLeoko authored Jun 16, 2018
2 parents dcbacb5 + 76dd142 commit 0229464
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 20 deletions.
137 changes: 117 additions & 20 deletions src/main/java/me/leoko/advancedban/manager/DatabaseManager.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package me.leoko.advancedban.manager;

import me.leoko.advancedban.MethodInterface;
import me.leoko.advancedban.Universal;
import me.leoko.advancedban.utils.SQLQuery;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import me.leoko.advancedban.MethodInterface;
import me.leoko.advancedban.Universal;
import me.leoko.advancedban.utils.SQLQuery;

public class DatabaseManager {

Expand Down Expand Up @@ -70,39 +71,34 @@ public void setup(boolean useMySQLServer) {
" \n"
+ " MySQL-Error\n"
+ " Could not migrate old tables!\n"
+ " Disabling plugin!\n"
+ " Skype: Leoko33\n"
+ " Issue tracker: https://github.com/DevLeoko/AdvancedBan/issues\n"
+ " \n"
);
Universal.get().debug(ex);
}
} else {
connection = connectHSQL();
}

executeStatement(SQLQuery.CREATE_TABLE_PUNISHMENT);
executeStatement(SQLQuery.CREATE_TABLE_PUNISHMENT_HISTORY);

if (useMySQL) {
try {
Class.forName("org.hsqldb.jdbc.JDBCDriver");
} catch (ClassNotFoundException ex) {
Universal.get().log("§cERROR: failed to load HSQLDB JDBC driver.");
Universal.get().debug(ex.getMessage());
return;
}
try {
connection = DriverManager.getConnection("jdbc:hsqldb:file:" + mi.getDataFolder().getPath() + "/data/storage;hsqldb.lock_file=false", "SA", "");
migrateHSQL();
} catch (SQLException ex) {
Universal.get().log(
" \n"
+ " HSQLDB-Error\n"
+ " Could not connect to HSQLDB-Server!\n"
+ " Disabling plugin!\n"
+ " MySQL-Error\n"
+ " Could not migrate HSQLDB!\n"
+ " Skype: Leoko33\n"
+ " Issue tracker: https://github.com/DevLeoko/AdvancedBan/issues\n"
+ " \n"
);
Universal.get().debug(ex);
}
}

executeStatement(SQLQuery.CREATE_TABLE_PUNISHMENT);
executeStatement(SQLQuery.CREATE_TABLE_PUNISHMENT_HISTORY);

if (useMySQL) {
syncAutoId();
}
}
Expand Down Expand Up @@ -137,6 +133,34 @@ private void connectMySQLServer() {
}
}

private Connection connectHSQL() {
try {
Class.forName("org.hsqldb.jdbc.JDBCDriver");
} catch (ClassNotFoundException ex) {
Universal.get().log("§cERROR: failed to load HSQLDB JDBC driver.");
Universal.get().debug(ex.getMessage());

return null;
}

try {
return DriverManager.getConnection("jdbc:hsqldb:file:" + Universal.get().getMethods().getDataFolder().getPath() + "/data/storage" + ";hsqldb.lock_file=false", "SA", "");
} catch (SQLException ex) {
Universal.get().log(
" \n"
+ " HSQLDB-Error\n"
+ " Could not connect to HSQLDB-Server!\n"
+ " Disabling plugin!\n"
+ " Skype: Leoko33\n"
+ " Issue tracker: https://github.com/DevLeoko/AdvancedBan/issues\n"
+ " \n"
);
Universal.get().debug(ex);
}

return null;
}

public void executeStatement(SQLQuery sql, Object... parameters) {
executeStatement(sql, false, parameters);
}
Expand All @@ -159,6 +183,10 @@ private ResultSet executeStatement(SQLQuery sql, boolean result, Object... param
}

public ResultSet executeStatement(String sql, boolean result, Object... parameters) {
return executeStatement(connection, sql, result, parameters);
}

private ResultSet executeStatement(Connection connection, String sql, boolean result, Object... parameters) {
try {
PreparedStatement statement = connection.prepareStatement(sql);

Expand Down Expand Up @@ -243,7 +271,76 @@ private int getNextAutoId() {
private void syncAutoId() {
final int nextId = getNextAutoId();

syncAutoId(nextId);
}

private void syncAutoId(int nextId) {
executeStatement(SQLQuery.SET_PUNISHMENT_AUTO_ID.toString(), false, nextId);
executeStatement(SQLQuery.SET_PUNISHMENT_HISTORY_AUTO_ID.toString(), false, nextId);
}

private void migrateHSQL() throws SQLException {
final File dataDir = Universal.get().getMethods().getDataFolder();
final File hsqlScript = new File(dataDir, "/data/storage.script");

if (!hsqlScript.exists()) return;

final int idOffset = getNextAutoId();
int id;
int maxId = 0;

try {
try (final Connection hsqlConnection = connectHSQL()) {
try (final ResultSet result = executeStatement(hsqlConnection, SQLQuery.SELECT_ALL_PUNISHMENTS_HISTORY.getHsqldb(), true)) {
while(result.next()) {
id = result.getInt("id") + idOffset;
executeStatement(
SQLQuery.INSERT_PUNISHMENT_HISTORY_WITH_ID,
id,
result.getString("name"),
result.getString("uuid"),
result.getString("reason"),
result.getString("operator"),
result.getString("punishmentType"),
result.getLong("start"),
result.getLong("end"),
result.getString("calculation")
);

if (id > maxId)
maxId = id;
}
}

try (final ResultSet result = executeStatement(hsqlConnection, SQLQuery.SELECT_ALL_PUNISHMENTS.getHsqldb(), true)) {
while(result.next()) {
id = result.getInt("id") + idOffset;
executeStatement(
SQLQuery.INSERT_PUNISHMENT_WITH_ID,
id,
result.getString("name"),
result.getString("uuid"),
result.getString("reason"),
result.getString("operator"),
result.getString("punishmentType"),
result.getInt("start"),
result.getInt("end"),
result.getString("calculation")
);

if (id > maxId)
maxId = id;
}
}

executeStatement(hsqlConnection, "SHUTDOWN", false);
}

syncAutoId(idOffset + maxId);

Files.move(hsqlScript.getParentFile().toPath(), new File(dataDir, "/data.old").toPath());
} catch (Exception e) {
throw new SQLException("Migration failed: " + e.getMessage(), e);
}
}
}
23 changes: 23 additions & 0 deletions src/main/java/me/leoko/advancedban/utils/SQLQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public enum SQLQuery {
"(name, uuid, reason, operator, punishmentType, start, end, calculation) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
),
INSERT_PUNISHMENT_WITH_ID(
"INSERT INTO `Punishments` " +
"(`id`, `name`, `uuid`, `reason`, `operator`, `punishmentType`, `start`, `end`, `calculation`) " +
"VALUES (?, ?, ?, ?, ?, ?, FROM_UNIXTIME(? * 0.001), FROM_UNIXTIME(? * 0.001), ?)",

""
),
BUMP_PUNISHMENT_AUTO_ID(
"INSERT INTO `Punishments` (`id`) VALUES (NULL);\n" +
"DELETE FROM `Punishments` WHERE `id` = LAST_INSERT_ID()",
Expand All @@ -85,6 +92,13 @@ public enum SQLQuery {
"(name, uuid, reason, operator, punishmentType, start, end, calculation) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
),
INSERT_PUNISHMENT_HISTORY_WITH_ID(
"INSERT INTO `PunishmentHistory` " +
"(`id`, `name`, `uuid`, `reason`, `operator`, `punishmentType`, `start`, `end`, `calculation`) " +
"VALUES (?, ?, ?, ?, ?, ?, FROM_UNIXTIME(? * 0.001), FROM_UNIXTIME(? * 0.001), ?)",

""
),
SELECT_EXACT_PUNISHMENT(
"SELECT * FROM `Punishments` WHERE `uuid` = ? AND `start` = FROM_UNIXTIME(? * 0.001)",
"SELECT * FROM Punishments WHERE uuid = ? AND start = ?"
Expand Down Expand Up @@ -212,6 +226,7 @@ public enum SQLQuery {
"ALTER TABLE `PunishmentHistory` " +
"DROP `start_old`, " +
"DROP `end_old`;",

""
);

Expand All @@ -230,4 +245,12 @@ public enum SQLQuery {
public String toString() {
return DatabaseManager.get().isUseMySQL() ? mysql : hsqldb;
}

public String getMysql() {
return mysql;
}

public String getHsqldb() {
return hsqldb;
}
}

0 comments on commit 0229464

Please sign in to comment.