Skip to content

Commit

Permalink
migration: Write new data directory version if all migrations succeed
Browse files Browse the repository at this point in the history
Migrations are applied if the app version number is higher than the data
directory version number. Currently, the only existing migration writes
the new version number to the data directory after applying its
migration. Moving the data directory version write to the Migrator makes
the code future-proof when we have multiple migrations.
  • Loading branch information
alvasw committed Oct 22, 2024
1 parent 67a9748 commit 59cd878
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package bisq.application.migration;

import bisq.application.migration.migrations.Migration;
import bisq.application.migration.migrations.MigrationFailedException;
import bisq.application.migration.migrations.MigrationsForV2_1_2;
import bisq.common.application.ApplicationVersion;
import bisq.common.platform.Version;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

Expand All @@ -21,11 +25,28 @@ public Migrator(Version appVersion, Path dataDir) {
}

public void migrate() {
boolean allMigrationsSucceeded = true;

for (Migration migration : allMigrations) {
Version migrationVersion = migration.getVersion();
if (migrationVersion.belowOrEqual(appVersion)) {
log.info("Running {} migrations.", migrationVersion);
migration.run(dataDir);

try {
migration.run(dataDir);
} catch (MigrationFailedException e) {
log.error("Migration failed.", e);
allMigrationsSucceeded = false;
}
}
}

if (allMigrationsSucceeded) {
try {
Path versionFilePath = dataDir.resolve("version");
Files.writeString(versionFilePath, ApplicationVersion.getVersion().toString());
} catch (IOException e) {
throw new MigrationFailedException(e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package bisq.application.migration.migrations;

import bisq.common.application.ApplicationVersion;
import bisq.common.file.FileUtils;
import bisq.common.platform.OS;
import bisq.common.platform.Version;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class MigrationsForV2_1_2 implements Migration {
Expand All @@ -18,9 +16,6 @@ public void run(Path dataDir) {
Path torDataDir = dataDir.resolve("tor");
FileUtils.deleteFileOrDirectory(torDataDir);
}

Path versionFilePath = dataDir.resolve("version");
Files.writeString(versionFilePath, ApplicationVersion.getVersion().toString());
} catch (IOException e) {
throw new MigrationFailedException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package bisq.application.migration;

import bisq.common.application.ApplicationVersion;
import bisq.common.platform.Version;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import static org.assertj.core.api.Assertions.assertThat;

public class MigratorTest {
@Test
void migrationSuccess(@TempDir Path dataDir) throws IOException {
Path versionFilePath = dataDir.resolve("version");
Version dataDirVersion = new Version("2.1.0");
Files.writeString(versionFilePath, dataDirVersion.toString());

Version appVersion = ApplicationVersion.getVersion();
Migrator migrator = new Migrator(appVersion, dataDir);

migrator.migrate();

String readVersion = Files.readString(dataDir.resolve("version"));
assertThat(readVersion).isEqualTo(appVersion.toString());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package bisq.application.migration.migrations;

import bisq.common.application.ApplicationVersion;
import bisq.common.platform.Version;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
Expand All @@ -18,20 +17,17 @@ public class MigrationsForV2_1_2Tests {
private final MigrationsForV2_1_2 migrationsForV212 = new MigrationsForV2_1_2();

@Test
void migrationTest(@TempDir Path dataDir) throws IOException {
void migrateEmptyDataDir(@TempDir Path dataDir) {
Version version = migrationsForV212.getVersion();
Version expectedVersion = new Version("2.1.2");
assertThat(version).isEqualTo(expectedVersion);

migrationsForV212.run(dataDir);
String readVersion = Files.readString(dataDir.resolve("version"));
assertThat(readVersion)
.isEqualTo(ApplicationVersion.getVersion().toString());
}

@Test
@EnabledOnOs({OS.MAC, OS.WINDOWS})
void torFilesRemovalTestOnMacAndWindows(@TempDir Path dataDir) throws IOException {
void torFilesRemovalTestOnMacAndWindows(@TempDir Path dataDir) {
Path torDataDir = dataDir.resolve("tor");
createFakeTorDataDir(torDataDir);

Expand All @@ -46,15 +42,11 @@ void torFilesRemovalTestOnMacAndWindows(@TempDir Path dataDir) throws IOExceptio

fileExists = pluggableTransportDir.resolve("README.SNOWFLAKE.md").toFile().exists();
assertThat(fileExists).isTrue();

Path versionFilePath = dataDir.resolve("version");
String version = Files.readString(versionFilePath);
assertThat(version).isEqualTo(ApplicationVersion.getVersion().toString());
}

@Test
@EnabledOnOs(OS.LINUX)
void torFilesRemovalTestOnLinux(@TempDir Path dataDir) throws IOException {
void torFilesRemovalTestOnLinux(@TempDir Path dataDir) {
Path torDataDir = dataDir.resolve("tor");
createFakeTorDataDir(torDataDir);

Expand All @@ -69,10 +61,6 @@ void torFilesRemovalTestOnLinux(@TempDir Path dataDir) throws IOException {

fileExists = pluggableTransportDir.resolve("README.SNOWFLAKE.md").toFile().exists();
assertThat(fileExists).isFalse();

Path versionFilePath = dataDir.resolve("version");
String version = Files.readString(versionFilePath);
assertThat(version).isEqualTo(ApplicationVersion.getVersion().toString());
}

private void createFakeTorDataDir(Path torDataDir) {
Expand Down

0 comments on commit 59cd878

Please sign in to comment.