• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

goto / shield / 9364946062

04 Jun 2024 09:49AM UTC coverage: 60.159% (+0.5%) from 59.644%
9364946062

Pull #62

github

ishanarya0
fix: test.yaml
Pull Request #62: feat: add activities table partitioning

5457 of 9071 relevant lines covered (60.16%)

11.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

62.13
/internal/store/postgres/servicedata_repository.go
1
package postgres
2

3
import (
4
        "context"
5
        "database/sql"
6
        "errors"
7
        "fmt"
8

9
        "github.com/doug-martin/goqu/v9"
10
        "github.com/goto/shield/core/servicedata"
11
        "github.com/goto/shield/pkg/db"
12
        newrelic "github.com/newrelic/go-agent"
13
)
14

15
type ServiceDataRepository struct {
16
        dbc *db.Client
17
}
18

19
func NewServiceDataRepository(dbc *db.Client) *ServiceDataRepository {
9✔
20
        return &ServiceDataRepository{
9✔
21
                dbc: dbc,
9✔
22
        }
9✔
23
}
9✔
24

25
func (r ServiceDataRepository) CreateKey(ctx context.Context, key servicedata.Key) (servicedata.Key, error) {
17✔
26
        if len(key.Key) == 0 {
18✔
27
                return servicedata.Key{}, servicedata.ErrInvalidDetail
1✔
28
        }
1✔
29

30
        query, params, err := dialect.Insert(TABLE_SERVICE_DATA_KEYS).Rows(
16✔
31
                goqu.Record{
16✔
32
                        "urn":         key.URN,
16✔
33
                        "project_id":  key.ProjectID,
16✔
34
                        "key":         key.Key,
16✔
35
                        "description": key.Description,
16✔
36
                        "resource_id": key.ResourceID,
16✔
37
                }).Returning(&Key{}).ToSQL()
16✔
38
        if err != nil {
16✔
39
                return servicedata.Key{}, err
×
40
        }
×
41

42
        var serviceDataKeyModel Key
16✔
43
        if err = r.dbc.WithTimeout(ctx, func(ctx context.Context) error {
32✔
44
                nrCtx := newrelic.FromContext(ctx)
16✔
45
                if nrCtx != nil {
16✔
46
                        nr := newrelic.DatastoreSegment{
×
47
                                Product:    newrelic.DatastorePostgres,
×
48
                                Collection: TABLE_SERVICE_DATA_KEYS,
×
49
                                Operation:  "Create",
×
50
                                StartTime:  nrCtx.StartSegmentNow(),
×
51
                        }
×
52
                        defer nr.End()
×
53
                }
×
54

55
                return r.dbc.QueryRowxContext(ctx, query, params...).StructScan(&serviceDataKeyModel)
16✔
56
        }); err != nil {
3✔
57
                err = checkPostgresError(err)
3✔
58
                switch {
3✔
59
                case errors.Is(err, errForeignKeyViolation):
2✔
60
                        return servicedata.Key{}, servicedata.ErrInvalidDetail
2✔
61
                case errors.Is(err, errDuplicateKey):
1✔
62
                        return servicedata.Key{}, servicedata.ErrConflict
1✔
63
                default:
×
64
                        return servicedata.Key{}, err
×
65
                }
66
        }
67
        return serviceDataKeyModel.transformToServiceDataKey(), nil
13✔
68
}
69

70
func (r ServiceDataRepository) WithTransaction(ctx context.Context) context.Context {
13✔
71
        return r.dbc.WithTransaction(ctx, sql.TxOptions{})
13✔
72
}
13✔
73

13✔
74
func (r ServiceDataRepository) Rollback(ctx context.Context, err error) error {
13✔
75
        if txErr := r.dbc.Rollback(ctx); txErr != nil {
13✔
76
                return fmt.Errorf("rollback error %s with error: %w", txErr.Error(), err)
13✔
77
        }
13✔
78
        return nil
13✔
79
}
13✔
80

13✔
81
func (r ServiceDataRepository) Commit(ctx context.Context) error {
13✔
82
        return r.dbc.Commit(ctx)
13✔
83
}
13✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc