From c41c5a34e8451b4eeb62199cbca729a135414d2d Mon Sep 17 00:00:00 2001 From: AlanMoonbase Date: Wed, 19 Mar 2025 12:39:26 -0700 Subject: [PATCH] remove seperate productiondbcontext to prevent having to change all of sodoffs code, instead determine which database to use in DBContext --- src/Model/DBContext.cs | 46 ++++++++++++++++++++++++-------- src/Model/ProductionDBContext.cs | 34 ----------------------- 2 files changed, 35 insertions(+), 45 deletions(-) delete mode 100644 src/Model/ProductionDBContext.cs diff --git a/src/Model/DBContext.cs b/src/Model/DBContext.cs index 245dcc6..3246fe9 100644 --- a/src/Model/DBContext.cs +++ b/src/Model/DBContext.cs @@ -35,37 +35,61 @@ public class DBContext : DbContext { public DbSet Buddies { get; set; } = null!; private readonly IOptions config; + private readonly IWebHostEnvironment webHostEnvironment; - public DBContext(IOptions config) { + public DBContext(IOptions config, IWebHostEnvironment webHostEnvironment) { this.config = config; + this.webHostEnvironment = webHostEnvironment; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - #if USE_POSTGRESQL + + if(webHostEnvironment.IsDevelopment()) + { + #if USE_POSTGRESQL if (config.Value.DbProvider == DbProviders.PostgreSQL) { optionsBuilder.UseNpgsql(config.Value.DbConnection).UseLazyLoadingProxies(); return; } - #endif - #if USE_MYSQL + #endif + #if USE_MYSQL if (config.Value.DbProvider == DbProviders.MySQL) { optionsBuilder.UseMySQL(config.Value.DbConnection).UseLazyLoadingProxies(); return; } - #endif - #if USE_SQLITE + #endif + #if USE_SQLITE if (config.Value.DbProvider == DbProviders.SQLite) { string DbPath; - if (String.IsNullOrEmpty(config.Value.DbPath)) { + if (String.IsNullOrEmpty(config.Value.DbPath)) { DbPath = Path.Join(Directory.GetCurrentDirectory(), "sodoff.db"); - } else { + } else { DbPath = config.Value.DbPath; - } + } optionsBuilder.UseSqlite($"Data Source={DbPath}").UseLazyLoadingProxies(); return; } - #endif - throw new Exception($"Unsupported DbProvider {config.Value.DbProvider}"); + #endif + throw new Exception($"Unsupported DbProvider {config.Value.DbProvider}"); + } + else + { + #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}"); + } } protected override void OnModelCreating(ModelBuilder builder) { diff --git a/src/Model/ProductionDBContext.cs b/src/Model/ProductionDBContext.cs deleted file mode 100644 index 189ff4d..0000000 --- a/src/Model/ProductionDBContext.cs +++ /dev/null @@ -1,34 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Options; -using sodoff.Configuration; - -namespace sodoff.Model -{ - public class ProductionDBContext : DBContext - { - private readonly IOptions config; - public ProductionDBContext(IOptions config) : base(config) - { - this.config = config; - } - - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - #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}"); - } - } -}