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

@ -44,33 +44,39 @@
"// UseCache": "When true, downloading assets in partial mode will be stored in assets-cache for use in subsequent requests",
"UseCache": true
},
"ApiServer": {
"// ResponseURL": "When not empty is used as server url in some responses. Otherwise will be auto detected.",
"ResponseURL": "",
"ApiServer": {
"// ResponseURL": "When not empty is used as server url in some responses. Otherwise will be auto detected.",
"ResponseURL": "",
"// MMOAdress": "MMO server address (IP or domain) to use in GetMMOServerInfo* responses.",
"MMOAdress": "127.0.0.1",
"// MMOAdress": "MMO server address (IP or domain) to use in GetMMOServerInfo* responses.",
"MMOAdress": "127.0.0.1",
"// MMOPort": "MMO server port to use in GetMMOServerInfo* responses.",
"MMOPort": 9933,
"// MMOPort": "MMO server port to use in GetMMOServerInfo* responses.",
"MMOPort": 9933,
"// MMOSupportMinVersion": "Minimum client version allowed to use MMO. For example: '0xa3a31a0a' mean SoD 3.31, '0xa0000000' mean all SoD version, 0 mean all games.",
"MMOSupportMinVersion": "0",
"// LoadNonSoDData": "set to 'true' to support non SoD games, set to 'false' to reduce memory usage",
"LoadNonSoDData": true,
"// MMOSupportMinVersion": "Minimum client version allowed to use MMO. For example: '0xa3a31a0a' mean SoD 3.31, '0xa0000000' mean all SoD version, 0 mean all games.",
"MMOSupportMinVersion": "0",
"// DbProvider": "Select database backend to use: SQLite, PostgreSQL, MySQL (availability may depend on build options)",
"DbProvider": "SQLite",
"// LoadNonSoDData": "set to 'true' to support non SoD games, set to 'false' to reduce memory usage",
"LoadNonSoDData": true,
"// DbPath": "Path to SQLite database file. If empty, \"sodoff.db\" from current directory will be used.",
"DbPath": "",
"// DbProvider": "Select database backend to use: SQLite, PostgreSQL, MySQL (availability may depend on build options)",
"DbProvider": "SQLite",
"// 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": ""
},
"// 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": "",
"// 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": {
"Default": "Information",

View File

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