Skip to content

Commit

Permalink
Implement MigrationService
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alvasw committed Oct 20, 2024
1 parent 246535d commit 31167e0
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Boolean> 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);
}
}
}
32 changes: 32 additions & 0 deletions application/src/main/java/bisq/application/migration/Migrator.java
Original file line number Diff line number Diff line change
@@ -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<Migration> 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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 31167e0

Please sign in to comment.