diff --git a/management/server/store/sql_store.go b/management/server/store/sql_store.go index 5e103dfd69c..e67ef585b8a 100644 --- a/management/server/store/sql_store.go +++ b/management/server/store/sql_store.go @@ -86,12 +86,6 @@ func NewSqlStore(ctx context.Context, db *gorm.DB, storeEngine Engine, metrics t sql.SetMaxOpenConns(conns) - if storeEngine == MysqlStoreEngine { - sql.SetConnMaxLifetime(time.Minute * 2) - sql.SetConnMaxIdleTime(time.Minute * 2) - sql.SetMaxIdleConns(conns) - } - log.WithContext(ctx).Infof("Set max open db connections to %d", conns) if err := migrate(ctx, db); err != nil { @@ -974,10 +968,9 @@ func NewMysqlStore(ctx context.Context, dsn string, metrics telemetry.AppMetrics func getGormConfig() *gorm.Config { return &gorm.Config{ - Logger: logger.Default.LogMode(logger.Silent), - CreateBatchSize: 400, - PrepareStmt: true, - SkipDefaultTransaction: true, + Logger: logger.Default.LogMode(logger.Silent), + CreateBatchSize: 400, + PrepareStmt: true, } } diff --git a/management/server/testutil/store.go b/management/server/testutil/store.go index e96960562d7..4b8d77d0f99 100644 --- a/management/server/testutil/store.go +++ b/management/server/testutil/store.go @@ -11,6 +11,7 @@ import ( "runtime" "time" + log "github.com/sirupsen/logrus" "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/modules/mysql" "github.com/testcontainers/testcontainers-go/modules/postgres" @@ -19,23 +20,12 @@ import ( var ( mysqlContainerConfigPath = "../testdata/mysql.cnf" - mysqlContainer = (*mysql.MySQLContainer)(nil) - postgresContainer = (*postgres.PostgresContainer)(nil) - mysqlConnStr = "" - postgresConnStr = "" ) -func emptyCleanup() { return } - // CreateMysqlTestContainer creates a new MySQL container for testing. func CreateMysqlTestContainer() (func(), error) { ctx := context.Background() - if mysqlContainer != nil && mysqlContainer.IsRunning() { - refreshMysqlDatabase(ctx) - return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_MYSQL_DSN", mysqlConnStr) - } - _, caller, _, ok := runtime.Caller(0) if !ok { return nil, fmt.Errorf("failed to get caller information") @@ -52,25 +42,25 @@ func CreateMysqlTestContainer() (func(), error) { return nil, err } + cleanUp := func() { + timeout := 2 * time.Second + if err = container.Stop(ctx, &timeout); err != nil { + log.WithContext(ctx).Warnf("failed to stop container: %s", err) + } + } + talksConn, err := container.ConnectionString(ctx) if err != nil { return nil, err } - mysqlContainer = container - mysqlConnStr = talksConn - return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_MYSQL_DSN", talksConn) + return cleanUp, os.Setenv("NETBIRD_STORE_ENGINE_MYSQL_DSN", talksConn) } // CreatePostgresTestContainer creates a new PostgreSQL container for testing. func CreatePostgresTestContainer() (func(), error) { ctx := context.Background() - if postgresContainer != nil && postgresContainer.IsRunning() { - refreshPostgresDatabase(ctx) - return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_POSTGRES_DSN", postgresConnStr) - } - container, err := postgres.RunContainer(ctx, testcontainers.WithImage("postgres:16-alpine"), postgres.WithDatabase("netbird"), @@ -85,22 +75,17 @@ func CreatePostgresTestContainer() (func(), error) { return nil, err } + cleanUp := func() { + timeout := 2 * time.Second + if err = container.Stop(ctx, &timeout); err != nil { + log.WithContext(ctx).Warnf("failed to stop container: %s", err) + } + } + talksConn, err := container.ConnectionString(ctx) if err != nil { return nil, err } - postgresContainer = container - postgresConnStr = talksConn - - return emptyCleanup, os.Setenv("NETBIRD_STORE_ENGINE_POSTGRES_DSN", talksConn) -} - -func refreshMysqlDatabase(ctx context.Context) { - _, _, _ = mysqlContainer.Exec(ctx, []string{"mysqladmin", "--user=root", "drop", "netbird", "-f"}) - _, _, _ = mysqlContainer.Exec(ctx, []string{"mysqladmin", "--user=root", "create", "netbird"}) -} -func refreshPostgresDatabase(ctx context.Context) { - _, _, _ = postgresContainer.Exec(ctx, []string{"dropdb", "-f", "netbird"}) - _, _, _ = postgresContainer.Exec(ctx, []string{"createdb", "netbird"}) + return cleanUp, os.Setenv("NETBIRD_STORE_ENGINE_POSTGRES_DSN", talksConn) }