UpsertSubscription creates or updates a subscription by workspace.
(ctx context.Context, workspace string, payload *storepb.SubscriptionPayload)
| 24 | |
| 25 | // UpsertSubscription creates or updates a subscription by workspace. |
| 26 | func (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. |
no test coverage detected