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

Method UpdateInstance

backend/store/instance.go:222–285  ·  view source on GitHub ↗

UpdateInstance updates an instance.

(ctx context.Context, patch *UpdateInstanceMessage)

Source from the content-addressed store, hash-verified

220
221// UpdateInstance updates an instance.
222func (s *Store) UpdateInstance(ctx context.Context, patch *UpdateInstanceMessage) (*InstanceMessage, error) {
223 set := qb.Q()
224
225 if v := patch.EnvironmentID; v != nil {
226 if *v == "" {
227 // Unset the environment by setting it to NULL
228 set.Comma("environment = ?", nil)
229 } else {
230 set.Comma("environment = ?", *v)
231 }
232 }
233 if v := patch.Deleted; v != nil {
234 set.Comma("deleted = ?", *v)
235 }
236 if v := patch.Metadata; v != nil {
237 redacted, err := s.obfuscateInstance(ctx, v)
238 if err != nil {
239 return nil, err
240 }
241 metadata, err := protojson.Marshal(redacted)
242 if err != nil {
243 return nil, err
244 }
245 set.Comma("metadata = ?", metadata)
246 }
247
248 if set.Len() == 0 {
249 return nil, errors.New("no update field specified")
250 }
251
252 where := qb.Q()
253 if v := patch.ResourceID; v != nil {
254 where.And("resource_id = ?", *v)
255 }
256 if v := patch.FindByEnvironmentID; v != nil {
257 where.And("environment = ?", *v)
258 }
259 if patch.Workspace != "" {
260 where.And("workspace = ?", patch.Workspace)
261 }
262
263 if where.Len() == 0 {
264 return nil, errors.Errorf("empty where")
265 }
266
267 q := qb.Q().Space("UPDATE instance SET ?", set).
268 Space("WHERE ?", where)
269
270 query, args, err := q.ToSQL()
271 if err != nil {
272 return nil, errors.Wrapf(err, "failed to build sql")
273 }
274
275 if _, err := s.GetDB().ExecContext(ctx, query, args...); err != nil {
276 return nil, err
277 }
278
279 if v := patch.ResourceID; v != nil {

Callers

nothing calls this directly

Calls 11

obfuscateInstanceMethod · 0.95
GetDBMethod · 0.95
GetInstanceMethod · 0.95
QFunction · 0.92
getInstanceCacheKeyFunction · 0.85
CommaMethod · 0.80
LenMethod · 0.80
AndMethod · 0.80
ErrorfMethod · 0.80
SpaceMethod · 0.80
ToSQLMethod · 0.80

Tested by

no test coverage detected