Files
Libation/Source/DataLayer/LibationContextFactory.cs
Taylor Southwick 1b5db9b28f Add support for postgres
Supporting postgres simplifies deployments to environments such as kubernetes. Since sqlite doesn't work well on nfs shares it can be easier for databases to have a dedicated db set up that applications can connect to. Sqlite is easier for most deployments though, so this will default to that if the settings haven't been updated to support it.

This change does the following:

- Separate out SQLite from the DataLayer and adds a Postgres assembly for migrations as well
- Add a configuration setting for a postgres connection string that will be used if it is there, otherwise reverts to the original sqlite string
- Add a copydb command for the cli to bootstrap the postgres db
- A convenience script to update migrations for both dbs at the same time
2025-10-27 16:30:50 -07:00

42 lines
1.4 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure;
using System;
namespace DataLayer
{
public class LibationContextFactory
{
public static void ConfigureOptions(NpgsqlDbContextOptionsBuilder options)
{
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
options.MigrationsAssembly("DataLayer.Postgres");
options.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery);
}
public static LibationContext CreatePostgres(string connectionString)
{
var options = new DbContextOptionsBuilder<LibationContext>();
options.UseNpgsql(connectionString, ConfigureOptions);
return new LibationContext(options.Options);
}
public static LibationContext CreateSqlite(string connectionString)
{
var options = new DbContextOptionsBuilder<LibationContext>();
options
.ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning))
.UseSqlite(connectionString, options =>
{
options.MigrationsAssembly("DataLayer.Sqlite");
options.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery);
});
return new LibationContext(options.Options);
}
}
}