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

mindersec / minder / 14831306943

05 May 2025 07:29AM UTC coverage: 56.912% (+0.006%) from 56.906%
14831306943

Pull #5621

github

web-flow
Merge 9a09bd468 into 416b6047a
Pull Request #5621: build(deps): bump github.com/open-policy-agent/opa from 1.2.0 to 1.4.2

18369 of 32276 relevant lines covered (56.91%)

36.98 hits per line

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

0.0
/internal/datasources/service/tx.go
1
// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors
2
// SPDX-License-Identifier: Apache-2.0
3

4
package service
5

6
import (
7
        "database/sql"
8

9
        "github.com/mindersec/minder/internal/db"
10
)
11

12
// serviceTX is an interface that defines the methods for a service transaction.
13
//
14
// This service may be used with a pre-built transaction or without one.
15
// thus, we need to be able to handle both cases.
16
type serviceTX interface {
17
        Q() db.ExtendQuerier
18
        Commit() error
19
        Rollback() error
20
}
21

22
// This is a handy helper function to optionally begin a transaction if we've already
23
// got one.
24
func beginTx(d *dataSourceService, opts txGetter) (serviceTX, error) {
×
25
        if opts == nil {
×
26
                opts = &Options{}
×
27
        }
×
28
        if opts.getTransaction() != nil {
×
29
                return &externalTX{q: opts.getTransaction()}, nil
×
30
        }
×
31

32
        return beginInternalTx(d, opts)
×
33
}
34

35
// builds a new transaction for the service
36
func beginInternalTx(d *dataSourceService, opts txGetter) (serviceTX, error) {
×
37
        tx, err := d.store.BeginTransaction()
×
38
        if err != nil {
×
39
                return nil, err
×
40
        }
×
41

42
        // If this is a read-only operation we can set the transaction to read-only
43
        // We can know this by casting to the *ReadOptions struct
44
        if _, ok := opts.(*ReadOptions); ok {
×
45
                if _, err := tx.Query("SET TRANSACTION READ ONLY"); err != nil {
×
46
                        return nil, err
×
47
                }
×
48
        }
49

50
        return &internalTX{tx: tx, q: d.store.GetQuerierWithTransaction(tx)}, nil
×
51
}
52

53
type externalTX struct {
54
        q db.ExtendQuerier
55
}
56

57
func (e *externalTX) Q() db.ExtendQuerier {
×
58
        return e.q
×
59
}
×
60

61
func (*externalTX) Commit() error {
×
62
        return nil
×
63
}
×
64

65
func (*externalTX) Rollback() error {
×
66
        return nil
×
67
}
×
68

69
type internalTX struct {
70
        tx *sql.Tx
71
        q  db.ExtendQuerier
72
}
73

74
func (i *internalTX) Q() db.ExtendQuerier {
×
75
        return i.q
×
76
}
×
77

78
func (i *internalTX) Commit() error {
×
79
        return i.tx.Commit()
×
80
}
×
81

82
func (i *internalTX) Rollback() error {
×
83
        return i.tx.Rollback()
×
84
}
×
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