Skip to content

Commit 7a528df

Browse files
committed
Fix issue with adding new column to SQL
1 parent 891812b commit 7a528df

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>simplexity</groupId>
88
<artifactId>SimplePMs</artifactId>
9-
<version>2.4.1</version>
9+
<version>2.4.2</version>
1010
<packaging>jar</packaging>
1111

1212
<name>SimplePMs</name>

src/main/java/simplexity/simplepms/saving/SqlHandler.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ player_uuid VARCHAR (36) NOT NULL PRIMARY KEY,
6161
messages_disabled BOOLEAN NOT NULL
6262
);""");
6363
playerSettingsInitStatement.execute();
64-
updateDatabaseColumns();
64+
updateDatabaseColumns().join();
6565
} catch (SQLException e) {
6666
logger.warn("Failed to connect to database: {}", e.getMessage(), e);
6767
}
@@ -169,20 +169,26 @@ public void updateSettings(UUID playerUUID, boolean socialSpyEnabled, boolean me
169169

170170
}
171171

172-
private void updateDatabaseColumns() {
173-
if (ConfigHandler.getInstance().isMysqlEnabled()) {
174-
doesMysqlColumnExist("blocklist", "blocked_player_name").thenAccept(exists -> {
175-
if (!exists) {
176-
addColumn("blocklist", "blocked_player_name", "VARCHAR(256)", "");
172+
private CompletableFuture<Boolean> updateDatabaseColumns() {
173+
return CompletableFuture.supplyAsync(() -> {
174+
String tableName = "blocklist";
175+
String columnName = "blocked_player_name";
176+
boolean columnExists;
177+
try {
178+
if (ConfigHandler.getInstance().isMysqlEnabled()) {
179+
columnExists = doesMysqlColumnExist(tableName, columnName).get();
180+
} else {
181+
columnExists = doesSqliteColumnExist(tableName, columnName).get();
177182
}
178-
});
179-
} else {
180-
doesSqliteColumnExist("blocklist", "blocked_player_name").thenAccept(exists -> {
181-
if (!exists) {
182-
addColumn("blocklist", "blocked_player_name", "VARCHAR(256)", "");
183+
if (!columnExists) {
184+
addColumn(tableName, columnName, "VARCHAR(256)", "");
183185
}
184-
});
185-
}
186+
} catch (Exception e) {
187+
logger.warn("Unable to update database table: {} column: {}, error: {}", tableName, columnName, e.getMessage(), e);
188+
return false;
189+
}
190+
return true;
191+
});
186192
}
187193

188194
private CompletableFuture<Boolean> doesSqliteColumnExist(String tableName, String columnName) {
@@ -219,7 +225,7 @@ private CompletableFuture<Boolean> doesMysqlColumnExist(String tableName, String
219225
// Possibly extremely cursed way to do this :cackle:
220226

221227
private void addColumn(String tableName, String columnName, String dataType, String constraints) {
222-
String query = "ALTER TABLE " + tableName + " ADD COLUMN " + columnName + dataType + constraints + ";";
228+
String query = String.format("ALTER TABLE %s ADD COLUMN %s %s %s;", tableName, columnName, dataType, constraints);
223229
try (Connection connection = getConnection()) {
224230
PreparedStatement statement = connection.prepareStatement(query);
225231
statement.executeUpdate();

0 commit comments

Comments
 (0)