Skip to content

Commit

Permalink
Add failsafe backup creator to database upgrader and fix timestamp de…
Browse files Browse the repository at this point in the history
…fault setting issues, Close #87, #86
  • Loading branch information
WiIIiam278 committed Aug 28, 2021
1 parent 7539094 commit d8e2ea7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/java/me/william278/huskhomes2/HuskHomes.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static HuskHomes getInstance() {
public static Connection getConnection() {
return database.getConnection();
}
public static void backupDatabase() { database.backup(); }

// Map integration handling
private static Map map;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/me/william278/huskhomes2/data/SQL/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public Database(HuskHomes instance) {

public abstract void load();

public abstract void backup();

public void initialize() {
Connection connection = getConnection();

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/me/william278/huskhomes2/data/SQL/MySQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,9 @@ public void load() {

initialize();
}

@Override
public void backup() {
plugin.getLogger().info("Remember to make backups of your HuskHomes Database before updating HuskHomes!");
}
}
28 changes: 26 additions & 2 deletions src/main/java/me/william278/huskhomes2/data/SQL/SQLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

import me.william278.huskhomes2.HuskHomes;

import java.io.File;
import java.io.IOException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;
import java.util.logging.Level;

public class SQLite extends Database {
Expand Down Expand Up @@ -119,4 +125,22 @@ public void load() {

initialize();
}

@Override
public void backup() {
final String BACKUPS_FOLDER_NAME = "database-backups";
final String backupFileName = DATABASE_NAME + "Backup_" + DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss-SS")
.withLocale(Locale.getDefault())
.withZone(ZoneId.systemDefault())
.format(Instant.now()).replaceAll(" ", "-") + ".db";
final File databaseFile = new File(plugin.getDataFolder(), DATABASE_NAME + ".db");
new File(plugin.getDataFolder(), BACKUPS_FOLDER_NAME).mkdirs();
final File backUpFile = new File(plugin.getDataFolder(), BACKUPS_FOLDER_NAME + File.separator + backupFileName);
try {
Files.copy(databaseFile.toPath(), backUpFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
plugin.getLogger().info("Created a backup of your database.");
} catch (IOException iox) {
plugin.getLogger().log(Level.WARNING, "An error occurred making a database backup", iox);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static void upgradeDatabase() {
plugin.reloadConfig();
if (plugin.getConfig().getInt("config_file_version", 1) <= 6) {
plugin.getLogger().info("Database upgrade needed: Adding logout position tracking and ignoring request data...");
HuskHomes.backupDatabase(); // Backup database before upgrades are carried out!
String[] statements = SQLiteUpgradeStatements;
if (HuskHomes.getSettings().getDatabaseType().equalsIgnoreCase("mysql")) {
statements = mySQLUpgradeStatements;
Expand All @@ -72,6 +73,7 @@ public static void upgradeDatabase() {

if (plugin.getConfig().getInt("config_file_version", 1) <= 7) {
plugin.getLogger().info("Database upgrade needed: Adding creation timestamps to homes and warps...");
HuskHomes.backupDatabase(); // Backup database before upgrades are carried out!
try (PreparedStatement tableUpdateStatement = HuskHomes.getConnection().prepareStatement(
"ALTER TABLE " + HuskHomes.getSettings().getHomesDataTable()
+ " ADD `creation_time` timestamp NULL DEFAULT NULL;")) {
Expand Down

0 comments on commit d8e2ea7

Please sign in to comment.