From 788e043269245813dd517787345f80e86b4f447f Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Thu, 18 Jan 2024 11:03:15 -0500 Subject: [PATCH] Added more popups for user when opening a profile that requires migration. --- .../perfin/data/DataSourceFactory.java | 2 ++ .../perfin/data/impl/JdbcDataSourceFactory.java | 2 +- .../perfin/data/impl/migration/Migrations.java | 10 ++++++++++ .../andrewlalis/perfin/model/ProfileLoader.java | 16 +++++++++++++--- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/andrewlalis/perfin/data/DataSourceFactory.java b/src/main/java/com/andrewlalis/perfin/data/DataSourceFactory.java index 44c5d3f..5fbc7d8 100644 --- a/src/main/java/com/andrewlalis/perfin/data/DataSourceFactory.java +++ b/src/main/java/com/andrewlalis/perfin/data/DataSourceFactory.java @@ -16,4 +16,6 @@ public interface DataSourceFactory { INCOMPATIBLE } SchemaStatus getSchemaStatus(String profileName) throws IOException; + + int getSchemaVersion(String profileName) throws IOException; } 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 77ed7ab..59819b7 100644 --- a/src/main/java/com/andrewlalis/perfin/data/impl/JdbcDataSourceFactory.java +++ b/src/main/java/com/andrewlalis/perfin/data/impl/JdbcDataSourceFactory.java @@ -176,7 +176,7 @@ public class JdbcDataSourceFactory implements DataSourceFactory { return Profile.getDir(profileName).resolve(".jdbc-schema-version.txt"); } - private static int getSchemaVersion(String profileName) throws IOException { + public int getSchemaVersion(String profileName) throws IOException { if (Files.exists(getSchemaVersionFile(profileName))) { try { return Integer.parseInt(Files.readString(getSchemaVersionFile(profileName)).strip()); diff --git a/src/main/java/com/andrewlalis/perfin/data/impl/migration/Migrations.java b/src/main/java/com/andrewlalis/perfin/data/impl/migration/Migrations.java index bd8122d..6e80290 100644 --- a/src/main/java/com/andrewlalis/perfin/data/impl/migration/Migrations.java +++ b/src/main/java/com/andrewlalis/perfin/data/impl/migration/Migrations.java @@ -34,4 +34,14 @@ public class Migrations { } return selectedMigration; } + + public static Map getSchemaVersionCompatibility() { + final Map compatibilities = new HashMap<>(); + compatibilities.put(1, "1.4.0"); + return compatibilities; + } + + public static String getLatestCompatibleVersion(int schemaVersion) { + return getSchemaVersionCompatibility().get(schemaVersion); + } } diff --git a/src/main/java/com/andrewlalis/perfin/model/ProfileLoader.java b/src/main/java/com/andrewlalis/perfin/model/ProfileLoader.java index 4b58b2d..b1f6a4e 100644 --- a/src/main/java/com/andrewlalis/perfin/model/ProfileLoader.java +++ b/src/main/java/com/andrewlalis/perfin/model/ProfileLoader.java @@ -4,6 +4,7 @@ import com.andrewlalis.perfin.PerfinApp; import com.andrewlalis.perfin.control.Popups; import com.andrewlalis.perfin.data.DataSourceFactory; import com.andrewlalis.perfin.data.ProfileLoadException; +import com.andrewlalis.perfin.data.impl.migration.Migrations; import com.andrewlalis.perfin.data.util.FileUtil; import javafx.stage.Window; import org.slf4j.Logger; @@ -18,6 +19,10 @@ import java.util.Properties; import static com.andrewlalis.perfin.data.util.FileUtil.copyResourceFile; +/** + * Component responsible for loading a profile from storage, as well as some + * other basic tasks concerning the set of stored profiles. + */ public class ProfileLoader { private static final Logger log = LoggerFactory.getLogger(ProfileLoader.class); @@ -49,16 +54,21 @@ public class ProfileLoader { if (status == DataSourceFactory.SchemaStatus.NEEDS_MIGRATION) { boolean confirm = Popups.confirm(window, "The profile \"" + name + "\" has an outdated data schema and needs to be migrated to the latest version. Is this okay?"); if (!confirm) { - throw new ProfileLoadException("User rejected migration."); + int existingSchemaVersion = dataSourceFactory.getSchemaVersion(name); + String compatibleVersion = Migrations.getLatestCompatibleVersion(existingSchemaVersion); + Popups.message( + window, + "The profile \"" + name + "\" is using schema version " + existingSchemaVersion + ", which is compatible with Perfin version " + compatibleVersion + ". Consider downgrading Perfin to access this profile safely." + ); + throw new ProfileLoadException("User rejected the migration."); } } else if (status == DataSourceFactory.SchemaStatus.INCOMPATIBLE) { - Popups.error(window, "The profile \"" + name + "\" has a data schema that's incompatible with this app."); + Popups.error(window, "The profile \"" + name + "\" has a data schema that's incompatible with this app. Update Perfin to access this profile safely."); throw new ProfileLoadException("Incompatible schema version."); } } catch (IOException e) { throw new ProfileLoadException("Failed to get profile's schema status.", e); } - Popups.message(window, "Test!"); return new Profile(name, settings, dataSourceFactory.getDataSource(name)); }