2017-04-17 13:37:52 +08:00
|
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using osu.Framework.Logging;
|
2017-07-26 19:22:02 +08:00
|
|
|
|
using osu.Framework.Platform;
|
2017-04-17 13:37:52 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
|
{
|
2017-07-27 15:56:41 +08:00
|
|
|
|
public abstract class DatabaseBackedStore
|
2017-04-17 13:37:52 +08:00
|
|
|
|
{
|
2017-07-26 19:22:02 +08:00
|
|
|
|
protected readonly Storage Storage;
|
2017-04-17 13:37:52 +08:00
|
|
|
|
|
2017-10-17 14:50:42 +08:00
|
|
|
|
protected readonly Func<OsuDbContext> GetContext;
|
2017-10-17 14:00:27 +08:00
|
|
|
|
|
2017-10-17 14:50:42 +08:00
|
|
|
|
protected DatabaseBackedStore(Func<OsuDbContext> getContext, Storage storage = null)
|
2017-04-17 13:37:52 +08:00
|
|
|
|
{
|
2017-07-26 19:22:02 +08:00
|
|
|
|
Storage = storage;
|
2017-10-17 14:50:42 +08:00
|
|
|
|
GetContext = getContext;
|
2017-04-17 13:37:52 +08:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Prepare();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2017-04-17 16:43:48 +08:00
|
|
|
|
Logger.Error(e, $@"Failed to initialise the {GetType()}! Trying again with a clean database...");
|
2017-04-17 18:44:03 +08:00
|
|
|
|
Prepare(true);
|
2017-04-17 13:37:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-10-17 18:58:33 +08:00
|
|
|
|
/// Perform any common clean-up tasks. Should be run when idle, or whenever necessary.
|
2017-08-01 09:08:05 +08:00
|
|
|
|
/// </summary>
|
2017-10-17 18:58:33 +08:00
|
|
|
|
public virtual void Cleanup()
|
2017-08-01 09:08:05 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Prepare this database for use. Tables should be created here.
|
2017-04-17 13:37:52 +08:00
|
|
|
|
/// </summary>
|
2017-04-17 18:44:03 +08:00
|
|
|
|
protected abstract void Prepare(bool reset = false);
|
2017-04-17 13:37:52 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reset this database to a default state. Undo all changes to database and storage backings.
|
|
|
|
|
/// </summary>
|
2017-04-17 18:44:03 +08:00
|
|
|
|
public void Reset() => Prepare(true);
|
2017-04-17 13:37:52 +08:00
|
|
|
|
}
|
2017-07-27 14:06:10 +08:00
|
|
|
|
}
|