Added more popups for user when opening a profile that requires migration.

This commit is contained in:
Andrew Lalis 2024-01-18 11:03:15 -05:00
parent da589807ef
commit 788e043269
4 changed files with 26 additions and 4 deletions

View File

@ -16,4 +16,6 @@ public interface DataSourceFactory {
INCOMPATIBLE INCOMPATIBLE
} }
SchemaStatus getSchemaStatus(String profileName) throws IOException; SchemaStatus getSchemaStatus(String profileName) throws IOException;
int getSchemaVersion(String profileName) throws IOException;
} }

View File

@ -176,7 +176,7 @@ public class JdbcDataSourceFactory implements DataSourceFactory {
return Profile.getDir(profileName).resolve(".jdbc-schema-version.txt"); 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))) { if (Files.exists(getSchemaVersionFile(profileName))) {
try { try {
return Integer.parseInt(Files.readString(getSchemaVersionFile(profileName)).strip()); return Integer.parseInt(Files.readString(getSchemaVersionFile(profileName)).strip());

View File

@ -34,4 +34,14 @@ public class Migrations {
} }
return selectedMigration; return selectedMigration;
} }
public static Map<Integer, String> getSchemaVersionCompatibility() {
final Map<Integer, String> compatibilities = new HashMap<>();
compatibilities.put(1, "1.4.0");
return compatibilities;
}
public static String getLatestCompatibleVersion(int schemaVersion) {
return getSchemaVersionCompatibility().get(schemaVersion);
}
} }

View File

@ -4,6 +4,7 @@ import com.andrewlalis.perfin.PerfinApp;
import com.andrewlalis.perfin.control.Popups; import com.andrewlalis.perfin.control.Popups;
import com.andrewlalis.perfin.data.DataSourceFactory; import com.andrewlalis.perfin.data.DataSourceFactory;
import com.andrewlalis.perfin.data.ProfileLoadException; import com.andrewlalis.perfin.data.ProfileLoadException;
import com.andrewlalis.perfin.data.impl.migration.Migrations;
import com.andrewlalis.perfin.data.util.FileUtil; import com.andrewlalis.perfin.data.util.FileUtil;
import javafx.stage.Window; import javafx.stage.Window;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -18,6 +19,10 @@ import java.util.Properties;
import static com.andrewlalis.perfin.data.util.FileUtil.copyResourceFile; 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 { public class ProfileLoader {
private static final Logger log = LoggerFactory.getLogger(ProfileLoader.class); private static final Logger log = LoggerFactory.getLogger(ProfileLoader.class);
@ -49,16 +54,21 @@ public class ProfileLoader {
if (status == DataSourceFactory.SchemaStatus.NEEDS_MIGRATION) { 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?"); 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) { 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) { } 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."); throw new ProfileLoadException("Incompatible schema version.");
} }
} catch (IOException e) { } catch (IOException e) {
throw new ProfileLoadException("Failed to get profile's schema status.", e); throw new ProfileLoadException("Failed to get profile's schema status.", e);
} }
Popups.message(window, "Test!");
return new Profile(name, settings, dataSourceFactory.getDataSource(name)); return new Profile(name, settings, dataSourceFactory.getDataSource(name));
} }