Skip to content

refactor(service): RecomputeWorkspaceSize takes ID, writes only size_bytes#353

Open
Adam-D-Lewis wants to merge 2 commits into
mainfrom
refactor/workspace-size-targeted-update
Open

refactor(service): RecomputeWorkspaceSize takes ID, writes only size_bytes#353
Adam-D-Lewis wants to merge 2 commits into
mainfrom
refactor/workspace-size-targeted-update

Conversation

@Adam-D-Lewis

@Adam-D-Lewis Adam-D-Lewis commented May 27, 2026

Copy link
Copy Markdown
Member

Summary

Follow-up to #306. That PR fixed the symptom of #294 (workspace path clobbered after creation) by syncing the in-memory struct before db.Save(ws) runs. This PR removes the underlying footgun so the class of bug can't recur for any other column.

Two changes

1. Column-scoped write. UpdateWorkspaceSize previously called db.Save(ws) — a full-row write driven by the in-memory struct. If anything else updated the row in the DB between ws being loaded at job start and this call (e.g. SetWorkspacePath, a future targeted update on another column, a concurrent admin write), the stale value on ws overwrote it. Switched to db.Model(...).Where("id = ?", wsID).Update("size_bytes", sizeBytes) so the function writes only the column it owns.

2. Signature + rename to match the rest of the file. SetWorkspaceStatus(wsID, status) and SetWorkspacePath(wsID, path) take an ID and return an error. The old UpdateWorkspaceSize(ws *Workspace) took a full struct and silently dropped errors. Now RecomputeWorkspaceSize(wsID uuid.UUID) error:

  • Loads the workspace fresh inside the function — callers no longer maintain an up-to-date struct.
  • Returns the error to the caller.
  • Renamed because "Update" reads like a pure setter; "Recompute" better captures that the function measures the directory and persists the result.

The five worker callsites now log a warning on failure, matching the existing pattern around SyncPackagesFromWorkspace and CreateVersionSnapshot.

Impact on #306

The in-memory ws.Path = resolvedPath line added there becomes defense-in-depth rather than load-bearing — the targeted update means RecomputeWorkspaceSize cannot clobber path (or any other column) even with a stale struct. Worth keeping for in-memory/DB consistency, but no longer the only thing preventing the regression.

How to test

go test ./internal/service/... ./internal/worker/... — all existing tests pass, including the #294 regression test added in #306. go build ./... clean.

db.Save(ws) writes every column from the in-memory struct, so any field
that diverged from the DB between job-start load and this call gets
clobbered. #294 was caused by exactly this pattern overwriting the path
column with "" — fixed in #306 with an in-memory sync, but the underlying
footgun (full-row save of a stale struct) remained.

Switch to a column-scoped UPDATE on size_bytes so the function only
writes the column it owns, and check the error (previously discarded).
Set ws.SizeBytes in-memory only after the write succeeds.
@netlify

netlify Bot commented May 27, 2026

Copy link
Copy Markdown

Deploy Preview for nebi-docs canceled.

Name Link
🔨 Latest commit 79635bb
🔍 Latest deploy log https://app.netlify.com/projects/nebi-docs/deploys/6a17212597fed10008db8cb5

Match the SetWorkspace{Status,Path} shape: take wsID instead of a
*models.Workspace, load fresh inside the function, and return an error.
The function is fundamentally a read-compute-write (it measures the
on-disk directory then writes one column), so an internal fresh load
makes it self-contained — callers can no longer pass a stale struct
that ends up clobbering anything.

Renamed from UpdateWorkspaceSize because "Update" reads like a pure
setter; "Recompute" better describes that the function measures the
size and persists the result.

All 5 worker callsites now log warnings on failure, matching the
existing pattern around SyncPackagesFromWorkspace / CreateVersionSnapshot.
@Adam-D-Lewis Adam-D-Lewis changed the title refactor(service): use targeted UPDATE in UpdateWorkspaceSize refactor(service): RecomputeWorkspaceSize takes ID, writes only size_bytes May 27, 2026
@Adam-D-Lewis Adam-D-Lewis requested a review from viniciusdc May 27, 2026 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant