From 16222e47f508821daa08fe4eab1eb7510e14064f Mon Sep 17 00:00:00 2001 From: KartoffelChips Date: Sun, 15 Dec 2024 12:07:05 +0100 Subject: [PATCH] added migration to new db schema (firstJoin column) --- pom.xml | 2 +- .../lifestealz/util/storage/SQLStorage.java | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index bdcd411..59122db 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.strassburger lifestealz - 2.8.0 + 2.8.1 jar LifeStealZ diff --git a/src/main/java/org/strassburger/lifestealz/util/storage/SQLStorage.java b/src/main/java/org/strassburger/lifestealz/util/storage/SQLStorage.java index b24915f..8755c54 100644 --- a/src/main/java/org/strassburger/lifestealz/util/storage/SQLStorage.java +++ b/src/main/java/org/strassburger/lifestealz/util/storage/SQLStorage.java @@ -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()); } @@ -196,4 +198,37 @@ public List 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()); + } + } }