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 bool LoadNonSoDData { get; set; } = false;
|
||||||
|
|
||||||
public DbProviders DbProvider { get; set; } = DbProviders.SQLite;
|
public DbProviders DbProvider { get; set; } = DbProviders.SQLite;
|
||||||
|
public DbProviders ProdDbProvider { get; set; } = DbProviders.MySQL;
|
||||||
public string DbPath { get; set; } = string.Empty;
|
public string DbPath { get; set; } = string.Empty;
|
||||||
public string DbConnection { get; set; } = string.Empty;
|
public string DbConnection { get; set; } = string.Empty;
|
||||||
|
public string ProdDbConnection { get; set; } = string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum DbProviders {
|
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>();
|
options.Filters.Add<LogRequestOnError>();
|
||||||
});
|
});
|
||||||
builder.Services.AddDbContext<DBContext>();
|
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
|
// create Modding Service singleton here ... do this before start http server to avoid serve invalid assets list
|
||||||
builder.Services.AddSingleton<ModdingService>(new ModdingService());
|
builder.Services.AddSingleton<ModdingService>(new ModdingService());
|
||||||
@ -63,11 +64,10 @@ var app = builder.Build();
|
|||||||
|
|
||||||
using var scope = app.Services.CreateScope();
|
using var scope = app.Services.CreateScope();
|
||||||
|
|
||||||
if(app.Environment.IsDevelopment())
|
if (app.Environment.IsDevelopment())
|
||||||
{
|
scope.ServiceProvider.GetRequiredService<DBContext>().Database.Migrate();
|
||||||
var migrations = scope.ServiceProvider.GetRequiredService<DBContext>().Database.GetPendingMigrations();
|
else
|
||||||
if (migrations != null) await scope.ServiceProvider.GetRequiredService<DBContext>().Database.MigrateAsync();
|
scope.ServiceProvider.GetRequiredService<ProductionDBContext>().Database.Migrate();
|
||||||
}
|
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
|
|
||||||
|
@ -63,13 +63,19 @@
|
|||||||
"// DbProvider": "Select database backend to use: SQLite, PostgreSQL, MySQL (availability may depend on build options)",
|
"// DbProvider": "Select database backend to use: SQLite, PostgreSQL, MySQL (availability may depend on build options)",
|
||||||
"DbProvider": "SQLite",
|
"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": "Path to SQLite database file. If empty, \"sodoff.db\" from current directory will be used.",
|
||||||
"DbPath": "",
|
"DbPath": "",
|
||||||
|
|
||||||
"// DbConnection": "Database connection string for PostgreSQL and MySQL",
|
"// DbConnection": "Database connection string for PostgreSQL and MySQL",
|
||||||
"// DbConnection PostgreSQL Example": "Host=127.0.0.1;Database=sodoffdb;Username=sodoffuser;Password=secret",
|
"// 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 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": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
|
@ -168,5 +168,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Dlls\" />
|
<Folder Include="Dlls\" />
|
||||||
|
<Folder Include="Migrations\ProductionMigrations\" />
|
||||||
|
<Folder Include="Migrations\DevMigrations\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user