Skip to content

Commit

Permalink
added migration to new db schema (firstJoin column)
Browse files Browse the repository at this point in the history
  • Loading branch information
KartoffelChipss committed Dec 15, 2024
1 parent 9121b50 commit 16222e4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.strassburger</groupId>
<artifactId>lifestealz</artifactId>
<version>2.8.0</version>
<version>2.8.1</version>
<packaging>jar</packaging>

<name>LifeStealZ</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public void init() {
if (connection == null) return;
try (Statement statement = connection.createStatement()) {
statement.executeUpdate("CREATE TABLE IF NOT EXISTS hearts (uuid VARCHAR(36) PRIMARY KEY, name VARCHAR(255), maxhp REAL, hasbeenRevived INTEGER, craftedHearts INTEGER, craftedRevives INTEGER, killedOtherPlayers INTEGER, firstJoin INTEGER)");

migrateDatabase(connection);
} catch (SQLException e) {
getPlugin().getLogger().severe("Failed to initialize SQL database: " + e.getMessage());
}
Expand Down Expand Up @@ -196,4 +198,37 @@ public List<String> getPlayerNames() {

return playerNames;
}

private void migrateDatabase(Connection connection) {
try (Statement statement = connection.createStatement()) {
boolean hasFirstJoin = false;
String databaseType = connection.getMetaData().getDatabaseProductName().toLowerCase();

if (databaseType.contains("sqlite")) {
try (ResultSet resultSet = statement.executeQuery("PRAGMA table_info(hearts)")) {
while (resultSet.next()) {
if ("firstJoin".equalsIgnoreCase(resultSet.getString("name"))) {
hasFirstJoin = true;
break;
}
}
}
} else if (databaseType.contains("mysql")) {
try (ResultSet resultSet = statement.executeQuery(
"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'hearts' AND COLUMN_NAME = 'firstJoin'")) {
if (resultSet.next()) {
hasFirstJoin = true;
}
}
}

if (!hasFirstJoin) {
getPlugin().getLogger().info("Adding 'firstJoin' column to 'hearts' table.");
statement.executeUpdate("ALTER TABLE hearts ADD COLUMN firstJoin INTEGER DEFAULT 0");
}

} catch (SQLException e) {
getPlugin().getLogger().severe("Failed to migrate SQL database: " + e.getMessage());
}
}
}

0 comments on commit 16222e4

Please sign in to comment.