MCPcopy Create free account
hub / github.com/bytebase/bytebase / UpdateProjects

Method UpdateProjects

backend/store/project.go:275–324  ·  view source on GitHub ↗

UpdateProjects updates projects in a single transaction.

(ctx context.Context, patches ...*UpdateProjectMessage)

Source from the content-addressed store, hash-verified

273
274// UpdateProjects updates projects in a single transaction.
275func (s *Store) UpdateProjects(ctx context.Context, patches ...*UpdateProjectMessage) error {
276 if len(patches) == 0 {
277 return nil
278 }
279
280 // Remove all projects from cache first
281 for _, patch := range patches {
282 s.removeProjectCache(patch.ResourceID)
283 }
284
285 // Prepare arrays for batch update
286 resourceIDs := make([]string, len(patches))
287 titles := make([]*string, len(patches))
288 deleteds := make([]*bool, len(patches))
289 settings := make([]*string, len(patches))
290
291 for i, patch := range patches {
292 resourceIDs[i] = patch.ResourceID
293 titles[i] = patch.Title
294 deleteds[i] = patch.Delete
295 if patch.Setting != nil {
296 payload, err := protojson.Marshal(patch.Setting)
297 if err != nil {
298 return err
299 }
300 settings[i] = new(string(payload))
301 }
302 }
303
304 // Batch update using UPDATE FROM unnest
305 q := qb.Q().Space(`
306 UPDATE project AS p SET
307 name = COALESCE(u.name, p.name),
308 deleted = COALESCE(u.deleted, p.deleted),
309 setting = COALESCE(u.setting::jsonb, p.setting)
310 FROM unnest(?::text[], ?::text[], ?::bool[], ?::text[]) AS u(resource_id, name, deleted, setting)
311 WHERE p.resource_id = u.resource_id AND p.workspace = ?
312 `, resourceIDs, titles, deleteds, settings, patches[0].Workspace)
313
314 query, args, err := q.ToSQL()
315 if err != nil {
316 return err
317 }
318
319 if _, err := s.GetDB().ExecContext(ctx, query, args...); err != nil {
320 return err
321 }
322
323 return nil
324}
325
326func (s *Store) storeProjectCache(project *ProjectMessage) {
327 s.projectCache.Add(project.ResourceID, project)

Callers 5

UpdateProjectMethod · 0.80
DeleteProjectMethod · 0.80
UndeleteProjectMethod · 0.80
BatchDeleteProjectsMethod · 0.80
UpdateSettingMethod · 0.80

Calls 5

removeProjectCacheMethod · 0.95
GetDBMethod · 0.95
QFunction · 0.92
SpaceMethod · 0.80
ToSQLMethod · 0.80

Tested by

no test coverage detected