1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-15 23:17:18 +08:00

Create backup of databases before opening contexts

Attempt to avoid file IO issues.

Closes #16531.
This commit is contained in:
Dean Herbert 2022-01-21 00:01:34 +09:00
parent 1a20725162
commit 079b2dfc42

View File

@ -26,8 +26,6 @@ namespace osu.Game.Database
private readonly OsuConfigManager config; private readonly OsuConfigManager config;
private readonly Storage storage; private readonly Storage storage;
private bool hasTakenBackup;
public EFToRealmMigrator(DatabaseContextFactory efContextFactory, RealmContextFactory realmContextFactory, OsuConfigManager config, Storage storage) public EFToRealmMigrator(DatabaseContextFactory efContextFactory, RealmContextFactory realmContextFactory, OsuConfigManager config, Storage storage)
{ {
this.efContextFactory = efContextFactory; this.efContextFactory = efContextFactory;
@ -38,6 +36,8 @@ namespace osu.Game.Database
public void Run() public void Run()
{ {
createBackup();
using (var ef = efContextFactory.Get()) using (var ef = efContextFactory.Get())
{ {
migrateSettings(ef); migrateSettings(ef);
@ -77,8 +77,6 @@ namespace osu.Game.Database
{ {
Logger.Log($"Found {count} beatmaps in EF", LoggingTarget.Database); Logger.Log($"Found {count} beatmaps in EF", LoggingTarget.Database);
ensureBackup();
// 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()`.
if (realm.All<BeatmapSetInfo>().Any(s => !s.Protected)) if (realm.All<BeatmapSetInfo>().Any(s => !s.Protected))
@ -210,8 +208,6 @@ namespace osu.Game.Database
{ {
Logger.Log($"Found {count} scores in EF", LoggingTarget.Database); Logger.Log($"Found {count} scores in EF", LoggingTarget.Database);
ensureBackup();
// 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())
{ {
@ -291,8 +287,6 @@ 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);
@ -363,7 +357,6 @@ 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())
@ -400,9 +393,7 @@ 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() private void createBackup()
{
if (!hasTakenBackup)
{ {
string migration = $"before_final_migration_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}"; string migration = $"before_final_migration_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}";
@ -412,9 +403,6 @@ namespace osu.Game.Database
using (var source = storage.GetStream("collection.db")) using (var source = storage.GetStream("collection.db"))
using (var destination = storage.GetStream($"collection.{migration}.db", FileAccess.Write, FileMode.CreateNew)) using (var destination = storage.GetStream($"collection.{migration}.db", FileAccess.Write, FileMode.CreateNew))
source.CopyTo(destination); source.CopyTo(destination);
hasTakenBackup = true;
}
} }
} }
} }