From b3916a5e16c81654bcc7f96e9d8f89e86fda9c6d Mon Sep 17 00:00:00 2001 From: Nikola Irinchev Date: Thu, 15 Apr 2021 11:06:08 +0200 Subject: [PATCH] Create a repro for the migrations file lock issue --- Tests/Realm.Tests/Database/MigrationTests.cs | 39 ++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/Tests/Realm.Tests/Database/MigrationTests.cs b/Tests/Realm.Tests/Database/MigrationTests.cs index 78a5a3ee1e..a406031a7e 100644 --- a/Tests/Realm.Tests/Database/MigrationTests.cs +++ b/Tests/Realm.Tests/Database/MigrationTests.cs @@ -28,6 +28,45 @@ public class MigrationTests : RealmInstanceTest { private const string FileToMigrate = "ForMigrationsToCopyAndMigrate.realm"; + [Test] + public void AAAAAA_MigrationLocksFiles() + { + var config = (RealmConfiguration)RealmConfiguration.DefaultConfiguration; + + using (var realm = GetRealm()) + { + } + + var config2 = config.ConfigWithPath(config.DatabasePath); + config2.SchemaVersion = 99; + + // If the line below or line 49 is commented out, the file can be deleted as normal. + config2.MigrationCallback = (migration, oldSchemaVersion) => { }; + + using (var realm2 = GetRealm(config2)) + { + // If you comment this out, the test will succeed, even with a migration callback + var people = realm2.All(); + } + + // Try to delete the Realm file - this is a copy of Realm.DeleteRealm + var fullpath = config2.DatabasePath; + + var filesToDelete = new[] { string.Empty, ".log_a", ".log_b", ".log", ".lock", ".note" } + .Select(ext => fullpath + ext) + .Where(File.Exists); + + foreach (var file in filesToDelete) + { + Assert.DoesNotThrow(() => File.Delete(file), $"Failed to delete {file}"); + } + + if (Directory.Exists($"{fullpath}.management")) + { + Assert.DoesNotThrow(() => Directory.Delete($"{fullpath}.management", recursive: true), "Failed to delete management dir."); + } + } + [Test] public void TriggerMigrationBySchemaVersion() {