MCPcopy Index your code
hub / github.com/bytebase/bytebase / BatchUpdateDatabases

Method BatchUpdateDatabases

backend/store/database.go:397–453  ·  view source on GitHub ↗

BatchUpdateDatabases update databases in batch.

(ctx context.Context, databases []*DatabaseMessage, update *BatchUpdateDatabases)

Source from the content-addressed store, hash-verified

395
396// BatchUpdateDatabases update databases in batch.
397func (s *Store) BatchUpdateDatabases(ctx context.Context, databases []*DatabaseMessage, update *BatchUpdateDatabases) error {
398 set := qb.Q()
399
400 if update.ProjectID != nil {
401 set.Comma("project = ?", *update.ProjectID)
402 }
403 if v := update.EnvironmentID; v != nil {
404 if *v == "" {
405 set.Comma("environment = NULL")
406 } else {
407 set.Comma("environment = ?", *v)
408 }
409 }
410 if set.Len() == 0 {
411 return errors.New("no update field specified")
412 }
413
414 where := qb.Q()
415
416 if v := update.FindByEnvironmentID; v != nil {
417 where.Or("environment = ?", *v)
418 }
419
420 if len(databases) > 0 {
421 var dbInstances, dbNames []string
422 for _, database := range databases {
423 dbInstances = append(dbInstances, database.InstanceID)
424 dbNames = append(dbNames, database.DatabaseName)
425 }
426 where.Or(`(db.instance, db.name) IN (SELECT * FROM unnest(?::TEXT[], ?::TEXT[]))`, dbInstances, dbNames)
427 }
428
429 if where.Len() == 0 {
430 return errors.Errorf("empty where")
431 }
432
433 if update.Workspace != "" {
434 where.And("db.instance IN (SELECT resource_id FROM instance WHERE workspace = ?)", update.Workspace)
435 }
436
437 q := qb.Q().Space("UPDATE db SET ? WHERE ?", set, where)
438
439 query, args, err := q.ToSQL()
440 if err != nil {
441 return errors.Wrapf(err, "failed to build sql")
442 }
443
444 if _, err := s.GetDB().ExecContext(ctx, query, args...); err != nil {
445 return err
446 }
447
448 // Invalidate cache for updated databases
449 for _, database := range databases {
450 s.removeDatabaseCache(ctx, database.InstanceID, database.DatabaseName)
451 }
452 return nil
453}
454

Callers

nothing calls this directly

Calls 10

GetDBMethod · 0.95
removeDatabaseCacheMethod · 0.95
QFunction · 0.92
CommaMethod · 0.80
LenMethod · 0.80
OrMethod · 0.80
ErrorfMethod · 0.80
AndMethod · 0.80
SpaceMethod · 0.80
ToSQLMethod · 0.80

Tested by

no test coverage detected