Skip to content

Commit

Permalink
Fix crossbows not getting added to schema for some users
Browse files Browse the repository at this point in the history
  • Loading branch information
nossr50 committed Mar 31, 2024
1 parent b5a50da commit a047bca
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 189 deletions.
1 change: 1 addition & 0 deletions Changelog.txt
@@ -1,5 +1,6 @@
Version 2.2.003
(SQLDB) Fixed a bug where lastlogin was using a value that was too large
(SQLDB) Fixed bug where crossbows was not getting added to SQL schema for some users

Version 2.2.002
Fixed bug where thrown tridents did not grant XP or benefit from subskills
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.gmail.nossr50.mcMMO</groupId>
<artifactId>mcMMO</artifactId>
<version>2.2.003-SNAPSHOT</version>
<version>2.2.003</version>
<name>mcMMO</name>
<url>https://github.com/mcMMO-Dev/mcMMO</url>
<scm>
Expand Down
36 changes: 29 additions & 7 deletions src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java
Expand Up @@ -1034,19 +1034,41 @@ private void checkStructure() {
}

private void updateStructure(String tableName, String columnName, String columnSize) {
try (Connection connection = getConnection(PoolIdentifier.MISC);
Statement createStatement = connection.createStatement()) {

String startingLevel = "'" + mcMMO.p.getAdvancedConfig().getStartingLevel() + "'";
createStatement.executeUpdate("ALTER TABLE `" + tablePrefix + tableName + "` "
+ "ADD COLUMN IF NOT EXISTS `" + columnName + "` int(" + columnSize + ") unsigned NOT NULL DEFAULT " + startingLevel);

try (Connection connection = getConnection(PoolIdentifier.MISC)) {
if (!columnExists(connection, mcMMO.p.getGeneralConfig().getMySQLDatabaseName(), tablePrefix+tableName, columnName)) {
try (Statement createStatement = connection.createStatement()) {
logger.info("[SQLDB Check] Adding column '" + columnName + "' to table '" + tablePrefix + tableName + "'...");
String startingLevel = "'" + mcMMO.p.getAdvancedConfig().getStartingLevel() + "'";
createStatement.executeUpdate("ALTER TABLE `" + tablePrefix + tableName + "` "
+ "ADD COLUMN `" + columnName + "` int(" + columnSize + ") unsigned NOT NULL DEFAULT " + startingLevel);
}
} else {
logger.info("[SQLDB Check] Column '" + columnName + "' already exists in table '" + tablePrefix + tableName + "', looks good!");
}
} catch (SQLException e) {
e.printStackTrace(); // Consider more robust logging
throw new RuntimeException(e);
}
}

private boolean columnExists(Connection connection, String database, String tableName, String columnName) throws SQLException {
logger.info("[SQLDB Check] Checking if column '" + columnName + "' exists in table '" + tableName + "'");
try (Statement createStatement = connection.createStatement()) {
String sql = "SELECT `COLUMN_NAME`\n" +
"FROM `INFORMATION_SCHEMA`.`COLUMNS`\n" +
"WHERE `TABLE_SCHEMA`='" + database + "'\n" +
" AND `TABLE_NAME`='" + tableName + "'\n" +
" AND `COLUMN_NAME`='" + columnName + "'";
var resultSet = createStatement.executeQuery(sql);
return resultSet.next();
} catch (SQLException e) {
logger.info("Failed to check if column exists in table " + tableName + " for column " + columnName);
e.printStackTrace();
throw e;
}
}


private void setStatementQuery(PreparedStatement statement, String tableName) throws SQLException {
if (!this.h2) {
// Set schema name for MySQL
Expand Down

0 comments on commit a047bca

Please sign in to comment.