restructure ef core migrations

-added different context for production environments
-added migrations for both dev and production databases
-added config values ``ProdDbProvider`` and ``ProdDbConnection`` (since most likely production environment will use PGSQL or MySQL, although PGSQL migrations are not provided but can be made)
This commit is contained in:
Alan Moon 2025-03-19 11:48:47 -07:00
parent 4b9d7aca98
commit e9995f0876
11 changed files with 7463 additions and 26 deletions

View File

@ -9,8 +9,10 @@ public class ApiServerConfig {
public bool LoadNonSoDData { get; set; } = false;
public DbProviders DbProvider { get; set; } = DbProviders.SQLite;
public DbProviders ProdDbProvider { get; set; } = DbProviders.MySQL;
public string DbPath { get; set; } = string.Empty;
public string DbConnection { get; set; } = string.Empty;
public string ProdDbConnection { get; set; } = string.Empty;
}
public enum DbProviders {

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,34 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using sodoff.Configuration;
namespace sodoff.Model
{
public class ProductionDBContext : DBContext
{
private readonly IOptions<ApiServerConfig> config;
public ProductionDBContext(IOptions<ApiServerConfig> config) : base(config)
{
this.config = config;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
#if USE_POSTGRESQL
if (config.Value.ProdDbProvider == DbProviders.PostgreSQL)
{
optionsBuilder.UseNpgsql(config.Value.ProdDbConnection).UseLazyLoadingProxies();
return;
}
#endif
#if USE_MYSQL
if (config.Value.ProdDbProvider == DbProviders.MySQL)
{
optionsBuilder.UseMySQL(config.Value.ProdDbConnection).UseLazyLoadingProxies();
return;
}
#endif
throw new Exception($"Unsupported Production DbProvider {config.Value.ProdDbProvider}");
}
}
}

View File

@ -21,6 +21,7 @@ builder.Services.AddControllers(options => {
options.Filters.Add<LogRequestOnError>();
});
builder.Services.AddDbContext<DBContext>();
builder.Services.AddDbContext<ProductionDBContext>();
// create Modding Service singleton here ... do this before start http server to avoid serve invalid assets list
builder.Services.AddSingleton<ModdingService>(new ModdingService());
@ -63,11 +64,10 @@ var app = builder.Build();
using var scope = app.Services.CreateScope();
if(app.Environment.IsDevelopment())
{
var migrations = scope.ServiceProvider.GetRequiredService<DBContext>().Database.GetPendingMigrations();
if (migrations != null) await scope.ServiceProvider.GetRequiredService<DBContext>().Database.MigrateAsync();
}
if (app.Environment.IsDevelopment())
scope.ServiceProvider.GetRequiredService<DBContext>().Database.Migrate();
else
scope.ServiceProvider.GetRequiredService<ProductionDBContext>().Database.Migrate();
// Configure the HTTP request pipeline.

View File

@ -63,13 +63,19 @@
"// DbProvider": "Select database backend to use: SQLite, PostgreSQL, MySQL (availability may depend on build options)",
"DbProvider": "SQLite",
"// ProdDbProvider": "Select production database backend to use: PostgreSQL, MySQL (availability may depend on build options)",
"ProdDbProvider": "MySQL",
"// DbPath": "Path to SQLite database file. If empty, \"sodoff.db\" from current directory will be used.",
"DbPath": "",
"// DbConnection": "Database connection string for PostgreSQL and MySQL",
"// DbConnection PostgreSQL Example": "Host=127.0.0.1;Database=sodoffdb;Username=sodoffuser;Password=secret",
"// DbConnection MySQL Example": "Server=127.0.0.1;Database=sodoffdb;Uid=sodoffuser;Pwd=secret;Allow User Variables=True",
"DbConnection": ""
"DbConnection": "",
"// ProdDbConnection": "Database connection string for PostgreSQL and MySQL (examples above)",
"ProdDbConnection": "Server=172.21.0.3;Database=sodoffdb;Uid=sodoffuser;Pwd=8s7IvsrMQWASTXs;Allow User Variables=True"
},
"Logging": {
"LogLevel": {

View File

@ -168,5 +168,7 @@
<ItemGroup>
<Folder Include="Dlls\" />
<Folder Include="Migrations\ProductionMigrations\" />
<Folder Include="Migrations\DevMigrations\" />
</ItemGroup>
</Project>