Add safe deletion support.

This commit is contained in:
Dean Herbert
2017-02-24 17:08:13 +09:00
parent 6c3bda18b6
commit 958bf54c31
3 changed files with 31 additions and 5 deletions

View File

@ -8,6 +8,7 @@ using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Cryptography;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Formats;
@ -39,16 +40,37 @@ namespace osu.Game.Database
try
{
connection = prepareConnection();
deletePending();
}
catch
catch (Exception e)
{
Console.WriteLine(@"Failed to initialise the beatmap database! Trying again with a clean database...");
Logger.Error(e, @"Failed to initialise the beatmap database! Trying again with a clean database...");
storage.DeleteDatabase(@"beatmaps");
connection = prepareConnection();
}
}
}
private void deletePending()
{
foreach (var b in Query<BeatmapSetInfo>().Where(b => b.DeletePending))
{
try
{
storage.Delete(b.Path);
connection.Delete(b);
}
catch (Exception e)
{
Logger.Error(e, $@"Could not delete beatmap {b.ToString()}");
}
}
//this is required because sqlite migrations don't work, initially inserting nulls into this field.
//see https://github.com/praeclarum/sqlite-net/issues/326
connection.Query<BeatmapSetInfo>("UPDATE BeatmapSetInfo SET DeletePending = 0 WHERE DeletePending IS NULL");
}
private SQLiteConnection prepareConnection()
{
var conn = storage.GetDatabase(@"beatmaps");
@ -161,8 +183,9 @@ namespace osu.Game.Database
public void Delete(BeatmapSetInfo beatmapSet)
{
storage.Delete(beatmapSet.Path);
connection.Delete(beatmapSet);
beatmapSet.DeletePending = true;
Update(beatmapSet, false);
BeatmapSetRemoved?.Invoke(beatmapSet);
}