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

Method UpsertSubscription

backend/store/subscription.go:26–49  ·  view source on GitHub ↗

UpsertSubscription creates or updates a subscription by workspace.

(ctx context.Context, workspace string, payload *storepb.SubscriptionPayload)

Source from the content-addressed store, hash-verified

24
25// UpsertSubscription creates or updates a subscription by workspace.
26func (s *Store) UpsertSubscription(ctx context.Context, workspace string, payload *storepb.SubscriptionPayload) (*SubscriptionMessage, error) {
27 p, err := protojson.Marshal(payload)
28 if err != nil {
29 return nil, errors.Wrapf(err, "failed to marshal payload")
30 }
31
32 sub := &SubscriptionMessage{
33 Payload: &storepb.SubscriptionPayload{},
34 }
35 var payloadBytes []byte
36 if err := s.GetDB().QueryRowContext(ctx,
37 `INSERT INTO subscription (workspace, payload) VALUES ($1, $2)
38 ON CONFLICT (workspace) DO UPDATE SET payload = $2, updated_at = now()
39 RETURNING workspace, payload, created_at, updated_at`,
40 workspace, p,
41 ).Scan(&sub.Workspace, &payloadBytes, &sub.CreatedAt, &sub.UpdatedAt); err != nil {
42 return nil, errors.Wrapf(err, "failed to upsert subscription")
43 }
44 if err := common.ProtojsonUnmarshaler.Unmarshal(payloadBytes, sub.Payload); err != nil {
45 return nil, errors.Wrapf(err, "failed to unmarshal payload")
46 }
47 sub.Etag = fmt.Sprintf("%d", sub.UpdatedAt.UnixMilli())
48 return sub, nil
49}
50
51// GetSubscriptionByWorkspace gets a subscription by workspace.
52// Returns (nil, nil) if not found.

Callers 3

CancelPurchaseMethod · 0.80
handleInvoicePaidMethod · 0.80

Calls 3

GetDBMethod · 0.95
ScanMethod · 0.80
UnmarshalMethod · 0.80

Tested by

no test coverage detected