Skip to content

Commit e52abca

Browse files
nanotaboadaclaude
andcommitted
fix(config): fail fast on invalid DATABASE_PROVIDER; normalize case (#249)
Address post-merge review findings on the configurable provider feature. No behavioral change for valid sqlite/postgres values. - ServiceCollectionExtensions: trim and normalize DATABASE_PROVIDER before the switch; add explicit "sqlite"/"" case; throw InvalidOperationException for unrecognized values so typos like "postgress" no longer silently fall through to SQLite; replace null-coalescing guard on DATABASE_URL with IsNullOrWhiteSpace - entrypoint.sh: normalize DATABASE_PROVIDER to lowercase via tr so POSTGRES, Postgres, etc. are handled consistently - copilot-instructions.md: update Overview to mention both providers; replace SQLite-only "Never modify" constraint with a warning about ProviderSpecificMigrationsAssembly namespace constants Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 34bee4f commit e52abca

3 files changed

Lines changed: 20 additions & 9 deletions

File tree

.github/copilot-instructions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Overview
44

5-
REST API for managing football players built with ASP.NET Core 10. Implements CRUD operations with a layered architecture, EF Core + SQLite persistence, FluentValidation, AutoMapper, and in-memory caching. Part of a cross-language comparison study (Go, Java, Python, Rust, TypeScript). Primarily a learning and reference project — clarity and educational value take precedence over brevity.
5+
REST API for managing football players built with ASP.NET Core 10. Implements CRUD operations with a layered architecture, EF Core persistence (SQLite by default, PostgreSQL opt-in via `DATABASE_PROVIDER`), FluentValidation, AutoMapper, and in-memory caching. Part of a cross-language comparison study (Go, Java, Python, Rust, TypeScript). Primarily a learning and reference project — clarity and educational value take precedence over brevity.
66

77
## Tech Stack
88

@@ -181,7 +181,7 @@ Example: `feat(api): add player search endpoint (#123)`
181181
- Production configurations or deployment secrets
182182
- `.runsettings` coverage thresholds
183183
- Port configuration (9000)
184-
- Database type (SQLite — demo/dev only)
184+
- Migration namespace constants in `ProviderSpecificMigrationsAssembly` — renaming breaks runtime provider filtering for one or both providers
185185
- CD pipeline tag format (`vX.Y.Z-stadium`) or the stadium name sequence — names are assigned sequentially A→Z from the list in `CHANGELOG.md`; the next name is always the next unused letter
186186

187187
### Creating Issues

scripts/entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ log() {
1010

1111
log "✔ Starting container..."
1212

13-
DATABASE_PROVIDER="${DATABASE_PROVIDER:-sqlite}"
13+
DATABASE_PROVIDER=$(printf '%s' "${DATABASE_PROVIDER:-sqlite}" | tr '[:upper:]' '[:lower:]')
1414

1515
if [ "$DATABASE_PROVIDER" = "postgres" ]; then
1616
log "✔ Using PostgreSQL database."

src/Dotnet.Samples.AspNetCore.WebApi/Extensions/ServiceCollectionExtensions.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,25 @@ IWebHostEnvironment environment
3535
{
3636
services.AddDbContextPool<PlayerDbContext>(options =>
3737
{
38-
var provider = Environment.GetEnvironmentVariable("DATABASE_PROVIDER") ?? "sqlite";
38+
var provider = (
39+
Environment.GetEnvironmentVariable("DATABASE_PROVIDER") ?? ""
40+
)
41+
.Trim()
42+
.ToLowerInvariant();
3943

40-
switch (provider.ToLowerInvariant())
44+
switch (provider)
4145
{
4246
case "postgres":
43-
var connectionString =
44-
Environment.GetEnvironmentVariable("DATABASE_URL")
45-
?? throw new InvalidOperationException(
47+
var connectionString = Environment.GetEnvironmentVariable("DATABASE_URL");
48+
if (string.IsNullOrWhiteSpace(connectionString))
49+
throw new InvalidOperationException(
4650
"DATABASE_URL is required when DATABASE_PROVIDER=postgres."
4751
);
4852
options.UseNpgsql(connectionString);
4953
break;
5054

51-
default:
55+
case "sqlite":
56+
case "":
5257
var storagePath = Environment.GetEnvironmentVariable("STORAGE_PATH");
5358
var dataSource = !string.IsNullOrWhiteSpace(storagePath)
5459
? storagePath
@@ -61,6 +66,12 @@ IWebHostEnvironment environment
6166
}
6267
options.UseSqlite($"Data Source={dataSource}");
6368
break;
69+
70+
default:
71+
throw new InvalidOperationException(
72+
$"Unsupported DATABASE_PROVIDER value: '{provider}'. "
73+
+ "Valid values are 'sqlite' (default) and 'postgres'."
74+
);
6475
}
6576

6677
if (environment.IsDevelopment())

0 commit comments

Comments
 (0)