1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 10:33:30 +08:00

Tidy up context creation

This commit is contained in:
Dean Herbert 2017-10-16 11:06:19 +09:00
parent 5a684f926f
commit 129cca0704
2 changed files with 19 additions and 18 deletions

View File

@ -22,14 +22,29 @@ namespace osu.Game.Database
public DbSet<RulesetInfo> RulesetInfo { get; set; } public DbSet<RulesetInfo> RulesetInfo { get; set; }
private readonly string connectionString; private readonly string connectionString;
public OsuDbContext() static OsuDbContext()
{ {
connectionString = "DataSource=:memory:"; // required to initialise native SQLite libraries on some platforms.
SQLitePCL.Batteries_V2.Init();
} }
public OsuDbContext(string connectionString) /// <summary>
/// Create a new OsuDbContext instance.
/// </summary>
/// <param name="connectionString">A valid SQLite connection string. If not provided, an in-memory instance will be created.</param>
public OsuDbContext(string connectionString = "DataSource=:memory:")
{ {
this.connectionString = connectionString; this.connectionString = connectionString;
Database.SetCommandTimeout(new TimeSpan(TimeSpan.TicksPerSecond * 10));
var connection = Database.GetDbConnection();
connection.Open();
using (var cmd = connection.CreateCommand())
{
cmd.CommandText = "PRAGMA journal_mode=WAL;";
cmd.ExecuteNonQuery();
}
} }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

View File

@ -81,21 +81,7 @@ namespace osu.Game
protected override IReadOnlyDependencyContainer CreateLocalDependencies(IReadOnlyDependencyContainer parent) => protected override IReadOnlyDependencyContainer CreateLocalDependencies(IReadOnlyDependencyContainer parent) =>
dependencies = new DependencyContainer(base.CreateLocalDependencies(parent)); dependencies = new DependencyContainer(base.CreateLocalDependencies(parent));
private OsuDbContext createDbContext() private OsuDbContext createDbContext() => new OsuDbContext(Host.Storage.GetDatabaseConnectionString(@"client"));
{
SQLitePCL.Batteries.Init();
var connectionString = Host.Storage.GetDatabaseConnectionString(@"client");
var context = new OsuDbContext(connectionString);
var connection = context.Database.GetDbConnection();
connection.Open();
using (var command = connection.CreateCommand())
{
command.CommandText = "PRAGMA journal_mode=WAL;";
command.ExecuteNonQuery();
}
context.Database.SetCommandTimeout(new TimeSpan(TimeSpan.TicksPerSecond * 10));
return context;
}
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load() private void load()