using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
namespace OpenWaf.Infrastructure.Persistence;
///
/// Design-time context factory, so dotnet ef never has to boot the edge
/// to read the model.
///
/// Migrations are generated against PostgreSQL — the production provider —
/// because that is where a schema migration actually has to run. SQLite is the
/// single-node default and is kept in step by the same migration set; the two
/// providers differ in one place that matters (timestamps are integers on
/// SQLite, timestamptz on PostgreSQL) and that difference is a value converter,
/// not a schema difference.
///
public sealed class OpenWafDbContextFactory : IDesignTimeDbContextFactory
{
public OpenWafDbContext CreateDbContext(string[] args)
{
var useSqlite = Environment.GetEnvironmentVariable("OPENWAF_MIGRATE_PROVIDER")
?.Equals("sqlite", StringComparison.OrdinalIgnoreCase) == true;
var options = new DbContextOptionsBuilder()
.UseNpgsql(useSqlite
? "Host=localhost;Database=openwaf;Username=openwaf;Password=openwaf"
: "Host=localhost;Database=openwaf;Username=openwaf;Password=openwaf")
.Options;
return useSqlite ? new OpenWafDbContext(new DbContextOptionsBuilder()
.UseSqlite("Data Source=openwaf-design.db").Options) : new OpenWafDbContext(options);
}
}