Correctly rollback failed imports

This commit is contained in:
Dean Herbert
2018-05-28 21:45:05 +09:00
parent bcb04f6168
commit a3287b8cf2

View File

@ -58,13 +58,20 @@ namespace osu.Game.Database
private readonly List<Action> cachedEvents = new List<Action>(); private readonly List<Action> cachedEvents = new List<Action>();
/// <summary>
/// Allows delaying of outwards events until an operation is confirmed (at a database level).
/// </summary>
private bool delayingEvents; private bool delayingEvents;
private void cacheEvents() /// <summary>
{ /// Begin delaying outwards events.
delayingEvents = true; /// </summary>
} private void delayEvents() => delayingEvents = true;
/// <summary>
/// Flush delayed events and disable delaying.
/// </summary>
/// <param name="perform">Whether the flushed events should be performed.</param>
private void flushEvents(bool perform) private void flushEvents(bool perform)
{ {
if (perform) if (perform)
@ -167,8 +174,8 @@ namespace osu.Game.Database
/// <param name="archive">The archive to be imported.</param> /// <param name="archive">The archive to be imported.</param>
public TModel Import(ArchiveReader archive) public TModel Import(ArchiveReader archive)
{ {
TModel item = null; TModel item;
cacheEvents(); delayEvents();
try try
{ {
@ -196,6 +203,7 @@ namespace osu.Game.Database
item = null; item = null;
} }
// we only want to flush events after we've confirmed the write context didn't have any errors.
flushEvents(item != null); flushEvents(item != null);
return item; return item;
} }