Skip to content

Commit 4a90b1d

Browse files
committed
yes this is definitely a normal way to have settings migrate mhm
1 parent 26329f2 commit 4a90b1d

1 file changed

Lines changed: 64 additions & 1 deletion

File tree

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

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import simplexity.simplepms.saving.objects.PlayerSettings;
1212

1313
import java.sql.Connection;
14+
import java.sql.DatabaseMetaData;
1415
import java.sql.PreparedStatement;
1516
import java.sql.ResultSet;
1617
import java.sql.SQLException;
@@ -19,7 +20,7 @@
1920
import java.util.UUID;
2021
import java.util.concurrent.CompletableFuture;
2122

22-
@SuppressWarnings({"CallToPrintStackTrace", "SqlResolve", "StringTemplateMigration"})
23+
@SuppressWarnings({"CallToPrintStackTrace", "SqlResolve", "StringTemplateMigration", "SameParameterValue"})
2324
public class SqlHandler {
2425

2526
private SqlHandler() {
@@ -60,6 +61,7 @@ player_uuid VARCHAR (36) NOT NULL PRIMARY KEY,
6061
messages_disabled BOOLEAN NOT NULL
6162
);""");
6263
playerSettingsInitStatement.execute();
64+
updateDatabaseColumns();
6365
} catch (SQLException e) {
6466
logger.warn("Failed to connect to database: {}", e.getMessage(), e);
6567
}
@@ -167,6 +169,67 @@ public void updateSettings(UUID playerUUID, boolean socialSpyEnabled, boolean me
167169

168170
}
169171

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)", "");
177+
}
178+
});
179+
} else {
180+
doesSqliteColumnExist("blocklist", "blocked_player_name").thenAccept(exists -> {
181+
if (!exists) {
182+
addColumn("blocklist", "blocked_player_name", "VARCHAR(256)", "");
183+
}
184+
});
185+
}
186+
}
187+
188+
private CompletableFuture<Boolean> doesSqliteColumnExist(String tableName, String columnName) {
189+
return CompletableFuture.supplyAsync(() -> {
190+
String query = "PRAGMA table_info(" + tableName + ")";
191+
try (Connection connection = getConnection()) {
192+
PreparedStatement statement = connection.prepareStatement(query);
193+
ResultSet resultSet = statement.executeQuery();
194+
while (resultSet.next()) {
195+
if (columnName.equalsIgnoreCase(resultSet.getString("name"))) {
196+
return true;
197+
}
198+
}
199+
} catch (SQLException e) {
200+
logger.warn("Failed to to check for column {} in table {}: {}", columnName, tableName, e.getMessage(), e);
201+
}
202+
return false;
203+
});
204+
}
205+
206+
private CompletableFuture<Boolean> doesMysqlColumnExist(String tableName, String columnName) {
207+
return CompletableFuture.supplyAsync(() -> {
208+
try (Connection connection = getConnection()) {
209+
DatabaseMetaData metaData = connection.getMetaData();
210+
ResultSet resultSet = metaData.getColumns(null, null, tableName, columnName);
211+
return resultSet.next();
212+
} catch (SQLException e) {
213+
logger.warn("Failed to check for column {} in table {}: {}", columnName, tableName, e.getMessage(), e);
214+
}
215+
return false;
216+
});
217+
}
218+
219+
// Possibly extremely cursed way to do this :cackle:
220+
221+
private void addColumn(String tableName, String columnName, String dataType, String constraints) {
222+
String query = "ALTER TABLE " + tableName + " ADD COLUMN " + columnName + dataType + constraints + ";";
223+
try (Connection connection = getConnection()) {
224+
PreparedStatement statement = connection.prepareStatement(query);
225+
statement.executeUpdate();
226+
logger.info("Added new column '{}' to table '{}'", columnName, tableName);
227+
} catch (SQLException e) {
228+
logger.warn("Failed to add new column {} to table {}: {}", columnName, tableName, e.getMessage(), e);
229+
}
230+
}
231+
232+
170233
private void setupConfig() {
171234
if (!ConfigHandler.getInstance().isMysqlEnabled()) {
172235
hikariConfig.setJdbcUrl("jdbc:sqlite:" + SimplePMs.getInstance().getDataFolder() + "/simple-pms.db");

0 commit comments

Comments
 (0)