Make creating the database not take 2 whole seconds in integration tests (#45151)

* Make creating the database not take 2 whole seconds in integration tests

* gramar
This commit is contained in:
DrSmugleaf 2026-08-14 18:44:34 +00:00 • committed by GitHub
parent 7c19a7d1de
commit 82e28037a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 21 additions and 4 deletions

View file

@ -12,6 +12,7 @@ public static partial class PoolManager
{
// @formatter:off
(CCVars.DatabaseSynchronous.Name, "true"),
(CCVars.DatabaseSnapshot.Name, "true"),
(CCVars.DatabaseSqliteDelay.Name, "0"),
(CCVars.HolidaysEnabled.Name, "false"),
(CCVars.GameMap.Name, TestMap),

View file

@ -64,7 +64,7 @@ namespace Content.IntegrationTests.Tests.Preferences
var conn = new SqliteConnection("Data Source=:memory:");
conn.Open();
builder.UseSqlite(conn);
return new ServerDbSqlite(() => builder.Options, true, cfg, true, opsLog, serialization);
return new ServerDbSqlite(() => builder.Options, true, cfg, true, opsLog, serialization, false);
}
[Test]

View file

@ -421,6 +421,7 @@ namespace Content.Server.Database
private ISawmill _sawmill = default!;
private bool _synchronous;
private bool _snapshot;
// When running in integration tests, we'll use a single in-memory SQLite database connection.
// This is that connection, close it when we shut down.
private SqliteConnection? _sqliteInMemoryConnection;
@ -437,6 +438,7 @@ namespace Content.Server.Database
_sawmill = _logMgr.GetSawmill("db.manager");
_synchronous = _cfg.GetCVar(CCVars.DatabaseSynchronous);
_snapshot = _cfg.GetCVar(CCVars.DatabaseSnapshot);
var engine = _cfg.GetCVar(CCVars.DatabaseEngine).ToLower();
var opsLog = _logMgr.GetSawmill("db.op");
@ -445,7 +447,7 @@ namespace Content.Server.Database
{
case "sqlite":
SetupSqlite(out var contextFunc, out var inMemory);
_db = new ServerDbSqlite(contextFunc, inMemory, _cfg, _synchronous, opsLog, _serialization);
_db = new ServerDbSqlite(contextFunc, inMemory, _cfg, _synchronous, opsLog, _serialization, _snapshot);
break;
case "postgres":
var (pgOptions, conString) = CreatePostgresOptions();

View file

@ -38,7 +38,8 @@ namespace Content.Server.Database
IConfigurationManager cfg,
bool synchronous,
ISawmill opsLog,
ISerializationManager serialization)
ISerializationManager serialization,
bool snapshot)
: base(opsLog, serialization)
{
_options = options;
@ -51,7 +52,12 @@ namespace Content.Server.Database
if (synchronous)
{
prefsCtx.Database.Migrate();
// EnsureCreated means you can't apply migrations later, fine for tests
if (snapshot)
prefsCtx.Database.EnsureCreated();
else
prefsCtx.Database.Migrate();
_dbReadyTask = Task.CompletedTask;
prefsCtx.Dispose();
}

View file

@ -74,4 +74,12 @@ public sealed partial class CCVars
/// </summary>
public static readonly CVarDef<bool> DatabaseSynchronous =
CVarDef.Create("database.sync", false, CVar.SERVERONLY);
/// <summary>
/// Creates the SQLite database using EnsureCreated instead of Migrate, if using SQLite.
/// Setting this to true means you cannot apply migrations later.
/// Meant to be used in integration tests only.
/// </summary>
public static readonly CVarDef<bool> DatabaseSnapshot =
CVarDef.Create("database.snapshot", false, CVar.SERVERONLY);
}