mirror of
https://github.com/ppy/osu.git
synced 2025-01-28 11:02:54 +08:00
Localise EF context factory usage to migration only
This commit is contained in:
parent
bf4133021b
commit
e0c59f4b3c
@ -12,7 +12,6 @@ using osu.Framework.Platform;
|
|||||||
using osu.Game.Audio;
|
using osu.Game.Audio;
|
||||||
using osu.Game.Beatmaps;
|
using osu.Game.Beatmaps;
|
||||||
using osu.Game.Configuration;
|
using osu.Game.Configuration;
|
||||||
using osu.Game.Database;
|
|
||||||
using osu.Game.Graphics;
|
using osu.Game.Graphics;
|
||||||
using osu.Game.Input;
|
using osu.Game.Input;
|
||||||
using osu.Game.Input.Bindings;
|
using osu.Game.Input.Bindings;
|
||||||
@ -57,7 +56,6 @@ namespace osu.Game.Tests.Visual.Navigation
|
|||||||
private IReadOnlyList<Type> requiredGameBaseDependencies => new[]
|
private IReadOnlyList<Type> requiredGameBaseDependencies => new[]
|
||||||
{
|
{
|
||||||
typeof(OsuGameBase),
|
typeof(OsuGameBase),
|
||||||
typeof(DatabaseContextFactory),
|
|
||||||
typeof(Bindable<RulesetInfo>),
|
typeof(Bindable<RulesetInfo>),
|
||||||
typeof(IBindable<RulesetInfo>),
|
typeof(IBindable<RulesetInfo>),
|
||||||
typeof(Bindable<IReadOnlyList<Mod>>),
|
typeof(Bindable<IReadOnlyList<Mod>>),
|
||||||
|
@ -13,7 +13,7 @@ namespace osu.Game.Database
|
|||||||
{
|
{
|
||||||
private readonly Storage storage;
|
private readonly Storage storage;
|
||||||
|
|
||||||
private const string database_name = @"client.db";
|
public const string DATABASE_NAME = @"client.db";
|
||||||
|
|
||||||
private ThreadLocal<OsuDbContext> threadContexts;
|
private ThreadLocal<OsuDbContext> threadContexts;
|
||||||
|
|
||||||
@ -139,7 +139,7 @@ namespace osu.Game.Database
|
|||||||
threadContexts = new ThreadLocal<OsuDbContext>(CreateContext, true);
|
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 }
|
Database = { AutoTransactionsEnabled = false }
|
||||||
};
|
};
|
||||||
@ -152,7 +152,7 @@ namespace osu.Game.Database
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
storage.Delete(database_name);
|
storage.Delete(DATABASE_NAME);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
@ -149,8 +149,6 @@ namespace osu.Game
|
|||||||
|
|
||||||
private MultiplayerClient multiplayerClient;
|
private MultiplayerClient multiplayerClient;
|
||||||
|
|
||||||
private DatabaseContextFactory contextFactory;
|
|
||||||
|
|
||||||
private RealmContextFactory realmFactory;
|
private RealmContextFactory realmFactory;
|
||||||
|
|
||||||
protected override Container<Drawable> Content => content;
|
protected override Container<Drawable> Content => content;
|
||||||
@ -186,13 +184,13 @@ namespace osu.Game
|
|||||||
|
|
||||||
Resources.AddStore(new DllResourceStore(OsuResources.ResourceAssembly));
|
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", efContextFactory));
|
||||||
|
if (efContextFactory != null)
|
||||||
dependencies.Cache(realmFactory = new RealmContextFactory(Storage, "client", contextFactory));
|
new EFToRealmMigrator(efContextFactory, realmFactory, LocalConfig).Run();
|
||||||
|
|
||||||
new EFToRealmMigrator(contextFactory, realmFactory, LocalConfig).Run();
|
|
||||||
|
|
||||||
dependencies.CacheAs(Storage);
|
dependencies.CacheAs(Storage);
|
||||||
|
|
||||||
@ -397,7 +395,6 @@ namespace osu.Game
|
|||||||
Scheduler.Add(() =>
|
Scheduler.Add(() =>
|
||||||
{
|
{
|
||||||
realmBlocker = realmFactory.BlockAllOperations();
|
realmBlocker = realmFactory.BlockAllOperations();
|
||||||
contextFactory.FlushConnections();
|
|
||||||
|
|
||||||
readyToRun.Set();
|
readyToRun.Set();
|
||||||
}, false);
|
}, false);
|
||||||
@ -458,29 +455,6 @@ namespace osu.Game
|
|||||||
AvailableMods.Value = dict;
|
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)
|
protected override void Dispose(bool isDisposing)
|
||||||
{
|
{
|
||||||
base.Dispose(isDisposing);
|
base.Dispose(isDisposing);
|
||||||
@ -489,8 +463,6 @@ namespace osu.Game
|
|||||||
BeatmapManager?.Dispose();
|
BeatmapManager?.Dispose();
|
||||||
LocalConfig?.Dispose();
|
LocalConfig?.Dispose();
|
||||||
|
|
||||||
contextFactory?.FlushConnections();
|
|
||||||
|
|
||||||
realmFactory?.Dispose();
|
realmFactory?.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user