Create backup before any realm contexts are used

This commit is contained in:
Dean Herbert 2022-01-22 11:50:47 +09:00
parent 5622d2ba4f
commit 855ef3fa92
2 changed files with 15 additions and 16 deletions

View File

@ -100,10 +100,6 @@ protected override void LoadComplete()
{
base.LoadComplete();
// needs to be run on the update thread because of realm BlockAllOperations.
// maybe we can work around this? not sure..
createBackup();
Task.Factory.StartNew(() =>
{
using (var ef = efContextFactory.Get())
@ -470,17 +466,5 @@ private void migrateSettings(OsuDbContext db)
private string? getRulesetShortNameFromLegacyID(long rulesetId) =>
efContextFactory.Get().RulesetInfo.FirstOrDefault(r => r.ID == rulesetId)?.ShortName;
private void createBackup()
{
string migration = $"before_final_migration_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}";
efContextFactory.CreateBackup($"client.{migration}.db");
realmContextFactory.CreateBackup($"client.{migration}.realm");
using (var source = storage.GetStream("collection.db"))
using (var destination = storage.GetStream($"collection.{migration}.db", FileAccess.Write, FileMode.CreateNew))
source.CopyTo(destination);
}
}
}

View File

@ -197,6 +197,21 @@ private void load(ReadableKeyCombinationProvider keyCombinationProvider)
dependencies.Cache(RulesetStore = new RulesetStore(realmFactory, Storage));
dependencies.CacheAs<IRulesetStore>(RulesetStore);
// Backup is taken here rather than in EFToRealmMigrator to avoid recycling realm contexts
// after initial usages below. It can be moved once a direction is established for handling re-subscription.
// See https://github.com/ppy/osu/pull/16547 for more discussion.
if (EFContextFactory != null)
{
string migration = $"before_final_migration_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}";
EFContextFactory.CreateBackup($"client.{migration}.db");
realmFactory.CreateBackup($"client.{migration}.realm");
using (var source = Storage.GetStream("collection.db"))
using (var destination = Storage.GetStream($"collection.{migration}.db", FileAccess.Write, FileMode.CreateNew))
source.CopyTo(destination);
}
dependencies.CacheAs(Storage);
var largeStore = new LargeTextureStore(Host.CreateTextureLoaderStore(new NamespacedResourceStore<byte[]>(Resources, @"Textures")));