From 64c46e6be94ecbe493cda75da5f7ce658d70b270 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Mon, 5 Feb 2024 11:38:10 -0500 Subject: [PATCH] Added connection test when creating any data source now. --- .../perfin/data/impl/JdbcDataSourceFactory.java | 6 +++++- .../andrewlalis/perfin/model/ProfileLoader.java | 16 +++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/andrewlalis/perfin/data/impl/JdbcDataSourceFactory.java b/src/main/java/com/andrewlalis/perfin/data/impl/JdbcDataSourceFactory.java index bdad590..0727cbf 100644 --- a/src/main/java/com/andrewlalis/perfin/data/impl/JdbcDataSourceFactory.java +++ b/src/main/java/com/andrewlalis/perfin/data/impl/JdbcDataSourceFactory.java @@ -59,7 +59,11 @@ public class JdbcDataSourceFactory implements DataSourceFactory { throw new ProfileLoadException("Profile " + profileName + " has a database with an unsupported schema version."); } } - return new JdbcDataSource(getJdbcUrl(profileName), Profile.getContentDir(profileName)); + var dataSource = new JdbcDataSource(getJdbcUrl(profileName), Profile.getContentDir(profileName)); + if (!testConnection(dataSource)) { + throw new ProfileLoadException("Unabled to connect to the profile's database."); + } + return dataSource; } public SchemaStatus getSchemaStatus(String profileName) throws IOException { diff --git a/src/main/java/com/andrewlalis/perfin/model/ProfileLoader.java b/src/main/java/com/andrewlalis/perfin/model/ProfileLoader.java index 90cb815..8297cf4 100644 --- a/src/main/java/com/andrewlalis/perfin/model/ProfileLoader.java +++ b/src/main/java/com/andrewlalis/perfin/model/ProfileLoader.java @@ -14,6 +14,7 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.time.LocalDateTime; import java.util.Collections; import java.util.List; import java.util.Properties; @@ -50,6 +51,8 @@ public class ProfileLoader { } catch (IOException e) { throw new ProfileLoadException("Failed to load profile settings.", e); } + + // Try to check the profile's schema version and migrate if needed. try { DataSourceFactory.SchemaStatus status = dataSourceFactory.getSchemaStatus(name); if (status == DataSourceFactory.SchemaStatus.NEEDS_MIGRATION) { @@ -72,11 +75,14 @@ public class ProfileLoader { } // Check for a recent backup and make one if not present. - try { - ProfileBackups.makeBackup(name); - ProfileBackups.cleanOldBackups(name); - } catch (IOException e) { - log.error("Failed to create backup for profile " + name + ".", e); + LocalDateTime lastBackup = ProfileBackups.getLastBackupTimestamp(name); + if (lastBackup == null || lastBackup.isBefore(LocalDateTime.now().minusDays(1))) { + try { + ProfileBackups.makeBackup(name); + ProfileBackups.cleanOldBackups(name); + } catch (IOException e) { + log.error("Failed to create backup for profile " + name + ".", e); + } } DataSource dataSource = dataSourceFactory.getDataSource(name);