UpdateWorkspace updates workspace payload fields atomically using jsonb_set. Each field is updated independently in SQL to avoid lost-update races.
(ctx context.Context, patch *UpdateWorkspaceMessage)
| 212 | // UpdateWorkspace updates workspace payload fields atomically using jsonb_set. |
| 213 | // Each field is updated independently in SQL to avoid lost-update races. |
| 214 | func (s *Store) UpdateWorkspace(ctx context.Context, patch *UpdateWorkspaceMessage) error { |
| 215 | q := qb.Q().Space("UPDATE workspace SET payload = payload") |
| 216 | if v := patch.Title; v != nil { |
| 217 | q.Space("|| jsonb_build_object('title', ?::text)", *v) |
| 218 | } |
| 219 | if v := patch.Logo; v != nil { |
| 220 | q.Space("|| jsonb_build_object('logo', ?::text)", *v) |
| 221 | } |
| 222 | q.Space("WHERE resource_id = ? AND deleted = FALSE", patch.ResourceID) |
| 223 | query, args, err := q.ToSQL() |
| 224 | if err != nil { |
| 225 | return errors.Wrapf(err, "failed to build sql") |
| 226 | } |
| 227 | if _, err := s.GetDB().ExecContext(ctx, query, args...); err != nil { |
| 228 | return errors.Wrapf(err, "failed to update workspace") |
| 229 | } |
| 230 | return nil |
| 231 | } |
| 232 | |
| 233 | // DeleteWorkspace soft-deletes a workspace by setting deleted = TRUE. |
| 234 | func (s *Store) DeleteWorkspace(ctx context.Context, resourceID string) error { |