diff --git a/samples/StateMachine/Trax.Samples.StateMachine.Api/Program.cs b/samples/StateMachine/Trax.Samples.StateMachine.Api/Program.cs index 3dd6b8a..dc52005 100644 --- a/samples/StateMachine/Trax.Samples.StateMachine.Api/Program.cs +++ b/samples/StateMachine/Trax.Samples.StateMachine.Api/Program.cs @@ -49,10 +49,7 @@ var connectionString = builder.Configuration.GetConnectionString("TraxDatabase") - ?? "Host=localhost;Port=5432;Database=trax_statemachine;Username=trax;Password=trax123"; - -// Create the sample database + snapshot tables before AddTrax (see SnapshotSchema). -await SnapshotSchema.EnsureAsync(connectionString); + ?? "Host=localhost;Port=5432;Database=trax;Username=trax;Password=trax123"; builder.Services.AddLogging(logging => logging.AddConsole()); @@ -75,7 +72,9 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); -// The snapshot store's DbContext (snapshot_draft + effect_claim tables). +// The snapshot store's DbContext. The snapshot_draft + effect_claim tables are created by the +// Trax migration set: UsePostgres above runs DbUp, which applies 040_state_machine_snapshots.sql. +// Nothing here creates tables — `docker compose up -d` (a `trax` database) is all that's needed. builder.Services.AddDbContext(options => options.UseNpgsql(connectionString)); builder.Services.AddTraxGraphQL(graphql => graphql); diff --git a/samples/StateMachine/Trax.Samples.StateMachine.Api/SnapshotSchema.cs b/samples/StateMachine/Trax.Samples.StateMachine.Api/SnapshotSchema.cs deleted file mode 100644 index 38bf53e..0000000 --- a/samples/StateMachine/Trax.Samples.StateMachine.Api/SnapshotSchema.cs +++ /dev/null @@ -1,49 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Npgsql; -using Trax.Effect.StateMachine.Persistence; - -namespace Trax.Samples.StateMachine.Api; - -/// -/// Creates the sample's database (if missing) and the snapshot_draft + effect_claim tables. -/// A production host ships these as a migration; the sample does it on startup so nothing but -/// docker compose up -d is needed. This must run before AddTrax, because -/// UsePostgres migrates the Trax framework tables during service registration, and it uses a -/// dedicated database so the snapshot tables are created reliably no matter what else is in the cluster. -/// -internal static class SnapshotSchema -{ - public static async Task EnsureAsync(string connectionString) - { - var target = new NpgsqlConnectionStringBuilder(connectionString); - var database = target.Database!; - - // Connect to the always-present `postgres` database to create ours if it does not exist yet. - var maintenance = new NpgsqlConnectionStringBuilder(connectionString) - { - Database = "postgres", - }; - await using (var admin = new NpgsqlConnection(maintenance.ConnectionString)) - { - await admin.OpenAsync(); - await using var cmd = admin.CreateCommand(); - cmd.CommandText = $"CREATE DATABASE \"{database}\""; - try - { - await cmd.ExecuteNonQueryAsync(); - } - catch (PostgresException ex) when (ex.SqlState == "42P04") - { - // Database already exists — idempotent no-op. - } - } - - // On a fresh database this creates the `trax` schema + the two snapshot tables; on later runs the - // tables already exist and this is a no-op. UsePostgres's DbUp then adds the framework tables - // (its `create schema if not exists trax` coexists with the schema created here). - await using var db = new SnapshotDbContext( - new DbContextOptionsBuilder().UseNpgsql(connectionString).Options - ); - await db.Database.EnsureCreatedAsync(); - } -} diff --git a/samples/StateMachine/Trax.Samples.StateMachine.Api/appsettings.json b/samples/StateMachine/Trax.Samples.StateMachine.Api/appsettings.json index c2f798f..75f9d2f 100644 --- a/samples/StateMachine/Trax.Samples.StateMachine.Api/appsettings.json +++ b/samples/StateMachine/Trax.Samples.StateMachine.Api/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "TraxDatabase": "Host=localhost;Port=5432;Database=trax_statemachine;Username=trax;Password=trax123" + "TraxDatabase": "Host=localhost;Port=5432;Database=trax;Username=trax;Password=trax123" }, "Logging": { "LogLevel": {