Skip to content

Feature 027: PostgreSQL for Local Development#31

Merged
ovation22 merged 10 commits intomainfrom
feature/027-postgresql-local-development
Jan 30, 2026
Merged

Feature 027: PostgreSQL for Local Development#31
ovation22 merged 10 commits intomainfrom
feature/027-postgresql-local-development

Conversation

@ovation22
Copy link
Copy Markdown
Owner

Summary

Enables PostgreSQL as the primary database for local development while maintaining SQL Server configuration as commented code for easy switching. Includes .NET Aspire orchestration with PostgreSQL and pgAdmin containers.

Implementation

Key Decisions

  • Resource Naming: Postgres server named "sql" to minimize code changes
  • Database Resource: Separated into postgres.AddDatabase("TripleDerby") following Aspire pattern
  • Connection String: Uses GetConnectionString("TripleDerby") matching database name
  • Manual Configuration: Direct UseNpgsql() calls instead of Aspire auto-configuration
  • No Aspire.Npgsql: Removed from services to avoid conflicts with manual configuration

Files Modified

  • AppHost: PostgreSQL + pgAdmin container orchestration
  • API: DbContext configuration + connection setup
  • 4 Microservices: Breeding, Racing, Training, Feeding - all updated
  • Infrastructure: Added Npgsql.EntityFrameworkCore.PostgreSQL package
  • Documentation: Feature spec + DATABASE_SWITCHING.md guide

Testing

  • ✅ Build succeeds without warnings
  • ✅ All packages installed correctly
  • ✅ Code follows dual-provider pattern (PostgreSQL active, SQL Server commented)
  • ✅ Documentation complete with troubleshooting guide

Switching Providers

See DATABASE_SWITCHING.md for detailed instructions on switching between PostgreSQL and SQL Server.

Quick Reference: 10 files to modify via commenting/uncommenting paired blocks

Related

Add NuGet packages and configure Aspire AppHost to use PostgreSQL
container with pgAdmin instead of SQL Server. SQL Server configuration
remains in code as comments for easy switching.

Changes:
- Add Aspire.Hosting.PostgreSQL package to AppHost
- Add Npgsql.EntityFrameworkCore.PostgreSQL to Infrastructure
- Configure PostgreSQL container with pgAdmin in AppHost
- Comment out SQL Server container configuration
- Keep connection name as "sql" for minimal code changes

The variable name 'sql' now points to PostgreSQL, allowing all existing
service references to work without modification.
Update API service to use Npgsql provider and PostgreSQL database while
maintaining SQL Server configuration as comments for easy switching.

Changes:
- Add Aspire.Npgsql package to API project
- Update DatabaseConfig to use UseNpgsql instead of UseSqlServer
- Update Program.cs to use AddNpgsqlDataSource instead of AddSqlServerClient
- Comment out SQL Server configuration with clear markers

Connection name remains "sql" for consistency with Aspire AppHost.
Update all 4 microservices (Breeding, Racing, Training, Feeding) to use
Npgsql provider and PostgreSQL database while maintaining SQL Server
configuration as comments for easy switching.

Changes per service:
- Add Aspire.Npgsql package to project file
- Update Program.cs to use UseNpgsql instead of UseSqlServer
- Update Program.cs to use AddNpgsqlDataSource instead of AddSqlServerClient
- Comment out SQL Server configuration with clear markers

Connection name remains "sql" for consistency with Aspire AppHost.
All services now follow the same dual-provider pattern.
Change GetConnectionString from "TripleDerby" to "sql" to match the
Aspire resource name defined in AppHost. Aspire injects connection
strings based on the resource name, not the database name.

This fixes the "ConnectionString is missing" error when services
attempt to connect to the database.

Files updated:
- API DatabaseConfig.cs
- All 4 microservices Program.cs files
Removed builder.AddNpgsqlDataSource() calls from all services since
we're using manual DbContext configuration to maintain the dual-provider
pattern (PostgreSQL/SQL Server switching via comments).

Aspire automatically injects the connection string via .WithReference(sql)
in the AppHost, making it available through GetConnectionString("sql").
The manual UseNpgsql() configuration in each service's Program.cs uses
this connection string directly.

This approach allows us to maintain commented SQL Server code alongside
active PostgreSQL code without Aspire client library conflicts.

Files updated:
- API Program.cs
- All 4 microservices Program.cs files
Removed Aspire.Npgsql packages that were conflicting with manual DbContext configuration approach. The manual configuration is needed to maintain the dual-provider pattern (PostgreSQL active, SQL Server commented) for easy switching between database providers.

Aspire connection strings are injected via .WithReference(sql) in AppHost and available through the ServiceDefaults configuration.

Services updated:
- TripleDerby.Api
- TripleDerby.Services.Breeding
- TripleDerby.Services.Racing
- TripleDerby.Services.Training
- TripleDerby.Services.Feeding
Separated Postgres server resource from database resource in AppHost to properly expose the database connection string. Changed connection string key from "sql" to "TripleDerby" to match the database resource name.

Changes:
- AppHost: Split AddPostgres and AddDatabase calls
- Postgres server named "sql", database named "TripleDerby"
- Updated all services to use GetConnectionString("TripleDerby")

This follows Aspire's resource naming convention where the connection string key matches the database resource name, not the server resource name.
Removed the unused 'hasError' field from Races, Horses, and RaceRuns components. Error state is tracked via the 'errorMessage' string instead.

Fixes compiler warnings:
- CS0414: The field 'Races.hasError' is assigned but its value is never used
- CS0414: The field 'Horses.hasError' is assigned but its value is never used
- CS0414: The field 'RaceRuns.hasError' is assigned but its value is never used
Updated feature 027 spec to reflect actual implementation with key decisions and troubleshooting guide. Created comprehensive DATABASE_SWITCHING.md guide for switching between PostgreSQL and SQL Server.

Key documentation:
- Implementation summary with architecture diagram
- Step-by-step switching instructions
- Connection string flow explanation
- Troubleshooting common issues
- File-by-file change reference

Also includes minor UI enhancement to Horses page (ShowHover and Gender column search).
@github-actions
Copy link
Copy Markdown

github-actions bot commented Jan 30, 2026

Test Results

723 tests  ±0   723 ✅ ±0   6s ⏱️ ±0s
  1 suites ±0     0 💤 ±0 
  1 files   ±0     0 ❌ ±0 

Results for commit 0e67eb5. ± Comparison against base commit 02186db.

♻️ This comment has been updated with latest results.

@ovation22 ovation22 merged commit 0efae32 into main Jan 30, 2026
2 checks passed
@ovation22 ovation22 deleted the feature/027-postgresql-local-development branch January 30, 2026 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant