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(); +}