go back to using `EnsureCreated` due to project structure being incorrect for migrations of multiple database types

This commit is contained in:
Alan Moon 2025-03-19 13:20:47 -07:00
parent c41c5a34e8
commit e512568495
12 changed files with 113 additions and 7443 deletions

View File

@ -9,9 +9,8 @@ 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 DbPath { 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

View File

@ -44,52 +44,39 @@ public class DBContext : DbContext {
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
if(webHostEnvironment.IsDevelopment())
if (!webHostEnvironment.IsDevelopment()) config.Value.DbConnection = Environment.GetEnvironmentVariable("PRODUCTION_DB_CONNECTION");
#if USE_POSTGRESQL
if (config.Value.DbProvider == DbProviders.PostgreSQL)
{
#if USE_POSTGRESQL
if (config.Value.DbProvider == DbProviders.PostgreSQL) {
optionsBuilder.UseNpgsql(config.Value.DbConnection).UseLazyLoadingProxies();
return;
}
#endif
#if USE_MYSQL
if (config.Value.DbProvider == DbProviders.MySQL) {
optionsBuilder.UseMySQL(config.Value.DbConnection).UseLazyLoadingProxies();
return;
}
#endif
#if USE_SQLITE
if (config.Value.DbProvider == DbProviders.SQLite) {
string DbPath;
if (String.IsNullOrEmpty(config.Value.DbPath)) {
DbPath = Path.Join(Directory.GetCurrentDirectory(), "sodoff.db");
} else {
DbPath = config.Value.DbPath;
}
optionsBuilder.UseSqlite($"Data Source={DbPath}").UseLazyLoadingProxies();
return;
}
#endif
throw new Exception($"Unsupported DbProvider {config.Value.DbProvider}");
optionsBuilder.UseNpgsql(config.Value.DbConnection).UseLazyLoadingProxies();
return;
}
else
#endif
#if USE_MYSQL
if (config.Value.DbProvider == DbProviders.MySQL)
{
#if USE_POSTGRESQL
if (config.Value.ProdDbProvider == DbProviders.PostgreSQL)
{
optionsBuilder.UseNpgsql(Environment.GetEnvironmentVariable("PRODUCTION_DB_CONNECTION")).UseLazyLoadingProxies();
return;
}
#endif
#if USE_MYSQL
if (config.Value.ProdDbProvider == DbProviders.MySQL)
{
optionsBuilder.UseMySQL(Environment.GetEnvironmentVariable("PRODUCTION_DB_CONNECTION")).UseLazyLoadingProxies();
return;
}
#endif
throw new Exception($"Unsupported Production DbProvider {config.Value.ProdDbProvider}");
optionsBuilder.UseMySQL(config.Value.DbConnection).UseLazyLoadingProxies();
return;
}
#endif
#if USE_SQLITE
if (config.Value.DbProvider == DbProviders.SQLite)
{
string DbPath;
if (String.IsNullOrEmpty(config.Value.DbPath))
{
DbPath = Path.Join(Directory.GetCurrentDirectory(), "sodoff.db");
}
else
{
DbPath = config.Value.DbPath;
}
optionsBuilder.UseSqlite($"Data Source={DbPath}").UseLazyLoadingProxies();
return;
}
#endif
throw new Exception($"Unsupported DbProvider {config.Value.DbProvider}");
}
protected override void OnModelCreating(ModelBuilder builder) {

View File

@ -8,6 +8,7 @@ using sodoff.Utils;
using System.Xml;
using System.Net;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
var builder = WebApplication.CreateBuilder(args);
@ -63,7 +64,7 @@ var app = builder.Build();
using var scope = app.Services.CreateScope();
scope.ServiceProvider.GetRequiredService<DBContext>().Database.Migrate();
scope.ServiceProvider.GetRequiredService<DBContext>().Database.EnsureCreated();
// Configure the HTTP request pipeline.

View File

@ -63,9 +63,6 @@
"// 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": "",

81
src/appsettings.prod.json Normal file
View File

@ -0,0 +1,81 @@
{
"Kestrel": {
"EndPoints": {
"Http": {
"// Url": "Listening URL for the API server - allows setting the listening IP address and port number -> http://ip.ip.ip.ip:port/ or http://[ip::ip]:port/",
"Url": "http://*:5000"
}
}
},
"AssetServer": {
"// Enabled": "Set to false to disable the built-in asset server",
"Enabled": true,
"// ListenIP": "Listening IP address for the asset server, default is '*' which represents all IPv4 and IPv6 addresses",
"ListenIP": "*",
"// Port": "Listening port number for the asset server. Should be different than for the server API",
"Port": 5001,
"// URLPrefix (1)": "Extra prefix in the URL; omitted while retrieving the path from the requested URL. See also: UseAnyURLPrefix setting.",
"// URLPrefix (2)": "For example, if set to '.com', then a request to 'http://localhost/.com/abc' will return the 'abc' file from the assets folder.",
"URLPrefix": ".com",
"// UseAnyURLPrefix (1)": "Extended version of URLPrefix (if set to true, then URLPrefix setting will be ignored).",
"// UseAnyURLPrefix (2)": "When true requires that requested URL contained extra prefix (one directory at path start), that will be omitted while retrieving the path from the requested URL.",
"// UseAnyURLPrefix (3)": "If set to true, then (for example) a request to 'http://localhost/any.com/abc' will return the 'abc' file from the assets folder ('any.com' will be omitted).",
"UseAnyURLPrefix": true,
"// Mode": "Two modes: full - everything is local, partial - downloads assets from ProviderURL if not found locally",
"Mode": "partial",
"// ProviderURL": "Proxy URL used in partial mode",
"ProviderURL": "https://web.archive.org/web/20230713000000id_/https://media.jumpstart.com/",
"// AutoEncryptRegexp": "Regular expression matched against the URL local part. If there's a match, the returned file will be encrypted on-the-fly using 3DES with AutoEncryptKey as the key",
"AutoEncryptRegexp": "/(2\\.[5-9]|2\\.[1-9][0-9]|3\\.[0-9]+)\\.[0-9]/DWADragonsMain.xml$",
"// AutoEncryptKey": "3DES key for encrypted files",
"AutoEncryptKey": "C92EC1AA-54CD-4D0C-A8D5-403FCCF1C0BD",
"// SubstituteMissingLocalAssets": "When true, if the game requests a High asset and only Low is available, it will return Low instead of downloading (partial mode) or error 404 (full mode)",
"SubstituteMissingLocalAssets": true,
"// 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": "",
"// 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,
"// 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,
"// DbProvider": "Select database backend to use: SQLite, PostgreSQL, MySQL (availability may depend on build options)",
"DbProvider": "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": ""
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

View File

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