Merge branch 'master' into realm-integration/reinit-on-corrupt

This commit is contained in:
Dean Herbert
2022-01-19 12:26:24 +09:00
29 changed files with 162 additions and 79 deletions

View File

@ -5,6 +5,7 @@ using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.EntityFrameworkCore.Storage;
using osu.Framework.Development;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Framework.Statistics;
@ -146,11 +147,15 @@ namespace osu.Game.Database
Database = { AutoTransactionsEnabled = false }
};
public void CreateBackup(string filename)
public void CreateBackup(string backupFilename)
{
Logger.Log($"Creating full EF database backup at {filename}", LoggingTarget.Database);
Logger.Log($"Creating full EF database backup at {backupFilename}", LoggingTarget.Database);
if (DebugUtils.IsDebugBuild)
Logger.Log("Your development database has been fully migrated to realm. If you switch back to a pre-realm branch and need your previous database, rename the backup file back to \"client.db\".\n\nNote that doing this can potentially leave your file store in a bad state.", level: LogLevel.Important);
using (var source = storage.GetStream(DATABASE_NAME))
using (var destination = storage.GetStream(filename, FileAccess.Write, FileMode.CreateNew))
using (var destination = storage.GetStream(backupFilename, FileAccess.Write, FileMode.CreateNew))
source.CopyTo(destination);
}

View File

@ -24,6 +24,8 @@ namespace osu.Game.Database
private readonly RealmContextFactory realmContextFactory;
private readonly OsuConfigManager config;
private bool hasTakenBackup;
public EFToRealmMigrator(DatabaseContextFactory efContextFactory, RealmContextFactory realmContextFactory, OsuConfigManager config)
{
this.efContextFactory = efContextFactory;
@ -34,16 +36,12 @@ namespace osu.Game.Database
public void Run()
{
using (var ef = efContextFactory.GetForWrite())
{
migrateSettings(ef);
using (var ef = efContextFactory.GetForWrite())
migrateSkins(ef);
using (var ef = efContextFactory.GetForWrite())
migrateBeatmaps(ef);
using (var ef = efContextFactory.GetForWrite())
migrateScores(ef);
}
// Delete the database permanently.
// Will cause future startups to not attempt migration.
@ -74,19 +72,25 @@ namespace osu.Game.Database
using (var realm = realmContextFactory.CreateContext())
{
Logger.Log($"Found {existingBeatmapSets.Count} beatmaps in EF", LoggingTarget.Database);
string migration = $"before_beatmap_migration_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}";
efContextFactory.CreateBackup($"client.{migration}.db");
if (!hasTakenBackup)
{
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.
// note that this cannot be written as: `realm.All<BeatmapInfo>().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()`.
if (realm.All<BeatmapSetInfo>().Any(s => !s.Protected))
{
Logger.Log("Skipping migration as realm already has beatmaps loaded", LoggingTarget.Database);
}
else
{
realmContextFactory.CreateBackup($"client.{migration}.realm");
using (var transaction = realm.BeginWrite())
{
foreach (var beatmapSet in existingBeatmapSets)
@ -144,10 +148,10 @@ namespace osu.Game.Database
}
transaction.Commit();
Logger.Log($"Successfully migrated {existingBeatmapSets.Count} beatmaps to realm", LoggingTarget.Database);
}
}
Logger.Log($"Successfully migrated {existingBeatmapSets.Count} beatmaps to realm", LoggingTarget.Database);
ef.Context.RemoveRange(existingBeatmapSets);
// Intentionally don't clean up the files, so they don't get purged by EF.
}
@ -199,19 +203,24 @@ namespace osu.Game.Database
using (var realm = realmContextFactory.CreateContext())
{
Logger.Log($"Found {existingScores.Count} scores in EF", LoggingTarget.Database);
string migration = $"before_score_migration_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}";
efContextFactory.CreateBackup($"client.{migration}.db");
if (!hasTakenBackup)
{
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.
// note that this cannot be written as: `realm.All<ScoreInfo>().All(s => s.Protected)`, because realm does not support `.All()`.
if (realm.All<ScoreInfo>().Any())
{
Logger.Log("Skipping migration as realm already has scores loaded", LoggingTarget.Database);
}
else
{
realmContextFactory.CreateBackup($"client.{migration}.realm");
using (var transaction = realm.BeginWrite())
{
foreach (var score in existingScores)
@ -248,10 +257,10 @@ namespace osu.Game.Database
}
transaction.Commit();
Logger.Log($"Successfully migrated {existingScores.Count} scores to realm", LoggingTarget.Database);
}
}
Logger.Log($"Successfully migrated {existingScores.Count} scores to realm", LoggingTarget.Database);
db.Context.RemoveRange(existingScores);
// Intentionally don't clean up the files, so they don't get purged by EF.
}

View File

@ -374,13 +374,13 @@ namespace osu.Game.Database
private string? getRulesetShortNameFromLegacyID(long rulesetId) =>
efContextFactory?.Get().RulesetInfo.FirstOrDefault(r => r.ID == rulesetId)?.ShortName;
public void CreateBackup(string filename)
public void CreateBackup(string backupFilename)
{
using (BlockAllOperations())
{
Logger.Log($"Creating full realm database backup at {filename}", LoggingTarget.Database);
Logger.Log($"Creating full realm database backup at {backupFilename}", LoggingTarget.Database);
using (var source = storage.GetStream(Filename))
using (var destination = storage.GetStream(filename, FileAccess.Write, FileMode.CreateNew))
using (var destination = storage.GetStream(backupFilename, FileAccess.Write, FileMode.CreateNew))
source.CopyTo(destination);
}
}