1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 11:22:57 +08:00

Merge pull request #16521 from peppy/backup-collections

Backup collections alongside main databases when migrating to realm
This commit is contained in:
Dan Balasescu 2022-01-19 17:02:49 +09:00 committed by GitHub
commit ddd1699621
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 20 deletions

View File

@ -2,9 +2,11 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.IO;
using System.Linq; using System.Linq;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using osu.Framework.Logging; using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Configuration; using osu.Game.Configuration;
using osu.Game.Models; using osu.Game.Models;
@ -22,14 +24,16 @@ namespace osu.Game.Database
private readonly DatabaseContextFactory efContextFactory; private readonly DatabaseContextFactory efContextFactory;
private readonly RealmContextFactory realmContextFactory; private readonly RealmContextFactory realmContextFactory;
private readonly OsuConfigManager config; private readonly OsuConfigManager config;
private readonly Storage storage;
private bool hasTakenBackup; 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.efContextFactory = efContextFactory;
this.realmContextFactory = realmContextFactory; this.realmContextFactory = realmContextFactory;
this.config = config; this.config = config;
this.storage = storage;
} }
public void Run() public void Run()
@ -73,15 +77,7 @@ namespace osu.Game.Database
{ {
Logger.Log($"Found {count} beatmaps in EF", LoggingTarget.Database); Logger.Log($"Found {count} beatmaps in EF", LoggingTarget.Database);
if (!hasTakenBackup) ensureBackup();
{
string migration = $"before_beatmap_migration_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}";
efContextFactory.CreateBackup($"client.{migration}.db");
realmContextFactory.CreateBackup($"client.{migration}.realm");
hasTakenBackup = true;
}
// only migrate data if the realm database is empty. // only migrate data if the realm database is empty.
// note that this cannot be written as: `realm.All<BeatmapSetInfo>().All(s => s.Protected)`, because realm does not support `.All()`. // note that this cannot be written as: `realm.All<BeatmapSetInfo>().All(s => s.Protected)`, because realm does not support `.All()`.
@ -201,15 +197,7 @@ namespace osu.Game.Database
{ {
Logger.Log($"Found {count} scores in EF", LoggingTarget.Database); Logger.Log($"Found {count} scores in EF", LoggingTarget.Database);
if (!hasTakenBackup) ensureBackup();
{
string migration = $"before_score_migration_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}";
efContextFactory.CreateBackup($"client.{migration}.db");
realmContextFactory.CreateBackup($"client.{migration}.realm");
hasTakenBackup = true;
}
// only migrate data if the realm database is empty. // only migrate data if the realm database is empty.
if (realm.All<ScoreInfo>().Any()) if (realm.All<ScoreInfo>().Any())
@ -272,6 +260,8 @@ namespace osu.Game.Database
if (!existingSkins.Any()) if (!existingSkins.Any())
return; return;
ensureBackup();
var userSkinChoice = config.GetBindable<string>(OsuSetting.Skin); var userSkinChoice = config.GetBindable<string>(OsuSetting.Skin);
int.TryParse(userSkinChoice.Value, out int userSkinInt); int.TryParse(userSkinChoice.Value, out int userSkinInt);
@ -342,6 +332,7 @@ namespace osu.Game.Database
return; return;
Logger.Log("Beginning settings migration to realm", LoggingTarget.Database); Logger.Log("Beginning settings migration to realm", LoggingTarget.Database);
ensureBackup();
using (var realm = realmContextFactory.CreateContext()) using (var realm = realmContextFactory.CreateContext())
using (var transaction = realm.BeginWrite()) using (var transaction = realm.BeginWrite())
@ -377,5 +368,22 @@ namespace osu.Game.Database
private string? getRulesetShortNameFromLegacyID(long rulesetId) => private string? getRulesetShortNameFromLegacyID(long rulesetId) =>
efContextFactory.Get().RulesetInfo.FirstOrDefault(r => r.ID == rulesetId)?.ShortName; 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");
using (var source = storage.GetStream("collection.db"))
using (var destination = storage.GetStream($"collection.{migration}.db", FileAccess.Write, FileMode.CreateNew))
source.CopyTo(destination);
hasTakenBackup = true;
}
}
} }
} }

View File

@ -195,7 +195,7 @@ namespace osu.Game
// A non-null context factory means there's still content to migrate. // A non-null context factory means there's still content to migrate.
if (efContextFactory != null) if (efContextFactory != null)
new EFToRealmMigrator(efContextFactory, realmFactory, LocalConfig).Run(); new EFToRealmMigrator(efContextFactory, realmFactory, LocalConfig, Storage).Run();
dependencies.CacheAs(Storage); dependencies.CacheAs(Storage);