Skip to content

Commit

Permalink
patch[persistence]: Optionally disable DB Transactions
Browse files Browse the repository at this point in the history
When defining a DB connection manager, allow the caller to optionally disable transactions.
  • Loading branch information
alwitt committed Jun 6, 2024
1 parent ae5bb72 commit ad33d18
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bin/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func DefineControlNode(
return nil, err
}

dbConns, err := db.NewSQLConnection(sqlDSN, logger.Error)
dbConns, err := db.NewSQLConnection(sqlDSN, logger.Error, false)
if err != nil {
log.WithError(err).WithFields(logTags).Error("Failed to define SQL connection manager")
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion bin/edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func DefineEdgeNode(

// Setup database connection manager
sqlDSN := db.GetSqliteDialector(config.Sqlite.DBFile)
dbConns, err := db.NewSQLConnection(sqlDSN, logger.Error)
dbConns, err := db.NewSQLConnection(sqlDSN, logger.Error, true)
if err != nil {
log.WithError(err).WithFields(logTags).Error("Failed to define SQL connection manager")
return theNode, err
Expand Down
2 changes: 1 addition & 1 deletion bin/util/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ func purgeUnknownSegmentsFromObjStore(c *cli.Context) error {
return err
}

dbConns, err := db.NewSQLConnection(sqlDSN, logger.Error)
dbConns, err := db.NewSQLConnection(sqlDSN, logger.Error, false)
if err != nil {
log.WithError(err).WithFields(logTags).Error("Failed to define SQL connection manager")
return err
Expand Down
21 changes: 15 additions & 6 deletions db/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,20 @@ type ConnectionManager interface {

type connectionManagerImpl struct {
goutils.Component
db *gorm.DB
db *gorm.DB
noTransactions bool
}

/*
NewSQLConnection define a new DB connection and transactions manager
@param dbDialector gorm.Dialector - GORM SQL dialector
@param logLevel logger.LogLevel - SQL log level
@param noTransactions bool - optionally, do not use transactions.
@returns new manager
*/
func NewSQLConnection(
dbDialector gorm.Dialector, logLevel logger.LogLevel,
dbDialector gorm.Dialector, logLevel logger.LogLevel, noTransactions bool,
) (ConnectionManager, error) {
db, err := gorm.Open(dbDialector, &gorm.Config{
Logger: logger.Default.LogMode(logLevel),
Expand Down Expand Up @@ -146,20 +148,27 @@ func NewSQLConnection(
LogTagModifiers: []goutils.LogMetadataModifier{
goutils.ModifyLogMetadataByRestRequestParam,
},
}, db: db,
}, db: db, noTransactions: noTransactions,
}, nil
}

func (c *connectionManagerImpl) NewTransaction() *gorm.DB {
return c.db.Begin()
if !c.noTransactions {
return c.db.Begin()
}
return c.db
}

func (c *connectionManagerImpl) Commit(session *gorm.DB) {
session.Commit()
if !c.noTransactions {
session.Commit()
}
}

func (c *connectionManagerImpl) Rollback(session *gorm.DB) {
session.Rollback()
if !c.noTransactions {
session.Rollback()
}
}

func (c *connectionManagerImpl) NewPersistanceManager() PersistenceManager {
Expand Down
10 changes: 5 additions & 5 deletions db/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestDBManagerVideoSource(t *testing.T) {

testInstance := fmt.Sprintf("ut-%s", uuid.NewString())
testDB := fmt.Sprintf("/tmp/%s.db", testInstance)
conns, err := db.NewSQLConnection(db.GetSqliteDialector(testDB), logger.Info)
conns, err := db.NewSQLConnection(db.GetSqliteDialector(testDB), logger.Info, true)
assert.Nil(err)

log.Debugf("Using %s", testDB)
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestDBManagerVideoSegment(t *testing.T) {

testInstance := fmt.Sprintf("ut-%s", uuid.NewString())
testDB := fmt.Sprintf("/tmp/%s.db", testInstance)
conns, err := db.NewSQLConnection(db.GetSqliteDialector(testDB), logger.Info)
conns, err := db.NewSQLConnection(db.GetSqliteDialector(testDB), logger.Info, true)
assert.Nil(err)

log.Debugf("Using %s", testDB)
Expand Down Expand Up @@ -477,7 +477,7 @@ func TestDBManagerVideoSegmentPurgeOldSegments(t *testing.T) {

testInstance := fmt.Sprintf("ut-%s", uuid.NewString())
testDB := fmt.Sprintf("/tmp/%s.db", testInstance)
conns, err := db.NewSQLConnection(db.GetSqliteDialector(testDB), logger.Info)
conns, err := db.NewSQLConnection(db.GetSqliteDialector(testDB), logger.Info, true)
assert.Nil(err)

log.Debugf("Using %s", testDB)
Expand Down Expand Up @@ -569,7 +569,7 @@ func TestDBManagerVideoRecording(t *testing.T) {

testInstance := fmt.Sprintf("ut-%s", uuid.NewString())
testDB := fmt.Sprintf("/tmp/%s.db", testInstance)
conns, err := db.NewSQLConnection(db.GetSqliteDialector(testDB), logger.Info)
conns, err := db.NewSQLConnection(db.GetSqliteDialector(testDB), logger.Info, true)
assert.Nil(err)

log.Debugf("Using %s", testDB)
Expand Down Expand Up @@ -804,7 +804,7 @@ func TestDBManagerRecordingSegments(t *testing.T) {
sqlDialector, err := db.GetPostgresDialector(getUnitTestPSQLConfig(assert))
assert.Nil(err)

conns, err := db.NewSQLConnection(sqlDialector, logger.Info)
conns, err := db.NewSQLConnection(sqlDialector, logger.Info, false)
assert.Nil(err)

utCtxt := context.Background()
Expand Down
2 changes: 1 addition & 1 deletion tracker/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestSourceHLSMonitor(t *testing.T) {

testInstance := fmt.Sprintf("ut-%s", uuid.NewString())
testDB := fmt.Sprintf("/tmp/%s.db", testInstance)
conns, err := db.NewSQLConnection(db.GetSqliteDialector(testDB), logger.Info)
conns, err := db.NewSQLConnection(db.GetSqliteDialector(testDB), logger.Info, true)
assert.Nil(err)

var testSource common.VideoSource
Expand Down

0 comments on commit ad33d18

Please sign in to comment.