wip: doctor/cli/docs/api to vector db consolidation; api hardening for descriptions, tenant, and scopes; migrations and conversions of all DALs to EF v10
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql;
|
||||
using StellaOps.Findings.Ledger.EfCore.CompiledModels;
|
||||
using StellaOps.Findings.Ledger.EfCore.Context;
|
||||
|
||||
namespace StellaOps.Findings.Ledger.Infrastructure.Postgres;
|
||||
|
||||
internal static class FindingsLedgerDbContextFactory
|
||||
{
|
||||
public const string DefaultSchemaName = "public";
|
||||
|
||||
public static FindingsLedgerDbContext Create(NpgsqlConnection connection, int commandTimeoutSeconds, string schemaName)
|
||||
{
|
||||
var normalizedSchema = string.IsNullOrWhiteSpace(schemaName)
|
||||
? DefaultSchemaName
|
||||
: schemaName.Trim();
|
||||
|
||||
var optionsBuilder = new DbContextOptionsBuilder<FindingsLedgerDbContext>()
|
||||
.UseNpgsql(connection, npgsql => npgsql.CommandTimeout(commandTimeoutSeconds));
|
||||
|
||||
if (string.Equals(normalizedSchema, DefaultSchemaName, StringComparison.Ordinal))
|
||||
{
|
||||
// Use the static compiled model when schema mapping matches the default model.
|
||||
// Guard: only apply if compiled model has entity types registered.
|
||||
try
|
||||
{
|
||||
var compiledModel = FindingsLedgerDbContextModel.Instance;
|
||||
if (compiledModel.GetEntityTypes().Any())
|
||||
{
|
||||
optionsBuilder.UseModel(compiledModel);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Fall back to reflection model if compiled model is not fully initialized.
|
||||
}
|
||||
}
|
||||
|
||||
return new FindingsLedgerDbContext(optionsBuilder.Options, normalizedSchema);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user