Skip to content

Commit

Permalink
Convert tables from MyISAM to InnoDB
Browse files Browse the repository at this point in the history
Fixed #712
  • Loading branch information
confuser committed Oct 8, 2017
1 parent d0dbdc2 commit cb82202
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/java/me/confuser/banmanager/BanManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import me.confuser.banmanager.runnables.*;
import me.confuser.banmanager.storage.*;
import me.confuser.banmanager.storage.global.*;
import me.confuser.banmanager.storage.mysql.ConvertMyISAMToInnoDb;
import me.confuser.banmanager.storage.mysql.MySQLDatabase;
import me.confuser.banmanager.util.DateUtils;
import me.confuser.banmanager.util.UpdateUtils;
Expand Down Expand Up @@ -337,6 +338,8 @@ private ConnectionSource setupConnection(DatabaseConfig dbConfig, String type) t
@SuppressWarnings("unchecked")
public void setupStorages() throws SQLException {
// TODO Refactor this
new ConvertMyISAMToInnoDb(localConn, getConfiguration().getLocalDb().getTables()); // Convert to InnoDb if MyISAM

playerStorage = new PlayerStorage(localConn);
playerBanStorage = new PlayerBanStorage(localConn);
playerBanRecordStorage = new PlayerBanRecordStorage(localConn);
Expand Down Expand Up @@ -370,6 +373,8 @@ public void setupStorages() throws SQLException {
return;
}

new ConvertMyISAMToInnoDb(globalConn, getConfiguration().getGlobalDb().getTables()); // Convert to InnoDb if MyISAM

globalPlayerBanStorage = new GlobalPlayerBanStorage(globalConn);
globalPlayerBanRecordStorage = new GlobalPlayerBanRecordStorage(globalConn);
globalPlayerMuteStorage = new GlobalPlayerMuteStorage(globalConn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public abstract class DatabaseConfig {
private int maxConnections;
@Getter
private int leakDetection;
@Getter
private HashMap<String, DatabaseTableConfig<?>> tables = new HashMap<>();

private DatabaseConfig(ConfigurationSection conf) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package me.confuser.banmanager.storage.mysql;

import com.j256.ormlite.field.SqlType;
import com.j256.ormlite.stmt.StatementBuilder;
import com.j256.ormlite.support.CompiledStatement;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.support.DatabaseConnection;
import com.j256.ormlite.support.DatabaseResults;
import com.j256.ormlite.table.DatabaseTableConfig;
import me.confuser.banmanager.BanManager;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class ConvertMyISAMToInnoDb {
private BanManager plugin = BanManager.getPlugin();

public ConvertMyISAMToInnoDb(ConnectionSource db, HashMap<String, DatabaseTableConfig<?>> tables) {
DatabaseConnection connection = null;

try {
connection = db.getReadWriteConnection();
} catch (SQLException e) {
e.printStackTrace();
return;
}

for (Map.Entry<String, DatabaseTableConfig<?>> entry : tables.entrySet()) {
final DatabaseResults result;
final String table = entry.getValue().getTableName();

try {
CompiledStatement statement = connection.compileStatement("SHOW TABLE STATUS WHERE Name = ?", StatementBuilder
.StatementType.SELECT, null, DatabaseConnection.DEFAULT_RESULT_FLAGS);

statement.setObject(0, table, SqlType.STRING);

result = statement.runQuery(null);
} catch (SQLException e) {
e.printStackTrace();
continue;
}

try {
while (result.next()) {
final String engine = result.getString(1);

if (engine.equals("MyISAM")) {
plugin.getLogger().info("Converting " + entry.getKey() + " table from MyISAM to InnoDB");

connection.executeStatement("ALTER TABLE " + table + " ENGINE=InnoDB;", DatabaseConnection.DEFAULT_RESULT_FLAGS);
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
result.closeQuietly();
}

}

connection.closeQuietly();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
public class MySQLDatabase extends MysqlDatabaseType {

public MySQLDatabase() {
setCreateTableSuffix("ENGINE=MyISAM DEFAULT CHARSET=utf8");
setCreateTableSuffix("ENGINE=InnoDB DEFAULT CHARSET=utf8");
}
}

0 comments on commit cb82202

Please sign in to comment.