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

Create backups before deleting scores and beatmaps from EF database

This commit is contained in:
Dean Herbert 2022-01-18 14:19:25 +09:00
parent b1a75ce480
commit 798482c941
2 changed files with 24 additions and 9 deletions

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.EntityFrameworkCore.Storage;
@ -144,6 +145,13 @@ namespace osu.Game.Database
Database = { AutoTransactionsEnabled = false }
};
public void CreateBackup(string filename)
{
using (var source = storage.GetStream(DATABASE_NAME))
using (var destination = storage.GetStream(filename, FileAccess.Write, FileMode.CreateNew))
source.CopyTo(destination);
}
public void ResetDatabase()
{
lock (writeLock)

View File

@ -1,6 +1,8 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using osu.Game.Beatmaps;
@ -31,13 +33,16 @@ 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.
@ -47,13 +52,13 @@ namespace osu.Game.Database
private void migrateBeatmaps(DatabaseWriteUsage ef)
{
// can be removed 20220730.
var existingBeatmapSets = ef.Context.EFBeatmapSetInfo
.Include(s => s.Beatmaps).ThenInclude(b => b.RulesetInfo)
.Include(s => s.Beatmaps).ThenInclude(b => b.Metadata)
.Include(s => s.Beatmaps).ThenInclude(b => b.BaseDifficulty)
.Include(s => s.Files).ThenInclude(f => f.FileInfo)
.Include(s => s.Metadata)
.ToList();
List<EFBeatmapSetInfo> existingBeatmapSets = ef.Context.EFBeatmapSetInfo
.Include(s => s.Beatmaps).ThenInclude(b => b.RulesetInfo)
.Include(s => s.Beatmaps).ThenInclude(b => b.Metadata)
.Include(s => s.Beatmaps).ThenInclude(b => b.BaseDifficulty)
.Include(s => s.Files).ThenInclude(f => f.FileInfo)
.Include(s => s.Metadata)
.ToList();
// previous entries in EF are removed post migration.
if (!existingBeatmapSets.Any())
@ -121,6 +126,7 @@ namespace osu.Game.Database
}
}
efContextFactory.CreateBackup($"client.before_beatmap_migration_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}.db");
ef.Context.RemoveRange(existingBeatmapSets);
// Intentionally don't clean up the files, so they don't get purged by EF.
@ -207,6 +213,7 @@ namespace osu.Game.Database
}
}
efContextFactory.CreateBackup($"client.before_scores_migration_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}.db");
db.Context.RemoveRange(existingScores);
// Intentionally don't clean up the files, so they don't get purged by EF.