mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-04 06:28:49 +00:00
Fix database migration not working, display "N/A" if timestamp is not available
This commit is contained in:
1 parent
1d2c3c6d36
commit
55321fde1f
2 files changed
+58
-20
No files matched your search
@@ -12,31 +12,66 @@ public class UpgradeDatabase {
|
|||||||
|
|
||||||
private static final HuskHomes plugin = HuskHomes.getInstance();
|
private static final HuskHomes plugin = HuskHomes.getInstance();
|
||||||
|
|
||||||
//todo this stuff doesn't work. TEST BEFORE LAUNCH
|
private static final String[] mySQLUpgradeStatements = {
|
||||||
|
"ALTER TABLE " + HuskHomes.getSettings().getPlayerDataTable()
|
||||||
|
+ " ADD `is_ignoring_requests` boolean NOT NULL DEFAULT 0, "
|
||||||
|
+ "ADD `offline_location_id` integer NULL DEFAULT NULL, "
|
||||||
|
+ "ADD FOREIGN KEY (`offline_location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION;"};
|
||||||
|
|
||||||
|
private static final String[] SQLiteUpgradeStatements = {
|
||||||
|
"ALTER TABLE " + HuskHomes.getSettings().getPlayerDataTable() + " ADD `is_ignoring_requests` boolean NOT NULL DEFAULT 0;",
|
||||||
|
|
||||||
|
"ALTER TABLE " + HuskHomes.getSettings().getPlayerDataTable() + " ADD `offline_location_id` integer NULL DEFAULT NULL;",
|
||||||
|
|
||||||
|
"CREATE TABLE " + HuskHomes.getSettings().getPlayerDataTable() + "_dg_tmp (" +
|
||||||
|
"`player_id` integer NOT NULL PRIMARY KEY," +
|
||||||
|
"`user_uuid` char(36) NOT NULL UNIQUE," +
|
||||||
|
"`username` varchar(16) NOT NULL," +
|
||||||
|
"`home_slots` integer NOT NULL," +
|
||||||
|
"`rtp_cooldown` integer NOT NULL," +
|
||||||
|
"`is_teleporting` boolean NOT NULL," +
|
||||||
|
"`dest_location_id` integer REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " ON DELETE SET NULL," +
|
||||||
|
"`last_location_id` integer REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " ON DELETE SET NULL," +
|
||||||
|
"`offline_location_id` integer DEFAULT NULL CONSTRAINT " + HuskHomes.getSettings().getPlayerDataTable() + "_" + HuskHomes.getSettings().getLocationsDataTable() + "_location_id_fk REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + "," +
|
||||||
|
"`is_ignoring_requests` boolean DEFAULT false NOT NULL" +
|
||||||
|
");",
|
||||||
|
|
||||||
|
"INSERT INTO " + HuskHomes.getSettings().getPlayerDataTable() + "_dg_tmp(player_id, user_uuid, username, home_slots, rtp_cooldown, is_teleporting, dest_location_id, last_location_id, offline_location_id, is_ignoring_requests) select player_id, user_uuid, username, home_slots, rtp_cooldown, is_teleporting, dest_location_id, last_location_id, offline_location_id, is_ignoring_requests FROM " + HuskHomes.getSettings().getPlayerDataTable() + ";",
|
||||||
|
|
||||||
|
"DROP TABLE " + HuskHomes.getSettings().getPlayerDataTable() + ";",
|
||||||
|
|
||||||
|
"ALTER TABLE " + HuskHomes.getSettings().getPlayerDataTable() + "_dg_tmp RENAME TO " + HuskHomes.getSettings().getPlayerDataTable() + ";"
|
||||||
|
};
|
||||||
|
|
||||||
// Upgrade the database system if the config file version is not high enough
|
// Upgrade the database system if the config file version is not high enough
|
||||||
public static void upgradeDatabase() {
|
public static void upgradeDatabase() {
|
||||||
plugin.reloadConfig();
|
plugin.reloadConfig();
|
||||||
if (plugin.getConfig().getInt("config_file_version", 1) <= 6) {
|
if (plugin.getConfig().getInt("config_file_version", 1) <= 6) {
|
||||||
plugin.getLogger().info("Detected that the database might need updating. Running database upgrade...");
|
plugin.getLogger().info("Database upgrade needed: Adding logout position tracking and ignoring request data...");
|
||||||
try (PreparedStatement tableUpdateStatement = HuskHomes.getConnection().prepareStatement(
|
String[] statements = SQLiteUpgradeStatements;
|
||||||
"ALTER TABLE " + HuskHomes.getSettings().getPlayerDataTable()
|
if (HuskHomes.getSettings().getDatabaseType().equalsIgnoreCase("mysql")) {
|
||||||
+ " ADD `is_ignoring_requests` boolean NOT NULL DEFAULT 0, "
|
statements = mySQLUpgradeStatements;
|
||||||
+ "ADD `offline_location_id` integer NULL DEFAULT NULL, "
|
}
|
||||||
+ "ADD FOREIGN KEY (`offline_location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION;")) {
|
int i = 1;
|
||||||
tableUpdateStatement.executeUpdate();
|
for (String statement : statements) {
|
||||||
plugin.getLogger().info("Database update complete!");
|
try (PreparedStatement tableUpdateStatement = HuskHomes.getConnection().prepareStatement(statement)) {
|
||||||
|
tableUpdateStatement.execute();
|
||||||
|
plugin.getLogger().info("Database upgrade in progress... (" + i + "/" + statements.length + ")");
|
||||||
|
i++;
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
plugin.getLogger().info("Skipped performing the database upgrade: " + e.getCause() + ". This might be because another server on your HuskHomes network already carried out the upgrade - in which case you can safely ignore this warning.");
|
plugin.getLogger().info("Skipped performing the database upgrade: " + e.getCause() + ". This might be because another server on your HuskHomes network already carried out the upgrade - in which case you can safely ignore this warning.");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Update the config file version
|
// Update the config file version
|
||||||
|
plugin.getLogger().info("Logout position and ignoring request database upgrade complete! (v7)");
|
||||||
plugin.getConfig().set("config_file_version", 7);
|
plugin.getConfig().set("config_file_version", 7);
|
||||||
plugin.saveConfig();
|
plugin.saveConfig();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (plugin.getConfig().getInt("config_file_version", 1) <= 7) {
|
if (plugin.getConfig().getInt("config_file_version", 1) <= 7) {
|
||||||
plugin.getLogger().info("Detected that the database might need updating. Running database upgrade...");
|
plugin.getLogger().info("Database upgrade needed: Adding creation timestamps to homes and warps...");
|
||||||
try (PreparedStatement tableUpdateStatement = HuskHomes.getConnection().prepareStatement(
|
try (PreparedStatement tableUpdateStatement = HuskHomes.getConnection().prepareStatement(
|
||||||
"ALTER TABLE " + HuskHomes.getSettings().getHomesDataTable()
|
"ALTER TABLE " + HuskHomes.getSettings().getHomesDataTable()
|
||||||
+ " ADD `creation_time` timestamp NOT NULL DEFAULT 0;")) {
|
+ " ADD `creation_time` timestamp NOT NULL DEFAULT 0;")) {
|
||||||
@@ -45,31 +80,31 @@ public class UpgradeDatabase {
|
|||||||
"UPDATE " + HuskHomes.getSettings().getHomesDataTable() + " SET `creation_time`=CURRENT_TIMESTAMP;")) {
|
"UPDATE " + HuskHomes.getSettings().getHomesDataTable() + " SET `creation_time`=CURRENT_TIMESTAMP;")) {
|
||||||
timeSettingStatement.executeUpdate();
|
timeSettingStatement.executeUpdate();
|
||||||
}
|
}
|
||||||
plugin.getLogger().info("Database update complete!");
|
plugin.getLogger().info("Database upgrade in progress... (1/2)");
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
plugin.getLogger().info("Skipped performing the database upgrade: " + e.getCause() + ". This might be because another server on your HuskHomes network already carried out the upgrade - in which case you can safely ignore this warning.");
|
plugin.getLogger().info("Skipped performing the database upgrade: " + e.getCause() + ". This might be because another server on your HuskHomes network already carried out the upgrade - in which case you can safely ignore this warning.");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
}
|
||||||
try (PreparedStatement tableUpdateStatement = HuskHomes.getConnection().prepareStatement(
|
try (PreparedStatement tableUpdateStatement = HuskHomes.getConnection().prepareStatement(
|
||||||
"ALTER TABLE " + HuskHomes.getSettings().getWarpsDataTable()
|
"ALTER TABLE " + HuskHomes.getSettings().getWarpsDataTable()
|
||||||
+ " ADD `creation_time` timestamp NOT NULL DEFAULT 0;")) {
|
+ " ADD `creation_time` timestamp NOT NULL DEFAULT 0;")) {
|
||||||
tableUpdateStatement.execute();
|
tableUpdateStatement.execute();
|
||||||
try (PreparedStatement timeSettingStatement = HuskHomes.getConnection().prepareStatement(
|
try (PreparedStatement timeSettingStatement = HuskHomes.getConnection().prepareStatement(
|
||||||
"UPDATE " + HuskHomes.getSettings().getWarpsDataTable() + " SET `creation_time`=CURRENT_TIMESTAMP;")) {
|
"UPDATE " + HuskHomes.getSettings().getHomesDataTable() + " SET `creation_time`=CURRENT_TIMESTAMP;")) {
|
||||||
timeSettingStatement.executeUpdate();
|
timeSettingStatement.executeUpdate();
|
||||||
}
|
}
|
||||||
plugin.getLogger().info("Database update complete!");
|
plugin.getLogger().info("Database upgrade in progress... (2/2)");
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
plugin.getLogger().info("Skipped performing the database upgrade: " + e.getCause() + ". This might be because another server on your HuskHomes network already carried out the upgrade - in which case you can safely ignore this warning.");
|
plugin.getLogger().info("Skipped performing the database upgrade: " + e.getCause() + ". This might be because another server on your HuskHomes network already carried out the upgrade - in which case you can safely ignore this warning.");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
}
|
||||||
|
plugin.getLogger().info("Creation timestamp upgrade complete! (v8)");
|
||||||
|
|
||||||
// Update the config file version
|
// Update the config file version
|
||||||
|
plugin.getLogger().info("All database upgrades have been completed!");
|
||||||
plugin.getConfig().set("config_file_version", 8);
|
plugin.getConfig().set("config_file_version", 8);
|
||||||
plugin.saveConfig();
|
plugin.saveConfig();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -71,6 +71,9 @@ public class SetPoint extends TeleportationPoint {
|
|||||||
* @return The formatted creation time string
|
* @return The formatted creation time string
|
||||||
*/
|
*/
|
||||||
public String getFormattedCreationTime() {
|
public String getFormattedCreationTime() {
|
||||||
|
if (creationTime == 0) {
|
||||||
|
return "N/A";
|
||||||
|
}
|
||||||
return DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT)
|
return DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT)
|
||||||
.withLocale(Locale.getDefault())
|
.withLocale(Locale.getDefault())
|
||||||
.withZone(ZoneId.systemDefault())
|
.withZone(ZoneId.systemDefault())
|
||||||
|
|||||||
Reference in new issue
Block a user