From 31167e00930479788145dec50c372c2c10881683 Mon Sep 17 00:00:00 2001 From: Alva Swanson Date: Sun, 20 Oct 2024 19:46:09 +0000 Subject: [PATCH] Implement MigrationService The MigrationService allows us to perform necessary migrations when upgrading to a new Bisq version. So far, the only option is to try to perform the migrations on each run. The MigrationService runs the necessary migrations if the data directory version is below the app version. --- .../migration/MigrationService.java | 46 +++++++++++++++++++ .../bisq/application/migration/Migrator.java | 32 +++++++++++++ .../migration/migrations/Migration.java | 11 +++++ 3 files changed, 89 insertions(+) create mode 100644 application/src/main/java/bisq/application/migration/MigrationService.java create mode 100644 application/src/main/java/bisq/application/migration/Migrator.java create mode 100644 application/src/main/java/bisq/application/migration/migrations/Migration.java diff --git a/application/src/main/java/bisq/application/migration/MigrationService.java b/application/src/main/java/bisq/application/migration/MigrationService.java new file mode 100644 index 0000000000..4f39f0e3c9 --- /dev/null +++ b/application/src/main/java/bisq/application/migration/MigrationService.java @@ -0,0 +1,46 @@ +package bisq.application.migration; + +import bisq.common.application.ApplicationVersion; +import bisq.common.platform.Version; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.concurrent.CompletableFuture; + +public class MigrationService { + static final Version VERSION_BEFORE_MIGRATION_SERVICE_INTRODUCED = new Version("2.1.1"); + private final Path dataDir; + private final File dataDirVersionFile; + + public MigrationService(Path dataDir) { + this.dataDir = dataDir; + this.dataDirVersionFile = dataDir.resolve("version").toFile(); + } + + public CompletableFuture runMigrations() { + Version dataDirVersion = getDataDirVersion(); + Version appVersion = ApplicationVersion.getVersion(); + + if (dataDirVersion.below(appVersion)) { + Migrator migrator = new Migrator(appVersion, dataDir); + migrator.migrate(); + } + + return CompletableFuture.completedFuture(true); + } + + Version getDataDirVersion() { + if (!dataDirVersionFile.exists()) { + return VERSION_BEFORE_MIGRATION_SERVICE_INTRODUCED; + } + + try { + String version = Files.readString(dataDirVersionFile.toPath()); + return new Version(version); + } catch (IOException e) { + throw new RuntimeException("Can't identify data dir version. This shouldn't happen.", e); + } + } +} diff --git a/application/src/main/java/bisq/application/migration/Migrator.java b/application/src/main/java/bisq/application/migration/Migrator.java new file mode 100644 index 0000000000..671c9e4dc0 --- /dev/null +++ b/application/src/main/java/bisq/application/migration/Migrator.java @@ -0,0 +1,32 @@ +package bisq.application.migration; + +import bisq.application.migration.migrations.Migration; +import bisq.common.platform.Version; +import lombok.extern.slf4j.Slf4j; + +import java.nio.file.Path; +import java.util.Collections; +import java.util.List; + +@Slf4j +public class Migrator { + private final Version appVersion; + private final Path dataDir; + private final List allMigrations; + + public Migrator(Version appVersion, Path dataDir) { + this.appVersion = appVersion; + this.dataDir = dataDir; + this.allMigrations = Collections.emptyList(); + } + + public void migrate() { + for (Migration migration : allMigrations) { + Version migrationVersion = migration.getVersion(); + if (migrationVersion.belowOrEqual(appVersion)) { + log.info("Running {} migrations.", migrationVersion); + migration.run(dataDir); + } + } + } +} diff --git a/application/src/main/java/bisq/application/migration/migrations/Migration.java b/application/src/main/java/bisq/application/migration/migrations/Migration.java new file mode 100644 index 0000000000..bfdf3bbb45 --- /dev/null +++ b/application/src/main/java/bisq/application/migration/migrations/Migration.java @@ -0,0 +1,11 @@ +package bisq.application.migration.migrations; + +import bisq.common.platform.Version; + +import java.nio.file.Path; + +public interface Migration { + void run(Path dataDir); + + Version getVersion(); +}