github.com/blend/go-sdk@v1.20220411.3/testutil/default_dbs.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package testutil
     9  
    10  import (
    11  	"database/sql"
    12  
    13  	"github.com/blend/go-sdk/db"
    14  )
    15  
    16  var (
    17  	_defaultDBs []*db.Connection
    18  )
    19  
    20  // DefaultDBs returns a default set database connections for tests.
    21  func DefaultDBs() []*db.Connection {
    22  	return _defaultDBs
    23  }
    24  
    25  // BeginAll begins a transaction in each of the underlying connections.
    26  // If an error is raised by *any* of the connections, t
    27  func BeginAll() ([]*sql.Tx, error) {
    28  	var output []*sql.Tx
    29  	for x := 0; x < len(_defaultDBs); x++ {
    30  		tx, err := _defaultDBs[x].Begin()
    31  		if err != nil {
    32  			for _, inFlight := range output {
    33  				func() { _ = inFlight.Rollback() }()
    34  			}
    35  			return nil, err
    36  		}
    37  		output = append(output, tx)
    38  	}
    39  	return output, nil
    40  }
    41  
    42  // RollbackAll calls `Rollback` on a set of transactions.
    43  func RollbackAll(txs ...*sql.Tx) error {
    44  	for _, tx := range txs {
    45  		if err := tx.Rollback(); err != nil {
    46  			return err
    47  		}
    48  	}
    49  	return nil
    50  }