Path: blob/main/components/gitpod-db/go/dbtest/conn.go
2500 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package dbtest56import (7"net"8"os"9"sync"10"testing"1112db "github.com/gitpod-io/gitpod/components/gitpod-db/go"13"github.com/stretchr/testify/require"14"gorm.io/gorm"15)1617var (18connLock = sync.Mutex{}19conn *gorm.DB20)2122func ConnectForTests(t *testing.T) *gorm.DB {23t.Helper()2425connLock.Lock()26defer connLock.Unlock()2728if conn != nil {29return conn30}3132// These are static connection details for tests, started by `leeway build components/gitpod-db/go:init-testdb`.33// We use the same static credentials for CI & local instance of MySQL Server.34var err error35conn, err = db.Connect(db.ConnectionParams{36User: "root",37Password: "test",38Host: net.JoinHostPort(os.Getenv("DB_HOST"), "23306"),39Database: "gitpod",40})41require.NoError(t, err, "Failed to establish connection to In a workspace, run `leeway run components/gitpod-db:init-testdb` once to bootstrap the db")4243return conn44}454647