From aa93042aa334c0111ddf279c73833c5b1f08add9 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 19 Jan 2022 16:28:16 +0900 Subject: [PATCH 1/2] Centralise backup code and also run on skin/settings migrations --- osu.Game/Database/EFToRealmMigrator.cs | 36 +++++++++++++------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/osu.Game/Database/EFToRealmMigrator.cs b/osu.Game/Database/EFToRealmMigrator.cs index 1de6c25070..23eb794837 100644 --- a/osu.Game/Database/EFToRealmMigrator.cs +++ b/osu.Game/Database/EFToRealmMigrator.cs @@ -73,15 +73,7 @@ namespace osu.Game.Database { Logger.Log($"Found {count} beatmaps in EF", LoggingTarget.Database); - if (!hasTakenBackup) - { - string migration = $"before_beatmap_migration_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}"; - - efContextFactory.CreateBackup($"client.{migration}.db"); - realmContextFactory.CreateBackup($"client.{migration}.realm"); - - hasTakenBackup = true; - } + ensureBackup(); // only migrate data if the realm database is empty. // note that this cannot be written as: `realm.All().All(s => s.Protected)`, because realm does not support `.All()`. @@ -201,15 +193,7 @@ namespace osu.Game.Database { Logger.Log($"Found {count} scores in EF", LoggingTarget.Database); - if (!hasTakenBackup) - { - string migration = $"before_score_migration_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}"; - - efContextFactory.CreateBackup($"client.{migration}.db"); - realmContextFactory.CreateBackup($"client.{migration}.realm"); - - hasTakenBackup = true; - } + ensureBackup(); // only migrate data if the realm database is empty. if (realm.All().Any()) @@ -272,6 +256,8 @@ namespace osu.Game.Database if (!existingSkins.Any()) return; + ensureBackup(); + var userSkinChoice = config.GetBindable(OsuSetting.Skin); int.TryParse(userSkinChoice.Value, out int userSkinInt); @@ -342,6 +328,7 @@ namespace osu.Game.Database return; Logger.Log("Beginning settings migration to realm", LoggingTarget.Database); + ensureBackup(); using (var realm = realmContextFactory.CreateContext()) using (var transaction = realm.BeginWrite()) @@ -377,5 +364,18 @@ namespace osu.Game.Database private string? getRulesetShortNameFromLegacyID(long rulesetId) => efContextFactory.Get().RulesetInfo.FirstOrDefault(r => r.ID == rulesetId)?.ShortName; + + private void ensureBackup() + { + if (!hasTakenBackup) + { + string migration = $"before_final_migration_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}"; + + efContextFactory.CreateBackup($"client.{migration}.db"); + realmContextFactory.CreateBackup($"client.{migration}.realm"); + + hasTakenBackup = true; + } + } } } From fad66d7682323f8fbee433c5fa9a4b9a2083a547 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 19 Jan 2022 16:31:27 +0900 Subject: [PATCH 2/2] Backup collections alongside main databases when migrating to realm --- osu.Game/Database/EFToRealmMigrator.cs | 10 +++++++++- osu.Game/OsuGameBase.cs | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/osu.Game/Database/EFToRealmMigrator.cs b/osu.Game/Database/EFToRealmMigrator.cs index 23eb794837..4c5dfe79cf 100644 --- a/osu.Game/Database/EFToRealmMigrator.cs +++ b/osu.Game/Database/EFToRealmMigrator.cs @@ -2,9 +2,11 @@ // See the LICENCE file in the repository root for full licence text. using System; +using System.IO; using System.Linq; using Microsoft.EntityFrameworkCore; using osu.Framework.Logging; +using osu.Framework.Platform; using osu.Game.Beatmaps; using osu.Game.Configuration; using osu.Game.Models; @@ -22,14 +24,16 @@ namespace osu.Game.Database private readonly DatabaseContextFactory efContextFactory; private readonly RealmContextFactory realmContextFactory; private readonly OsuConfigManager config; + private readonly Storage storage; private bool hasTakenBackup; - public EFToRealmMigrator(DatabaseContextFactory efContextFactory, RealmContextFactory realmContextFactory, OsuConfigManager config) + public EFToRealmMigrator(DatabaseContextFactory efContextFactory, RealmContextFactory realmContextFactory, OsuConfigManager config, Storage storage) { this.efContextFactory = efContextFactory; this.realmContextFactory = realmContextFactory; this.config = config; + this.storage = storage; } public void Run() @@ -374,6 +378,10 @@ namespace osu.Game.Database 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); + hasTakenBackup = true; } } diff --git a/osu.Game/OsuGameBase.cs b/osu.Game/OsuGameBase.cs index b24fdf2bfe..fb09dac1b1 100644 --- a/osu.Game/OsuGameBase.cs +++ b/osu.Game/OsuGameBase.cs @@ -195,7 +195,7 @@ namespace osu.Game // A non-null context factory means there's still content to migrate. if (efContextFactory != null) - new EFToRealmMigrator(efContextFactory, realmFactory, LocalConfig).Run(); + new EFToRealmMigrator(efContextFactory, realmFactory, LocalConfig, Storage).Run(); dependencies.CacheAs(Storage);