42 lines
1.2 KiB
C#
42 lines
1.2 KiB
C#
using System.Threading;
|
|
|
|
namespace NeonSprawl.Server.Game.Contracts;
|
|
|
|
/// <summary>Applies NEO-146 contract outcome table DDL once per process.</summary>
|
|
public static class PostgresContractOutcomeBootstrap
|
|
{
|
|
private static readonly string DdlRelativePath = Path.Combine("db", "migrations", "V012__contract_outcome.sql");
|
|
|
|
private static readonly Lock SchemaGate = new();
|
|
|
|
private static int _schemaReady;
|
|
|
|
public static void EnsureSchema(Npgsql.NpgsqlDataSource dataSource)
|
|
{
|
|
if (Volatile.Read(ref _schemaReady) != 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
lock (SchemaGate)
|
|
{
|
|
if (Volatile.Read(ref _schemaReady) != 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var ddlPath = Path.Combine(AppContext.BaseDirectory, DdlRelativePath);
|
|
if (!File.Exists(ddlPath))
|
|
{
|
|
throw new FileNotFoundException($"NEO-146 contract outcome DDL not found at '{ddlPath}'.", ddlPath);
|
|
}
|
|
|
|
var ddl = File.ReadAllText(ddlPath);
|
|
using var conn = dataSource.OpenConnection();
|
|
using var cmd = new Npgsql.NpgsqlCommand(ddl, conn);
|
|
cmd.ExecuteNonQuery();
|
|
Volatile.Write(ref _schemaReady, 1);
|
|
}
|
|
}
|
|
}
|