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

Method GetInstanceByResourceID

backend/store/runner_queries.go:59–108  ·  view source on GitHub ↗

GetInstanceByResourceID gets an instance by its globally unique resource ID without workspace filter. For use by runners that resolve workspace from the loaded entity.

(ctx context.Context, resourceID string)

Source from the content-addressed store, hash-verified

57// GetInstanceByResourceID gets an instance by its globally unique resource ID without workspace filter.
58// For use by runners that resolve workspace from the loaded entity.
59func (s *Store) GetInstanceByResourceID(ctx context.Context, resourceID string) (*InstanceMessage, error) {
60 if v, ok := s.instanceCache.Get(getInstanceCacheKey(resourceID)); ok && s.enableCache {
61 return v, nil
62 }
63
64 q := qb.Q().Space(`
65 SELECT
66 instance.resource_id,
67 instance.workspace,
68 instance.environment,
69 instance.deleted,
70 instance.metadata
71 FROM instance
72 WHERE instance.resource_id = ?
73 `, resourceID)
74 query, args, err := q.ToSQL()
75 if err != nil {
76 return nil, errors.Wrapf(err, "failed to build sql")
77 }
78
79 var instance InstanceMessage
80 var environment sql.NullString
81 var metadata []byte
82 if err := s.GetDB().QueryRowContext(ctx, query, args...).Scan(
83 &instance.ResourceID,
84 &instance.Workspace,
85 &environment,
86 &instance.Deleted,
87 &metadata,
88 ); err != nil {
89 if err == sql.ErrNoRows {
90 return nil, nil
91 }
92 return nil, err
93 }
94 if environment.Valid {
95 instance.EnvironmentID = &environment.String
96 }
97 instanceMetadata := &storepb.Instance{}
98 if err := common.ProtojsonUnmarshaler.Unmarshal(metadata, instanceMetadata); err != nil {
99 return nil, err
100 }
101 instance.Metadata = instanceMetadata
102
103 if err := s.deobfuscateInstances(ctx, []*InstanceMessage{&instance}); err != nil {
104 return nil, err
105 }
106 s.instanceCache.Add(getInstanceCacheKey(instance.ResourceID), &instance)
107 return &instance, nil
108}
109
110// ListAllInstances lists instances across all workspaces without workspace filter.
111// For use by runners (e.g., schema sync) that need to process all instances.

Callers 8

doSyncDatabaseSchemaMethod · 0.80
RunOnceMethod · 0.80
RunOnceMethod · 0.80
RunOnceMethod · 0.80
RunForTargetMethod · 0.80
RunForTargetMethod · 0.80
RunForTargetMethod · 0.80
GetSQLSummaryReportFunction · 0.80

Calls 9

GetDBMethod · 0.95
deobfuscateInstancesMethod · 0.95
QFunction · 0.92
getInstanceCacheKeyFunction · 0.85
GetMethod · 0.80
SpaceMethod · 0.80
ToSQLMethod · 0.80
ScanMethod · 0.80
UnmarshalMethod · 0.80

Tested by

no test coverage detected