From e66d433ffe1a2dc5b6115605ca12ae52746464e5 Mon Sep 17 00:00:00 2001 From: Flibio Date: Thu, 10 May 2018 12:09:27 -0600 Subject: [PATCH] Complete the database fix --- .../economylite/impl/PlayerServiceCommon.java | 56 +++++++++++++------ .../impl/VirtualServiceCommon.java | 42 ++++++++++++++ 2 files changed, 80 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/github/flibio/economylite/impl/PlayerServiceCommon.java b/src/main/java/io/github/flibio/economylite/impl/PlayerServiceCommon.java index 48df71d..f702c20 100644 --- a/src/main/java/io/github/flibio/economylite/impl/PlayerServiceCommon.java +++ b/src/main/java/io/github/flibio/economylite/impl/PlayerServiceCommon.java @@ -39,31 +39,51 @@ public PlayerServiceCommon(SqlManager manager, boolean h2) { if (manager.initialTestConnection()) { manager.executeUpdate("CREATE TABLE IF NOT EXISTS economyliteplayers(uuid VARCHAR(36), balance DECIMAL(11,2), currency VARCHAR(1024))"); } - repair(); + repair(h2); // Create caches balCache = CacheManager.create(logger, 64, 360); exCache = CacheManager.create(logger, 128, 360); topCache = CacheManager.create(logger, 16, 30); } - public boolean repair() { - ResultSet rs = manager.executeQuery("show index from economyliteplayers where Column_name='uuid'").get(); - try { - rs.next(); - rs.getString(1); - logger.info("Repairing the database..."); - } catch (Exception e) { - logger.debug("Database repairs not necessary!"); - return false; + private void repair(boolean h2) { + if (h2) { + try { + ResultSet rs = manager.executeQuery("SELECT * FROM INFORMATION_SCHEMA.CONSTRAINTS WHERE TABLE_NAME = 'ECONOMYLITEPLAYERS'").get(); + rs.next(); + rs.getString(1); + logger.info("Repairing the database..."); + } catch (Exception e) { + logger.debug("Database repairs not necessary!"); + return; + } + logger.info("Renaming database..."); + manager.executeUpdate("ALTER TABLE economyliteplayers RENAME TO economyliteplayersold"); + logger.info("Recreating database..."); + manager.executeUpdate("CREATE TABLE economyliteplayers AS SELECT * FROM economyliteplayersold"); + logger.info("Dropping database..."); + manager.executeUpdate("DROP TABLE economyliteplayersold"); + logger.info("Repairs complete!"); + return; + } else { + try { + ResultSet rs = manager.executeQuery("show index from economyliteplayers where Column_name='uuid'").get(); + rs.next(); + rs.getString(1); + logger.info("Repairing the database..."); + } catch (Exception e) { + logger.debug("Database repairs not necessary!"); + return; + } + logger.info("Renaming database..."); + manager.executeUpdate("RENAME TABLE economyliteplayers TO economyliteplayersold"); + logger.info("Recreating database..."); + manager.executeUpdate("CREATE TABLE economyliteplayers AS SELECT * FROM economyliteplayersold"); + logger.info("Dropping database..."); + manager.executeUpdate("DROP TABLE economyliteplayersold"); + logger.info("Repairs complete!"); + return; } - logger.info("Renaming database..."); - manager.executeUpdate("RENAME TABLE economyliteplayers TO economyliteplayersold"); - logger.info("Recreating database..."); - manager.executeUpdate("CREATE TABLE economyliteplayers AS SELECT * FROM economyliteplayersold"); - logger.info("Dropping database..."); - manager.executeUpdate("DROP TABLE economyliteplayersold"); - logger.info("Repairs complete!"); - return true; } public boolean isWorking() { diff --git a/src/main/java/io/github/flibio/economylite/impl/VirtualServiceCommon.java b/src/main/java/io/github/flibio/economylite/impl/VirtualServiceCommon.java index bb74dc2..105b91f 100644 --- a/src/main/java/io/github/flibio/economylite/impl/VirtualServiceCommon.java +++ b/src/main/java/io/github/flibio/economylite/impl/VirtualServiceCommon.java @@ -17,6 +17,7 @@ import org.spongepowered.api.service.economy.account.VirtualAccount; import java.math.BigDecimal; +import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -43,12 +44,53 @@ public VirtualServiceCommon(SqlManager manager, boolean h2) { manager.executeUpdate("ALTER TABLE `economylitevirts` CHANGE `id` `id` VARCHAR(1024)"); } } + repair(h2); // Create caches balCache = CacheManager.create(logger, 64, 360); exCache = CacheManager.create(logger, 128, 360); topCache = CacheManager.create(logger, 16, 30); } + private void repair(boolean h2) { + if (h2) { + try { + ResultSet rs = manager.executeQuery("SELECT * FROM INFORMATION_SCHEMA.CONSTRAINTS WHERE TABLE_NAME = 'ECONOMYLITEVIRTS'").get(); + rs.next(); + rs.getString(1); + logger.info("Repairing the database..."); + } catch (Exception e) { + logger.debug("Database repairs not necessary!"); + return; + } + logger.info("Renaming database..."); + manager.executeUpdate("ALTER TABLE economylitevirts RENAME TO economylitevirtsold"); + logger.info("Recreating database..."); + manager.executeUpdate("CREATE TABLE economylitevirts AS SELECT * FROM economylitevirtsold"); + logger.info("Dropping database..."); + manager.executeUpdate("DROP TABLE economylitevirtsold"); + logger.info("Repairs complete!"); + return; + } else { + try { + ResultSet rs = manager.executeQuery("show index from economylitevirts where Column_name='id'").get(); + rs.next(); + rs.getString(1); + logger.info("Repairing the database..."); + } catch (Exception e) { + logger.debug("Database repairs not necessary!"); + return; + } + logger.info("Renaming database..."); + manager.executeUpdate("RENAME TABLE economylitevirts TO economylitevirtsold"); + logger.info("Recreating database..."); + manager.executeUpdate("CREATE TABLE economylitevirts AS SELECT * FROM economylitevirtsold"); + logger.info("Dropping database..."); + manager.executeUpdate("DROP TABLE economylitevirtssold"); + logger.info("Repairs complete!"); + return; + } + } + public boolean isWorking() { return manager.testConnection(); }