forked from bisq-network/bisq2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
application/src/main/java/bisq/application/migration/MigrationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
application/src/main/java/bisq/application/migration/Migrator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
application/src/main/java/bisq/application/migration/migrations/Migration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |