Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
Complete the database fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Flibio committed May 10, 2018
1 parent 68628ae commit e66d433
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down

0 comments on commit e66d433

Please sign in to comment.