1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-26 22:32:55 +08:00

Localise EF context factory usage to migration only

This commit is contained in:
Dean Herbert 2022-01-11 22:16:06 +09:00
parent bf4133021b
commit e0c59f4b3c
3 changed files with 9 additions and 39 deletions

View File

@ -12,7 +12,6 @@ using osu.Framework.Platform;
using osu.Game.Audio;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
using osu.Game.Database;
using osu.Game.Graphics;
using osu.Game.Input;
using osu.Game.Input.Bindings;
@ -57,7 +56,6 @@ namespace osu.Game.Tests.Visual.Navigation
private IReadOnlyList<Type> requiredGameBaseDependencies => new[]
{
typeof(OsuGameBase),
typeof(DatabaseContextFactory),
typeof(Bindable<RulesetInfo>),
typeof(IBindable<RulesetInfo>),
typeof(Bindable<IReadOnlyList<Mod>>),

View File

@ -13,7 +13,7 @@ namespace osu.Game.Database
{
private readonly Storage storage;
private const string database_name = @"client.db";
public const string DATABASE_NAME = @"client.db";
private ThreadLocal<OsuDbContext> threadContexts;
@ -139,7 +139,7 @@ namespace osu.Game.Database
threadContexts = new ThreadLocal<OsuDbContext>(CreateContext, true);
}
protected virtual OsuDbContext CreateContext() => new OsuDbContext(CreateDatabaseConnectionString(database_name, storage))
protected virtual OsuDbContext CreateContext() => new OsuDbContext(CreateDatabaseConnectionString(DATABASE_NAME, storage))
{
Database = { AutoTransactionsEnabled = false }
};
@ -152,7 +152,7 @@ namespace osu.Game.Database
try
{
storage.Delete(database_name);
storage.Delete(DATABASE_NAME);
}
catch
{

View File

@ -149,8 +149,6 @@ namespace osu.Game
private MultiplayerClient multiplayerClient;
private DatabaseContextFactory contextFactory;
private RealmContextFactory realmFactory;
protected override Container<Drawable> Content => content;
@ -186,13 +184,13 @@ namespace osu.Game
Resources.AddStore(new DllResourceStore(OsuResources.ResourceAssembly));
dependencies.Cache(contextFactory = new DatabaseContextFactory(Storage));
DatabaseContextFactory efContextFactory = Storage.Exists(DatabaseContextFactory.DATABASE_NAME)
? new DatabaseContextFactory(Storage)
: null;
runMigrations();
dependencies.Cache(realmFactory = new RealmContextFactory(Storage, "client", contextFactory));
new EFToRealmMigrator(contextFactory, realmFactory, LocalConfig).Run();
dependencies.Cache(realmFactory = new RealmContextFactory(Storage, "client", efContextFactory));
if (efContextFactory != null)
new EFToRealmMigrator(efContextFactory, realmFactory, LocalConfig).Run();
dependencies.CacheAs(Storage);
@ -397,7 +395,6 @@ namespace osu.Game
Scheduler.Add(() =>
{
realmBlocker = realmFactory.BlockAllOperations();
contextFactory.FlushConnections();
readyToRun.Set();
}, false);
@ -458,29 +455,6 @@ namespace osu.Game
AvailableMods.Value = dict;
}
private void runMigrations()
{
try
{
using (var db = contextFactory.GetForWrite(false))
db.Context.Migrate();
}
catch (Exception e)
{
Logger.Error(e.InnerException ?? e, "Migration failed! We'll be starting with a fresh database.", LoggingTarget.Database);
// if we failed, let's delete the database and start fresh.
// todo: we probably want a better (non-destructive) migrations/recovery process at a later point than this.
contextFactory.ResetDatabase();
Logger.Log("Database purged successfully.", LoggingTarget.Database);
// only run once more, then hard bail.
using (var db = contextFactory.GetForWrite(false))
db.Context.Migrate();
}
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
@ -489,8 +463,6 @@ namespace osu.Game
BeatmapManager?.Dispose();
LocalConfig?.Dispose();
contextFactory?.FlushConnections();
realmFactory?.Dispose();
}
}