From c3cbd977f1e8861ab774c43db42685dd8bae319a Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Tue, 28 Oct 2025 01:06:52 +0200 Subject: [PATCH] fix(coderd/database/dbfake): use transaction for workspace builder (#20506) While investigating a flake I noticed that the dbfake workspace builder executes all database inserts without a transaction. Since our real wsbuilder implementation utilizes one it makes sense to do here as well. For example, our normal workspace <-> build relationship is such that a workspace cannot exist with at least one build. However, our GetWorkspaces query left joins workspace builds but has types that are non-nullable, leading to flakes like coder/internal#1103. --- coderd/database/dbfake/dbfake.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/coderd/database/dbfake/dbfake.go b/coderd/database/dbfake/dbfake.go index cf94812cfd..12c1a28d98 100644 --- a/coderd/database/dbfake/dbfake.go +++ b/coderd/database/dbfake/dbfake.go @@ -161,6 +161,19 @@ func (b WorkspaceBuildBuilder) Canceled() WorkspaceBuildBuilder { // Workspace will be optionally populated if no ID is set on the provided // workspace. func (b WorkspaceBuildBuilder) Do() WorkspaceResponse { + var resp WorkspaceResponse + // Use transaction, like real wsbuilder. + err := b.db.InTx(func(tx database.Store) error { + //nolint:revive // calls do on modified struct + b.db = tx + resp = b.doInTX() + return nil + }, nil) + require.NoError(b.t, err) + return resp +} + +func (b WorkspaceBuildBuilder) doInTX() WorkspaceResponse { b.t.Helper() jobID := uuid.New() b.seed.ID = uuid.New()