Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync auto IDs on every start #219

Merged
merged 7 commits into from
Jun 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/main/java/me/leoko/advancedban/manager/DatabaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public void setup(boolean useMySQLServer) {

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

if (useMySQL) {
syncAutoId();
}
}

public void shutdown() {
Expand Down Expand Up @@ -210,12 +214,36 @@ public boolean isUseMySQL() {
}

private void migrateIfNeccessary(SQLQuery detectionQuery, SQLQuery migrationQuery) throws SQLException {
try(final ResultSet result = executeResultStatement(detectionQuery, dbName)) {
try (final ResultSet result = executeResultStatement(detectionQuery)) {
if (!result.next()) return;

if ("varchar".equalsIgnoreCase(result.getString("DATA_TYPE"))) {
executeMultipleStatements(migrationQuery);
}
}
}

private int getNextAutoId() {
try (final ResultSet result = executeResultStatement(SQLQuery.SELECT_NEXT_AUTO_ID)) {
if (result.next()) {
return result.getInt(1);
}
} catch (SQLException ex) {
Universal.get().log(
"An unexpected error has ocurred while trying to retrieve the highest id\n"
+ "Please check the plugins/AdvancedBan/logs/latest.log file and report this"
+ "error in: https://github.com/DevLeoko/AdvancedBan/issues"
);
Universal.get().debug(ex);
}

return useMySQL ? 1 : 0;
}

private void syncAutoId() {
final int nextId = getNextAutoId();

executeStatement(SQLQuery.SET_PUNISHMENT_AUTO_ID.toString(), false, nextId);
executeStatement(SQLQuery.SET_PUNISHMENT_HISTORY_AUTO_ID.toString(), false, nextId);
}
}
2 changes: 1 addition & 1 deletion src/main/java/me/leoko/advancedban/utils/Punishment.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void create(boolean silent) {
Universal.get().debug(ex);
}
} else {
DatabaseManager.get().executeMultipleStatements(SQLQuery.BUMP_PUNISHMENT_AUTO_INCREMENT);
DatabaseManager.get().executeMultipleStatements(SQLQuery.BUMP_PUNISHMENT_AUTO_ID);
}

final int cWarnings = getType().getBasic() == PunishmentType.WARNING ? (PunishmentManager.get().getCurrentWarns(getUuid()) + 1) : 0;
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/me/leoko/advancedban/utils/SQLQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public enum SQLQuery {
"(name, uuid, reason, operator, punishmentType, start, end, calculation) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
),
BUMP_PUNISHMENT_AUTO_INCREMENT(
BUMP_PUNISHMENT_AUTO_ID(
"INSERT INTO `Punishments` (`id`) VALUES (NULL);\n" +
"DELETE FROM `Punishments` WHERE `id` = LAST_INSERT_ID()",

Expand Down Expand Up @@ -141,13 +141,27 @@ public enum SQLQuery {
"SELECT * FROM `PunishmentHistory` ORDER BY `start` DESC LIMIT ?",
"SELECT * FROM PunishmentHistory ORDER BY start DESC LIMIT ?"
),


SELECT_NEXT_AUTO_ID(
"SELECT `AUTO_INCREMENT` FROM `information_schema`.`TABLES` " +
"WHERE `TABLE_SCHEMA` = DATABASE() AND (`TABLE_NAME` = 'Punishments' OR `TABLE_NAME` = 'PunishmentHistory') ORDER BY `AUTO_INCREMENT` DESC LIMIT 1",
""
),
SET_PUNISHMENT_AUTO_ID(
"ALTER TABLE `Punishments` AUTO_INCREMENT = ?",
""
),
SET_PUNISHMENT_HISTORY_AUTO_ID(
"ALTER TABLE `PunishmentHistory` AUTO_INCREMENT = ?",
""
),

DETECT_PUNISHMENT_MIGRATION_STATUS(
"SELECT `DATA_TYPE` FROM `information_schema`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = 'Punishments' AND `COLUMN_NAME` = 'punishmentType'",
"SELECT `DATA_TYPE` FROM `information_schema`.`COLUMNS` WHERE `TABLE_SCHEMA` = DATABASE() AND `TABLE_NAME` = 'Punishments' AND `COLUMN_NAME` = 'punishmentType'",
""
),
DETECT_PUNISHMENT_HISTORY_MIGRATION_STATUS(
"SELECT `DATA_TYPE` FROM `information_schema`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = 'PunishmentHistory' AND `COLUMN_NAME` = 'punishmentType'",
"SELECT `DATA_TYPE` FROM `information_schema`.`COLUMNS` WHERE `TABLE_SCHEMA` = DATABASE() AND `TABLE_NAME` = 'PunishmentHistory' AND `COLUMN_NAME` = 'punishmentType'",
""
),

Expand Down