Use in-memory SQLite prefs DB when UserData is virtual.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,11 +50,11 @@ namespace Content.Server.Preferences
|
||||
{
|
||||
case "sqlite":
|
||||
var configPreferencesDbPath = _configuration.GetCVar<string>("database.prefs_sqlite_dbpath");
|
||||
var finalPreferencesDbPath =
|
||||
var inMemory = _resourceManager.UserData.RootDir == null;
|
||||
var finalPreferencesDbPath = inMemory ?
|
||||
null :
|
||||
Path.Combine(_resourceManager.UserData.RootDir, configPreferencesDbPath);
|
||||
dbConfig = new SqliteConfiguration(
|
||||
finalPreferencesDbPath
|
||||
);
|
||||
dbConfig = new SqliteConfiguration(finalPreferencesDbPath);
|
||||
break;
|
||||
case "postgres":
|
||||
dbConfig = new PostgresConfiguration(
|
||||
|
||||
Reference in New Issue
Block a user