From afa22051a16b1ff9a39ac5113dda375049621199 Mon Sep 17 00:00:00 2001 From: Jay Malhotra <5047192+SapiensAnatis@users.noreply.github.com> Date: Fri, 10 May 2024 18:08:23 +0100 Subject: [PATCH] Add subcomponent to conn str --- DragaliaAPI/DragaliaAPI.Database/ApiContextFactory.cs | 2 +- .../DragaliaAPI.Database/DatabaseConfiguration.cs | 4 +++- DragaliaAPI/DragaliaAPI.Database/PostgresOptions.cs | 9 +++++++-- DragaliaAPI/DragaliaAPI/ServiceConfiguration.cs | 2 +- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/DragaliaAPI/DragaliaAPI.Database/ApiContextFactory.cs b/DragaliaAPI/DragaliaAPI.Database/ApiContextFactory.cs index f978ef00b..1888ab4fa 100644 --- a/DragaliaAPI/DragaliaAPI.Database/ApiContextFactory.cs +++ b/DragaliaAPI/DragaliaAPI.Database/ApiContextFactory.cs @@ -21,7 +21,7 @@ public ApiContext CreateDbContext(string[] args) ?.Get(); DbContextOptions contextOptions = new DbContextOptionsBuilder() - .UseNpgsql(postgresOptions?.GetConnectionString()) + .UseNpgsql(postgresOptions?.GetConnectionString("IDesignTimeDbContextFactory")) .Options; return new ApiContext(contextOptions, new StubPlayerIdentityService()); diff --git a/DragaliaAPI/DragaliaAPI.Database/DatabaseConfiguration.cs b/DragaliaAPI/DragaliaAPI.Database/DatabaseConfiguration.cs index 72fec4e85..da4256e36 100644 --- a/DragaliaAPI/DragaliaAPI.Database/DatabaseConfiguration.cs +++ b/DragaliaAPI/DragaliaAPI.Database/DatabaseConfiguration.cs @@ -24,7 +24,9 @@ PostgresOptions postgresOptions { services = services .AddDbContext(options => - options.UseNpgsql(postgresOptions.GetConnectionString()).EnableDetailedErrors() + options + .UseNpgsql(postgresOptions.GetConnectionString("ApiContext")) + .EnableDetailedErrors() ) #pragma warning disable CS0618 // Type or member is obsolete .AddScoped() diff --git a/DragaliaAPI/DragaliaAPI.Database/PostgresOptions.cs b/DragaliaAPI/DragaliaAPI.Database/PostgresOptions.cs index 0d00d8b71..8f84eb532 100644 --- a/DragaliaAPI/DragaliaAPI.Database/PostgresOptions.cs +++ b/DragaliaAPI/DragaliaAPI.Database/PostgresOptions.cs @@ -16,7 +16,12 @@ public class PostgresOptions public bool DisableAutoMigration { get; set; } - public string GetConnectionString() + /// + /// Gets a connection string for the current instance. + /// + /// An optional suffix to add to the application name of the connection string. + /// The connection string. + public string GetConnectionString(string subComponent) { NpgsqlConnectionStringBuilder connectionStringBuilder = new() @@ -27,7 +32,7 @@ public string GetConnectionString() Password = this.Password, Database = this.Database, IncludeErrorDetail = true, - ApplicationName = "DragaliaAPI", + ApplicationName = $"DragaliaAPI_{subComponent}", }; return connectionStringBuilder.ConnectionString; diff --git a/DragaliaAPI/DragaliaAPI/ServiceConfiguration.cs b/DragaliaAPI/DragaliaAPI/ServiceConfiguration.cs index 09c2fc109..4c8aa80c1 100644 --- a/DragaliaAPI/DragaliaAPI/ServiceConfiguration.cs +++ b/DragaliaAPI/DragaliaAPI/ServiceConfiguration.cs @@ -281,7 +281,7 @@ PostgresOptions postgresOptions .UseSimpleAssemblyNameTypeSerializer() .UseRecommendedSerializerSettings() .UsePostgreSqlStorage(pgCfg => - pgCfg.UseNpgsqlConnection(postgresOptions.GetConnectionString()) + pgCfg.UseNpgsqlConnection(postgresOptions.GetConnectionString("Hangfire")) ) );