Switch a template to Postgres
Source content/
The project templates record runs in Postgres when
ConnectionStrings:Trax is set, and in memory when it is not. In memory loses runs on restart,
shares nothing between processes and leaves mode: QUEUE refused with TRAX_QUEUE_UNAVAILABLE, so
the templates allow it only in Development: started anywhere else without the connection string,
they refuse to start and name it, unless Trax:AllowInMemory is true, which their own tests set.
-
Start a Postgres:
docker run -d --name trax-db -p 127.0.0.1:5432:5432 \ -e POSTGRES_USER=trax -e POSTGRES_PASSWORD=trax123 -e POSTGRES_DB=trax postgres:17 -
Put the connection string in
appsettings.Development.json, or in the environment asConnectionStrings__Trax:{ "ConnectionStrings": { "Trax": "Host=localhost;Port=5432;Database=trax;Username=trax;Password=trax123" } } -
Run the host. Trax creates and migrates its
traxschema at startup, and the dashboard's runs now survive a restart.Program.csalready chooses the provider from the setting:var traxDatabase = builder.Configuration.GetConnectionString("Trax"); // ... refuses to start outside Development when it is null ... effects => (traxDatabase is null ? effects.UseInMemory() : effects.UsePostgres(traxDatabase))
A project made from a template before this choice was added has UseInMemory() alone: add the
Trax.Effect.Data.Postgres package at the version of its other Trax.* packages and make the same
change.
With a database, the scheduler queues work in it and runs it on local workers, so a
[TraxMutation] called with mode: QUEUE runs, and several processes on one database share the
work. HelloWorldTrain exposes Run only; change its [TraxMutation(GraphQLOperation.Run)] to
[TraxMutation] to serve the queue mode too.
The schema changes each release applies are listed in Schema migrations.
Move the application context
In the trax-hub and trax-api templates the application context, AppDbContext, is separate
and stays in memory. Its tables go under the schema in Data/AppSchema.cs, and on Postgres they
need EF Core migrations, as the comment above EnsureCreated() in Program.cs says.
EnsureCreated() creates nothing in a database that already has tables, and UsePostgres has
created Trax's by the time it runs, so the host starts without error and the first query of
AppDbContext fails with 42P01: relation "app.notes" does not exist.
-
Add the EF Core packages and the
dotnet eftool:dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL dotnet add package Microsoft.EntityFrameworkCore.Design dotnet new tool-manifest dotnet tool install dotnet-ef -
Change
UseInMemoryDatabase("app")toUseNpgsql, keeping EF Core's migration history in the application's schema:builder.Services.AddDbContextFactory<AppDbContext>(options => options.UseNpgsql( builder.Configuration.GetConnectionString("TraxDatabase"), npgsql => npgsql.MigrationsHistoryTable("__EFMigrationsHistory", AppSchema.Name) ) );Without
MigrationsHistoryTable, the history table goes to the first schema on the connection'ssearch_path, which istraxwhen the database user is namedtrax. -
Replace
db.Database.EnsureCreated();withdb.Database.Migrate();. -
Create the first migration.
dotnet efrunsProgram.csin the Development environment, so it readsConnectionStrings:TraxDatabasefromappsettings.Development.json:dotnet ef migrations add InitialApp --context AppDbContext
The host creates the app schema and its tables at startup, and applies each migration you add
after a model change.