|
| 1 | +# 0014. Configurable Database Provider |
| 2 | + |
| 3 | +Date: 2026-05-02 |
| 4 | + |
| 5 | +## Status |
| 6 | + |
| 7 | +Accepted — supersedes [ADR-0003](0003-use-sqlite-for-data-storage.md) |
| 8 | + |
| 9 | +## Context |
| 10 | + |
| 11 | +The project used SQLite as its only database engine (ADR-0003). This meant all environments — local development, Docker Compose, and any deployment — ran the same SQLite file-based database. For the majority of contributors this is ideal: zero infrastructure required. |
| 12 | + |
| 13 | +However, a fixed SQLite-only setup has limits: |
| 14 | + |
| 15 | +- SQLite does not support concurrent writes, so multi-instance deployments are not possible. |
| 16 | +- Developers who want to validate production-parity behaviour (e.g. PostgreSQL-specific query plans, type semantics, or constraint handling) have no path to do so without modifying the project. |
| 17 | +- A fixed SQLite-in-dev / PostgreSQL-in-prod split (a common alternative) introduces a different problem: subtle behavioral differences between environments that are hard to catch locally. |
| 18 | + |
| 19 | +The goal is to make the database engine fully configurable with a single environment variable, so the same stack is used consistently across every environment a developer chooses to run. |
| 20 | + |
| 21 | +## Decision |
| 22 | + |
| 23 | +We will introduce a `DATABASE_PROVIDER` environment variable that selects the database engine at startup: |
| 24 | + |
| 25 | +- **`DATABASE_PROVIDER=sqlite`** (default): SQLite everywhere. Zero infrastructure required. Works on any machine without Docker. Clone and run. |
| 26 | +- **`DATABASE_PROVIDER=postgres`**: PostgreSQL everywhere. Requires Docker. Opt-in for developers who want a server-based engine or full production parity. |
| 27 | + |
| 28 | +The default is `sqlite` to keep the barrier to entry as low as possible. |
| 29 | + |
| 30 | +### Provider selection at startup |
| 31 | + |
| 32 | +`ServiceCollectionExtensions.AddDbContextPool` reads `DATABASE_PROVIDER` and wires the appropriate EF Core provider: |
| 33 | + |
| 34 | +```csharp |
| 35 | +switch (provider.ToLowerInvariant()) |
| 36 | +{ |
| 37 | + case "postgres": |
| 38 | + options.UseNpgsql(Environment.GetEnvironmentVariable("DATABASE_URL")); |
| 39 | + break; |
| 40 | + default: |
| 41 | + options.UseSqlite($"Data Source={dataSource}"); |
| 42 | + break; |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +### Provider-specific EF Core migrations |
| 47 | + |
| 48 | +SQLite and PostgreSQL require different column type mappings (`TEXT`/`INTEGER` vs `uuid`/`boolean`/`timestamp with time zone`). A single migration set cannot satisfy both providers. To resolve this: |
| 49 | + |
| 50 | +- SQLite migrations remain in `Migrations/` (namespace `...Migrations`). |
| 51 | +- PostgreSQL migrations are placed in `Migrations/Npgsql/` (namespace `...Migrations.Npgsql`). |
| 52 | +- A custom `ProviderSpecificMigrationsAssembly` (implementing EF Core's `IMigrationsAssembly`) filters the discovered migrations to only those in the active provider's namespace. It is registered via `options.ReplaceService<IMigrationsAssembly, ProviderSpecificMigrationsAssembly>()`. |
| 53 | + |
| 54 | +`MigrateAsync()` at startup continues to work for both providers; each sees only its own migration set. |
| 55 | + |
| 56 | +### Docker Compose profiles |
| 57 | + |
| 58 | +The `postgres` service is declared under the `postgres` Compose profile so it only starts when explicitly requested: |
| 59 | + |
| 60 | +```bash |
| 61 | +# SQLite (default — no extra Docker service) |
| 62 | +docker compose up |
| 63 | + |
| 64 | +# PostgreSQL (opt-in) |
| 65 | +DATABASE_PROVIDER=postgres docker compose --profile postgres up |
| 66 | +``` |
| 67 | + |
| 68 | +## Consequences |
| 69 | + |
| 70 | +### Positive |
| 71 | + |
| 72 | +- Developers choose their engine once and use it consistently across all environments. |
| 73 | +- SQLite remains the zero-friction default — clone, run, done. |
| 74 | +- PostgreSQL is a first-class option for production-parity testing without requiring project-wide changes. |
| 75 | +- The Compose profile approach prevents accidental PostgreSQL containers from starting in SQLite mode. |
| 76 | +- `dotnet run` continues to work unchanged with SQLite. |
| 77 | + |
| 78 | +### Negative |
| 79 | + |
| 80 | +- Two parallel migration sets must be maintained. Adding a schema change requires migrations in both `Migrations/` and `Migrations/Npgsql/`. |
| 81 | +- `ProviderSpecificMigrationsAssembly` uses an EF Core internal API (`MigrationsAssembly` from `Microsoft.EntityFrameworkCore.Migrations.Internal`), annotated with `#pragma warning disable EF1001`. This may require updates on major EF Core version upgrades. |
| 82 | +- PostgreSQL support is not tested in CI (no Testcontainers integration yet — tracked in issue #353). |
| 83 | + |
| 84 | +### Neutral |
| 85 | + |
| 86 | +- The `DATABASE_URL` connection string format follows the Npgsql convention (`Host=...;Database=...;Username=...;Password=...`). |
| 87 | +- `.env.example` documents all three new environment variables (`DATABASE_PROVIDER`, `DATABASE_URL`, `POSTGRES_PASSWORD`). `.env` is git-ignored. |
0 commit comments