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;
|
using Npgsql;
|
||||||
|
|
||||||
namespace Content.Server.Database
|
namespace Content.Server.Database
|
||||||
@@ -50,9 +51,10 @@ namespace Content.Server.Database
|
|||||||
|
|
||||||
public class SqliteConfiguration : IDatabaseConfiguration
|
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;
|
_databaseFilePath = databaseFilePath;
|
||||||
}
|
}
|
||||||
@@ -62,7 +64,20 @@ namespace Content.Server.Database
|
|||||||
get
|
get
|
||||||
{
|
{
|
||||||
var optionsBuilder = new DbContextOptionsBuilder<PreferencesDbContext>();
|
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;
|
return optionsBuilder.Options;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,11 +50,11 @@ namespace Content.Server.Preferences
|
|||||||
{
|
{
|
||||||
case "sqlite":
|
case "sqlite":
|
||||||
var configPreferencesDbPath = _configuration.GetCVar<string>("database.prefs_sqlite_dbpath");
|
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);
|
Path.Combine(_resourceManager.UserData.RootDir, configPreferencesDbPath);
|
||||||
dbConfig = new SqliteConfiguration(
|
dbConfig = new SqliteConfiguration(finalPreferencesDbPath);
|
||||||
finalPreferencesDbPath
|
|
||||||
);
|
|
||||||
break;
|
break;
|
||||||
case "postgres":
|
case "postgres":
|
||||||
dbConfig = new PostgresConfiguration(
|
dbConfig = new PostgresConfiguration(
|
||||||
|
|||||||
Reference in New Issue
Block a user