Use in-memory SQLite prefs DB when UserData is virtual.

This commit is contained in:
Pieter-Jan Briers
2020-08-20 18:46:46 +02:00
parent 3203fdfb7f
commit 496b456120
2 changed files with 23 additions and 8 deletions

View File

@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Npgsql;
namespace Content.Server.Database
@@ -50,9 +51,10 @@ namespace Content.Server.Database
public class SqliteConfiguration : IDatabaseConfiguration
{
private readonly string _databaseFilePath;
private readonly string? _databaseFilePath;
public SqliteConfiguration(string databaseFilePath)
/// <param name="databaseFilePath">If null, an in-memory database is used.</param>
public SqliteConfiguration(string? databaseFilePath)
{
_databaseFilePath = databaseFilePath;
}
@@ -62,7 +64,20 @@ namespace Content.Server.Database
get
{
var optionsBuilder = new DbContextOptionsBuilder<PreferencesDbContext>();
optionsBuilder.UseSqlite($"Data Source={_databaseFilePath}");
SqliteConnection connection;
if (_databaseFilePath != null)
{
connection = new SqliteConnection($"Data Source={_databaseFilePath}");
}
else
{
connection = new SqliteConnection("Data Source=:memory:");
// When using an in-memory DB we have to open it manually
// so EFCore doesn't open, close and wipe it.
connection.Open();
}
optionsBuilder.UseSqlite(connection);
return optionsBuilder.Options;
}
}