feat(infra): add PostgreSQL provider switch (soft migration, default SqlServer) + local postgres:17 service
This commit is contained in:
@@ -5,8 +5,12 @@
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"Database": {
|
||||
"Provider": "SqlServer"
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
"Default": "Server=.;Integrated Security=False;Trusted_Connection=True;Encrypt=false;MultipleActiveResultSets=true;Database=GreenHomeDb;"
|
||||
"Default": "Server=.;Integrated Security=False;Trusted_Connection=True;Encrypt=false;MultipleActiveResultSets=true;Database=GreenHomeDb;",
|
||||
"Postgres": "Host=localhost;Port=5432;Database=greenhome;Username=greenhome;Password=greenhome"
|
||||
},
|
||||
"AvanakVoiceCall": {
|
||||
"Token": "A948B776B90CFD919B0EC60929714136CCB49DDB"
|
||||
|
||||
@@ -33,7 +33,24 @@ services:
|
||||
networks:
|
||||
- greenNetwork
|
||||
|
||||
postgres:
|
||||
image: postgres:17-alpine
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
POSTGRES_DB: greenhome
|
||||
POSTGRES_USER: greenhome
|
||||
POSTGRES_PASSWORD: greenhome
|
||||
ports:
|
||||
- "5432:5432"
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql/data
|
||||
networks:
|
||||
- greenNetwork
|
||||
|
||||
networks:
|
||||
serviceNetwork:
|
||||
name: greenNetwork
|
||||
external: true
|
||||
|
||||
volumes:
|
||||
pgdata:
|
||||
|
||||
@@ -13,6 +13,7 @@ using GreenHome.Infrastructure.Services.Telemetry;
|
||||
using GreenHome.Infrastructure.Services.Usage;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace GreenHome.Infrastructure;
|
||||
@@ -28,9 +29,19 @@ public static class DependencyInjection
|
||||
IConfiguration configuration)
|
||||
{
|
||||
// Entity Framework
|
||||
// Provider is switchable via config ("Database:Provider" = "SqlServer" | "Postgres")
|
||||
// to allow a soft migration: default stays SqlServer until cutover.
|
||||
var provider = configuration["Database:Provider"] ?? "SqlServer";
|
||||
services.AddDbContext<GreenHomeDbContext>(options =>
|
||||
{
|
||||
if (provider == "Postgres")
|
||||
{
|
||||
options.UseNpgsql(configuration.GetConnectionString("Postgres"));
|
||||
}
|
||||
else
|
||||
{
|
||||
options.UseSqlServer(configuration.GetConnectionString("Default"));
|
||||
}
|
||||
});
|
||||
|
||||
// Auth
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.9" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.9" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.9">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
||||
Reference in New Issue
Block a user