Skip to content

Commit 9da0f4a

Browse files
committed
feat(db): add PostgreSQL provider support with config-based provider selection
1 parent 6eb95db commit 9da0f4a

11 files changed

Lines changed: 81 additions & 12 deletions

File tree

docker-compose.override.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ services:
1010

1111
auth-service:
1212
environment:
13+
- DatabaseProvider=SqlServer
1314
- ASPNETCORE_ENVIRONMENT=Development
1415
- ASPNETCORE_URLS=http://+:8080
1516
- ConnectionStrings__TaskManagementDbConnection=Server=sqlserver,1433;Database=TaskManagementDb;User Id=sa;Password=${SQL_PASSWORD:-Passw0rd!1234};TrustServerCertificate=True;
@@ -43,6 +44,7 @@ services:
4344
extra_hosts:
4445
- "auth.localhost:host-gateway"
4546
environment:
47+
- DatabaseProvider=SqlServer
4648
- ASPNETCORE_ENVIRONMENT=Development
4749
- ASPNETCORE_URLS=http://+:8080
4850
- ConnectionStrings__TaskManagementDbConnection=Server=sqlserver,1433;Database=TaskManagementDb;User Id=sa;Password=${SQL_PASSWORD:-Passw0rd!1234};TrustServerCertificate=True;

src/TaskManagement.Api/Infrastructure/Persistence/Configuration/DatabaseConfiguration.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.Extensions.Configuration;
23

34
namespace TaskManagement.Api.Infrastructure.Persistence.Configuration
45
{
56
public static class DatabaseConfiguration
67
{
78
public static IServiceCollection AddDatabaseConfiguration(this IServiceCollection services, IConfiguration configuration)
89
{
10+
var databaseProvider = configuration["DatabaseProvider"] ?? "SqlServer";
11+
var sqlServerConnectionString = configuration.GetConnectionString("TaskManagementDbConnection");
12+
var postgresConnectionString = configuration.GetConnectionString("TaskManagementDbConnectionPostgres");
13+
914
services.AddDbContext<TaskManagementDbContext>(options =>
1015
{
11-
options.UseSqlServer(configuration.GetConnectionString("TaskManagementDbConnection"),
16+
if (IsPostgres(databaseProvider))
17+
{
18+
if (string.IsNullOrWhiteSpace(postgresConnectionString))
19+
{
20+
throw new InvalidOperationException("Connection string 'TaskManagementDbConnectionPostgres' not found.");
21+
}
22+
23+
options.UseNpgsql(postgresConnectionString,
24+
npgsqlOptions => npgsqlOptions.EnableRetryOnFailure(
25+
maxRetryCount: 5,
26+
maxRetryDelay: TimeSpan.FromSeconds(10),
27+
errorCodesToAdd: null));
28+
return;
29+
}
30+
31+
if (string.IsNullOrWhiteSpace(sqlServerConnectionString))
32+
{
33+
throw new InvalidOperationException("Connection string 'TaskManagementDbConnection' not found.");
34+
}
35+
36+
options.UseSqlServer(sqlServerConnectionString,
1237
sqlServerOptions => sqlServerOptions.EnableRetryOnFailure(
1338
maxRetryCount: 5,
1439
maxRetryDelay: TimeSpan.FromSeconds(10),
@@ -18,6 +43,11 @@ public static IServiceCollection AddDatabaseConfiguration(this IServiceCollectio
1843
return services;
1944
}
2045

46+
private static bool IsPostgres(string provider)
47+
=> provider.Equals("postgres", StringComparison.OrdinalIgnoreCase)
48+
|| provider.Equals("postgresql", StringComparison.OrdinalIgnoreCase)
49+
|| provider.Equals("npgsql", StringComparison.OrdinalIgnoreCase);
50+
2151
public static async Task ApplyMigrationsAsync(this WebApplication app)
2252
{
2353
using var scope = app.Services.CreateScope();

src/TaskManagement.Api/TaskManagement.Api.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<PrivateAssets>all</PrivateAssets>
2121
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2222
</PackageReference>
23+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.11" />
2324
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.11" />
2425
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
2526
<PackageReference Include="OpenIddict.Validation.AspNetCore" Version="6.0.0" />

src/TaskManagement.Api/appsettings.Development.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
{
2+
"DatabaseProvider": "SqlServer",
23
"Logging": {
34
"LogLevel": {
45
"Default": "Information",
56
"Microsoft.AspNetCore": "Warning"
67
}
78
},
89
"ConnectionStrings": {
9-
"TaskManagementDbConnection": "Server=(localdb)\\localhost;Database=TaskManagementDb;User Id=l0c4lh0st;Password=p4ssw0rd123!;TrustServerCertificate=True;"
10+
"TaskManagementDbConnection": "Server=(localdb)\\localhost;Database=TaskManagementDb;User Id=l0c4lh0st;Password=p4ssw0rd123!;TrustServerCertificate=True;",
11+
"TaskManagementDbConnectionPostgres": "Host=localhost;Port=5432;Database=TaskManagementDb;Username=postgres;Password=postgres"
1012
},
1113
"OpenIddict": {
1214
"Issuer": "https://localhost:44377/",

src/TaskManagement.Api/appsettings.Testing.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"DatabaseProvider": "SqlServer",
23
"Logging": {
34
"LogLevel": {
45
"Default": "Information",

src/TaskManagement.Api/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"DatabaseProvider": "SqlServer",
23
"Serilog": {
34
"Using": [ "Serilog.Sinks.Console" ],
45
"MinimumLevel": {
@@ -33,4 +34,4 @@
3334
}
3435
},
3536
"AllowedHosts": "*"
36-
}
37+
}

src/TaskManagement.Auth/Infrastructure/Persistence/Configuration/DbConfiguration.cs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,38 @@ public static class DbConfiguration
77
{
88
public static IServiceCollection AddDatabaseConfiguration(this IServiceCollection services, IConfiguration configuration)
99
{
10-
var connectionString = configuration.GetConnectionString("TaskManagementDbConnection")
11-
?? throw new InvalidOperationException("Connection string 'TaskManagementDbConnection' not found.");
10+
var databaseProvider = configuration["DatabaseProvider"] ?? "SqlServer";
11+
var sqlServerConnectionString = configuration.GetConnectionString("TaskManagementDbConnection");
12+
var postgresConnectionString = configuration.GetConnectionString("TaskManagementDbConnectionPostgres");
1213

1314
services.AddDbContext<ApplicationDbContext>(options =>
1415
{
15-
options.UseSqlServer(connectionString,
16-
sqlServerOptions => sqlServerOptions.EnableRetryOnFailure(
17-
maxRetryCount: 5,
18-
maxRetryDelay: TimeSpan.FromSeconds(10),
19-
errorNumbersToAdd: null));
16+
if (IsPostgres(databaseProvider))
17+
{
18+
if (string.IsNullOrWhiteSpace(postgresConnectionString))
19+
{
20+
throw new InvalidOperationException("Connection string 'TaskManagementDbConnectionPostgres' not found.");
21+
}
22+
23+
options.UseNpgsql(postgresConnectionString,
24+
npgsqlOptions => npgsqlOptions.EnableRetryOnFailure(
25+
maxRetryCount: 5,
26+
maxRetryDelay: TimeSpan.FromSeconds(10),
27+
errorCodesToAdd: null));
28+
}
29+
else
30+
{
31+
if (string.IsNullOrWhiteSpace(sqlServerConnectionString))
32+
{
33+
throw new InvalidOperationException("Connection string 'TaskManagementDbConnection' not found.");
34+
}
35+
36+
options.UseSqlServer(sqlServerConnectionString,
37+
sqlServerOptions => sqlServerOptions.EnableRetryOnFailure(
38+
maxRetryCount: 5,
39+
maxRetryDelay: TimeSpan.FromSeconds(10),
40+
errorNumbersToAdd: null));
41+
}
2042

2143
options.UseOpenIddict();
2244
});
@@ -27,6 +49,11 @@ public static IServiceCollection AddDatabaseConfiguration(this IServiceCollectio
2749
return services;
2850
}
2951

52+
private static bool IsPostgres(string provider)
53+
=> provider.Equals("postgres", StringComparison.OrdinalIgnoreCase)
54+
|| provider.Equals("postgresql", StringComparison.OrdinalIgnoreCase)
55+
|| provider.Equals("npgsql", StringComparison.OrdinalIgnoreCase);
56+
3057
public static async Task ApplyMigrationsAndSeedDataAsync(this WebApplication app)
3158
{
3259
using var scope = app.Services.CreateScope();
@@ -53,4 +80,4 @@ public static async Task ApplyMigrationsAndSeedDataAsync(this WebApplication app
5380
}
5481
}
5582
}
56-
}
83+
}

src/TaskManagement.Auth/TaskManagement.Auth.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.11" />
3131
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.11" />
3232
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.11" />
33+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.11" />
3334
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.11" />
3435
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.11" />
3536
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />

src/TaskManagement.Auth/appsettings.Development.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
{
2+
"DatabaseProvider": "SqlServer",
23
"Logging": {
34
"LogLevel": {
45
"Default": "Information",
56
"Microsoft.AspNetCore": "Warning"
67
}
78
},
89
"ConnectionStrings": {
9-
"TaskManagementDbConnection": "Server=(localdb)\\localhost;Database=TaskManagementDb;User Id=l0c4lh0st;Password=p4ssw0rd123!;TrustServerCertificate=True;"
10+
"TaskManagementDbConnection": "Server=(localdb)\\localhost;Database=TaskManagementDb;User Id=l0c4lh0st;Password=p4ssw0rd123!;TrustServerCertificate=True;",
11+
"TaskManagementDbConnectionPostgres": "Host=localhost;Port=5432;Database=TaskManagementDb;Username=postgres;Password=postgres"
1012
},
1113
"OpenIddict": {
1214
"Audience": "task_management_api_dev",

src/TaskManagement.Auth/appsettings.Testing.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"DatabaseProvider": "SqlServer",
23
"ConnectionStrings": {
34
"TaskManagementDbConnection": "Server=(localdb)\\localhost;Database=TaskManagementDb_Test;User Id=sa;Password=Passw0rd!;TrustServerCertificate=True;"
45
},

0 commit comments

Comments
 (0)