forked from SoDOff-Project/sodoff
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:
parent
4b9d7aca98
commit
e9995f0876
@ -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 {
|
||||
|
1329
src/Migrations/DevMigrations/20250319184324_InitialCreate.Designer.cs
generated
Normal file
1329
src/Migrations/DevMigrations/20250319184324_InitialCreate.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
1023
src/Migrations/DevMigrations/20250319184324_InitialCreate.cs
Normal file
1023
src/Migrations/DevMigrations/20250319184324_InitialCreate.cs
Normal file
File diff suppressed because it is too large
Load Diff
1326
src/Migrations/DevMigrations/DBContextModelSnapshot.cs
Normal file
1326
src/Migrations/DevMigrations/DBContextModelSnapshot.cs
Normal file
File diff suppressed because it is too large
Load Diff
1330
src/Migrations/ProductionMigrations/20250319184417_InitialCreate.Designer.cs
generated
Normal file
1330
src/Migrations/ProductionMigrations/20250319184417_InitialCreate.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
1058
src/Migrations/ProductionMigrations/20250319184417_InitialCreate.cs
Normal file
1058
src/Migrations/ProductionMigrations/20250319184417_InitialCreate.cs
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
34
src/Model/ProductionDBContext.cs
Normal file
34
src/Model/ProductionDBContext.cs
Normal 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}");
|
||||
}
|
||||
}
|
||||
}
|
@ -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.
|
||||
|
||||
|
@ -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",
|
||||
"// 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,
|
||||
"// 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": "SQLite",
|
||||
"// DbProvider": "Select database backend to use: SQLite, PostgreSQL, MySQL (availability may depend on build options)",
|
||||
"DbProvider": "SQLite",
|
||||
|
||||
"// DbPath": "Path to SQLite database file. If empty, \"sodoff.db\" from current directory will be used.",
|
||||
"DbPath": "",
|
||||
"// ProdDbProvider": "Select production database backend to use: PostgreSQL, MySQL (availability may depend on build options)",
|
||||
"ProdDbProvider": "MySQL",
|
||||
|
||||
"// 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": ""
|
||||
},
|
||||
"// 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",
|
||||
|
@ -168,5 +168,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Dlls\" />
|
||||
<Folder Include="Migrations\ProductionMigrations\" />
|
||||
<Folder Include="Migrations\DevMigrations\" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
Loading…
x
Reference in New Issue
Block a user