Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(projects): preserve omitted fields in concurrent updates (#9111)
2penheimer41c9bbafec3e
diff --git a/server/internal/handler/project.go b/server/internal/handler/project.goindex 5f8690a858..942c4995ee 100644--- a/server/internal/handler/project.go+++ b/server/internal/handler/project.go@@ -503,15 +503,13 @@ func (h *Handler) UpdateProject(w http.ResponseWriter, r *http.Request) {var rawFields map[string]json.RawMessagejson.Unmarshal(bodyBytes, &rawFields)- params := db.UpdateProjectParams{- ID: prevProject.ID,- Description: prevProject.Description,- Icon: prevProject.Icon,- LeadType: prevProject.LeadType,- LeadID: prevProject.LeadID,- StartDate: prevProject.StartDate,- DueDate: prevProject.DueDate,- }+ params := db.UpdateProjectParams{ID: prevProject.ID}+ _, params.DescriptionSet = rawFields["description"]+ _, params.IconSet = rawFields["icon"]+ _, params.LeadTypeSet = rawFields["lead_type"]+ _, params.LeadIDSet = rawFields["lead_id"]+ _, params.StartDateSet = rawFields["start_date"]+ _, params.DueDateSet = rawFields["due_date"]if req.Title != nil {params.Title = pgtype.Text{String: *req.Title, Valid: true}}diff --git a/server/internal/handler/project_partial_update_test.go b/server/internal/handler/project_partial_update_test.gonew file mode 100644index 0000000000..b305246d68--- /dev/null+++ b/server/internal/handler/project_partial_update_test.go@@ -0,0 +1,172 @@+package handler++import (+ "context"+ "net/http"+ "net/http/httptest"+ "reflect"+ "strings"+ "sync"+ "testing"+ "time"++ "github.com/jackc/pgx/v5"+ "github.com/multica-ai/multica/server/internal/testutil"+ db "github.com/multica-ai/multica/server/pkg/db/generated"+)++type projectReadPauseDB struct {+ db.DBTX+ paused, release chan struct{}+}++func (d projectReadPauseDB) QueryRow(ctx context.Context, query string, args ...any) pgx.Row {+ row := d.DBTX.QueryRow(ctx, query, args...)+ if strings.HasPrefix(query, "-- name: GetProjectInWorkspace :one") {+ return &contextLockPauseRow{Row: row, paused: d.paused, release: d.release}+ }+ return row+}++func projectNullableValues(p ProjectResponse) map[string]*string {+ return map[string]*string{+ "description": p.Description, "icon": p.Icon,+ "lead_type": p.LeadType, "lead_id": p.LeadID,+ "start_date": p.StartDate, "due_date": p.DueDate,+ }+}++func TestUpdateProjectConcurrentPartialUpdate(t *testing.T) {+ for _, clear := range []bool{false, true} {+ name := "set"+ if clear {+ name = "clear"+ }+ t.Run(name, func(t *testing.T) {+ projectID := dbfx.Project(t, "original title", testutil.Cols{+ "description": "original description", "icon": "original icon",+ "lead_type": "member", "lead_id": testUserID,+ "start_date": "2026-03-01", "due_date": "2026-03-31",+ })+ paused, release := make(chan struct{}), make(chan struct{})+ var once sync.Once+ resume := func() { once.Do(func() { close(release) }) }+ defer resume()+ h := *testHandler+ h.Queries = db.New(projectReadPauseDB{DBTX: testPool, paused: paused, release: release})+ req := withURLParam(newRequest("PUT", "/api/projects/"+projectID, map[string]any{+ "title": "updated title",+ }), "id", projectID)+ ctx, cancel := context.WithTimeout(req.Context(), 15*time.Second)+ defer cancel()+ req = req.WithContext(ctx)+ rec := httptest.NewRecorder()+ done := make(chan error, 1)+ go func() {+ h.UpdateProject(rec, req)+ done <- nil+ }()+ waitContextLockSignal(t, paused, "title update did not read project")+ changes := map[string]any{+ "description": "new description", "icon": "new icon",+ "lead_type": "agent", "lead_id": dbfx.Agent(t, "project lead", testRuntimeID),+ "start_date": "2026-04-01", "due_date": "2026-04-30",+ }+ if clear {+ for field := range changes {+ changes[field] = nil+ }+ }+ var competing ProjectResponse+ testutil.Call(t, testHandler.UpdateProject, withURLParam(newRequest("PUT", "/api/projects/"+projectID, changes), "id", projectID)).Want(http.StatusOK).JSON(&competing)+ resume()+ waitContextLockResult(t, done, "title-only update")+ updated := decodeProject(t, rec, http.StatusOK)+ stored, err := testHandler.Queries.GetProjectInWorkspace(ctx, db.GetProjectInWorkspaceParams{+ ID: parseUUID(projectID), WorkspaceID: parseUUID(testWorkspaceID),+ })+ if err != nil {+ t.Fatal(err)+ }+ for label, p := range map[string]ProjectResponse{"response": updated, "database": projectToResponse(stored)} {+ if p.Title != "updated title" {+ t.Errorf("%s title = %q, want updated title", label, p.Title)+ }+ for field, want := range projectNullableValues(competing) {+ got := projectNullableValues(p)[field]+ if !reflect.DeepEqual(got, want) {+ t.Errorf("%s lost concurrent %s update: got %v, want %v", label, field, got, want)+ }+ }+ }+ })+ }+}++func TestUpdateProjectNullableFieldPresence(t *testing.T) {+ agentID := dbfx.Agent(t, "project lead", testRuntimeID)+ initial := map[string]string{+ "description": "original description", "icon": "original icon",+ "lead_type": "member", "lead_id": testUserID,+ "start_date": "2026-03-01", "due_date": "2026-03-31",+ }+ values := map[string]string{+ "description": "new description", "icon": "new icon",+ "lead_type": "agent", "lead_id": agentID,+ "start_date": "2026-04-01", "due_date": "2026-04-30",+ }+ for field, value := range values {+ modes := []string{"omitted", "set", "null"}+ if field == "start_date" || field == "due_date" {+ modes = append(modes, "empty")+ }+ for _, mode := range modes {+ t.Run(field+"/"+mode, func(t *testing.T) {+ cols := testutil.Cols{}+ for key, value := range initial {+ cols[key] = value+ }+ projectID := dbfx.Project(t, "original title", cols)+ body := map[string]any{"title": "updated title", "status": nil, "priority": nil}+ switch mode {+ case "set":+ body[field] = value+ case "null":+ body[field] = nil+ case "empty":+ body[field] = ""+ }+ var updated ProjectResponse+ testutil.Call(t, testHandler.UpdateProject, withURLParam(newRequest("PUT", "/api/projects/"+projectID, body), "id", projectID)).Want(http.StatusOK).JSON(&updated)+ stored, err := testHandler.Queries.GetProjectInWorkspace(context.Background(), db.GetProjectInWorkspaceParams{+ ID: parseUUID(projectID), WorkspaceID: parseUUID(testWorkspaceID),+ })+ if err != nil {+ t.Fatal(err)+ }+ for label, p := range map[string]ProjectResponse{"response": updated, "database": projectToResponse(stored)} {+ if p.Title != "updated title" || p.Status != "planned" || p.Priority != "none" {+ t.Errorf("%s nonnullable fields = (%q, %q, %q)", label, p.Title, p.Status, p.Priority)+ }+ for key, got := range projectNullableValues(p) {+ want := initial[key]+ if key == field {+ if mode == "null" || mode == "empty" {+ if got != nil {+ t.Errorf("%s %s = %q, want null", label, key, *got)+ }+ continue+ }+ if mode == "set" {+ want = value+ }+ }+ if got == nil || *got != want {+ t.Errorf("%s %s = %v, want %q", label, key, got, want)+ }+ }+ }+ })+ }+ }+}diff --git a/server/pkg/db/generated/project.sql.go b/server/pkg/db/generated/project.sql.goindex 48dc681017..cde98b6f8f 100644--- a/server/pkg/db/generated/project.sql.go+++ b/server/pkg/db/generated/project.sql.go@@ -256,43 +256,55 @@ func (q *Queries) LockProjectForDelete(ctx context.Context, arg LockProjectForDeconst updateProject = `-- name: UpdateProject :oneUPDATE project SETtitle = COALESCE($2, title),- description = $3,- icon = $4,- status = COALESCE($5, status),- priority = COALESCE($6, priority),- lead_type = $7,- lead_id = $8,- start_date = $9,- due_date = $10,+ description = CASE WHEN $3::boolean THEN $4 ELSE description END,+ icon = CASE WHEN $5::boolean THEN $6 ELSE icon END,+ status = COALESCE($7, status),+ priority = COALESCE($8, priority),+ lead_type = CASE WHEN $9::boolean THEN $10 ELSE lead_type END,+ lead_id = CASE WHEN $11::boolean THEN $12 ELSE lead_id END,+ start_date = CASE WHEN $13::boolean THEN $14 ELSE start_date END,+ due_date = CASE WHEN $15::boolean THEN $16 ELSE due_date END,updated_at = now()WHERE id = $1RETURNING id, workspace_id, title, description, icon, status, lead_type, lead_id, created_at, updated_at, priority, start_date, due_date`type UpdateProjectParams struct {- ID pgtype.UUID `json:"id"`- Title pgtype.Text `json:"title"`- Description pgtype.Text `json:"description"`- Icon pgtype.Text `json:"icon"`- Status pgtype.Text `json:"status"`- Priority pgtype.Text `json:"priority"`- LeadType pgtype.Text `json:"lead_type"`- LeadID pgtype.UUID `json:"lead_id"`- StartDate pgtype.Date `json:"start_date"`- DueDate pgtype.Date `json:"due_date"`+ ID pgtype.UUID `json:"id"`+ Title pgtype.Text `json:"title"`+ DescriptionSet bool `json:"description_set"`+ Description pgtype.Text `json:"description"`+ IconSet bool `json:"icon_set"`+ Icon pgtype.Text `json:"icon"`+ Status pgtype.Text `json:"status"`+ Priority pgtype.Text `json:"priority"`+ LeadTypeSet bool `json:"lead_type_set"`+ LeadType pgtype.Text `json:"lead_type"`+ LeadIDSet bool `json:"lead_id_set"`+ LeadID pgtype.UUID `json:"lead_id"`+ StartDateSet bool `json:"start_date_set"`+ StartDate pgtype.Date `json:"start_date"`+ DueDateSet bool `json:"due_date_set"`+ DueDate pgtype.Date `json:"due_date"`}func (q *Queries) UpdateProject(ctx context.Context, arg UpdateProjectParams) (Project, error) {row := q.db.QueryRow(ctx, updateProject,arg.ID,arg.Title,+ arg.DescriptionSet,arg.Description,+ arg.IconSet,arg.Icon,arg.Status,arg.Priority,+ arg.LeadTypeSet,arg.LeadType,+ arg.LeadIDSet,arg.LeadID,+ arg.StartDateSet,arg.StartDate,+ arg.DueDateSet,arg.DueDate,)var i Projectdiff --git a/server/pkg/db/queries/project.sql b/server/pkg/db/queries/project.sqlindex faca704451..cdb5a46bba 100644--- a/server/pkg/db/queries/project.sql+++ b/server/pkg/db/queries/project.sql@@ -34,14 +34,14 @@ INSERT INTO project (-- name: UpdateProject :oneUPDATE project SETtitle = COALESCE(sqlc.narg('title'), title),- description = sqlc.narg('description'),- icon = sqlc.narg('icon'),+ description = CASE WHEN sqlc.arg('description_set')::boolean THEN sqlc.narg('description') ELSE description END,+ icon = CASE WHEN sqlc.arg('icon_set')::boolean THEN sqlc.narg('icon') ELSE icon END,status = COALESCE(sqlc.narg('status'), status),priority = COALESCE(sqlc.narg('priority'), priority),- lead_type = sqlc.narg('lead_type'),- lead_id = sqlc.narg('lead_id'),- start_date = sqlc.narg('start_date'),- due_date = sqlc.narg('due_date'),+ lead_type = CASE WHEN sqlc.arg('lead_type_set')::boolean THEN sqlc.narg('lead_type') ELSE lead_type END,+ lead_id = CASE WHEN sqlc.arg('lead_id_set')::boolean THEN sqlc.narg('lead_id') ELSE lead_id END,+ start_date = CASE WHEN sqlc.arg('start_date_set')::boolean THEN sqlc.narg('start_date') ELSE start_date END,+ due_date = CASE WHEN sqlc.arg('due_date_set')::boolean THEN sqlc.narg('due_date') ELSE due_date END,updated_at = now()WHERE id = $1RETURNING *;