mirror of
https://github.com/netbirdio/netbird.git
synced 2026-09-30 18:49:08 +02:00
[management] handle db conn close on errpr (#7740)
This commit is contained in:
@@ -93,7 +93,12 @@ func OpenSqliteFile(ctx context.Context, dataDir, storeFile string) (*Conn, erro
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewConn(ctx, gormDB, SqliteStoreEngine, nil)
|
||||
conn, err := NewConn(ctx, gormDB, SqliteStoreEngine, nil)
|
||||
if err != nil {
|
||||
closeGorm(gormDB)
|
||||
return nil, err
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
// OpenPostgres opens a Postgres database through gorm and a pgx pool sized by pool.
|
||||
@@ -107,7 +112,13 @@ func OpenPostgres(ctx context.Context, dsn string, pool PoolConfig) (*Conn, erro
|
||||
closeGorm(gormDB)
|
||||
return nil, err
|
||||
}
|
||||
return NewConn(ctx, gormDB, PostgresStoreEngine, pgxPool)
|
||||
conn, err := NewConn(ctx, gormDB, PostgresStoreEngine, pgxPool)
|
||||
if err != nil {
|
||||
pgxPool.Close()
|
||||
closeGorm(gormDB)
|
||||
return nil, err
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
// MysqlDSN adds the connection parameters every MySQL handle needs, keeping
|
||||
@@ -126,7 +137,12 @@ func OpenMysql(ctx context.Context, dsn string) (*Conn, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewConn(ctx, gormDB, MysqlStoreEngine, nil)
|
||||
conn, err := NewConn(ctx, gormDB, MysqlStoreEngine, nil)
|
||||
if err != nil {
|
||||
closeGorm(gormDB)
|
||||
return nil, err
|
||||
}
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func newPgxPool(ctx context.Context, dsn string, cfg PoolConfig) (*pgxpool.Pool, error) {
|
||||
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -23,7 +22,6 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/netbirdio/netbird/dns"
|
||||
@@ -716,29 +714,19 @@ func NewTestStoreFromSQL(ctx context.Context, filename string, dataDir string) (
|
||||
kind = types.SqliteStoreEngine
|
||||
}
|
||||
|
||||
storeStr := fmt.Sprintf("%s?cache=shared", db.SqliteFileName)
|
||||
if runtime.GOOS == "windows" {
|
||||
// Vo avoid `The process cannot access the file because it is being used by another process` on Windows
|
||||
storeStr = db.SqliteFileName
|
||||
}
|
||||
|
||||
file := filepath.Join(dataDir, storeStr)
|
||||
gormDB, err := gorm.Open(sqlite.Open(file), db.GormConfig())
|
||||
conn, err := db.OpenSqliteFile(ctx, dataDir, db.SqliteFileName)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, nil, fmt.Errorf("failed to create test store: %v", err)
|
||||
}
|
||||
|
||||
if filename != "" {
|
||||
err = LoadSQL(gormDB, filename)
|
||||
err = LoadSQL(conn.DB(nil), filename)
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, nil, fmt.Errorf("failed to load SQL file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
conn, err := db.NewConn(ctx, gormDB, db.SqliteStoreEngine, nil)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to create test store: %v", err)
|
||||
}
|
||||
store, err := NewSqlStore(ctx, conn, nil, false)
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
@@ -747,6 +735,7 @@ func NewTestStoreFromSQL(ctx context.Context, filename string, dataDir string) (
|
||||
|
||||
err = addAllGroupToAccount(ctx, store)
|
||||
if err != nil {
|
||||
_ = store.Close(ctx)
|
||||
return nil, nil, fmt.Errorf("failed to add all group to account: %v", err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user