Skip to content

Commit ef87b66

Browse files
committed
[db-go] Establish database connection
1 parent 308601a commit ef87b66

File tree

8 files changed

+305
-1
lines changed

8 files changed

+305
-1
lines changed

components/usage/BUILD.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ packages:
1313
config:
1414
packaging: app
1515
buildCommand: ["go", "build", "-trimpath", "-ldflags", "-buildid= -w -s -X 'github.com/gitpod-io/gitpod/usage/cmd.Version=commit-${__git_commit}'"]
16+
17+
- name: init-testdb
18+
type: generic
19+
deps:
20+
- components/gitpod-db:dbtest-init
21+
ephemeral: true
22+
23+
- name: load-testdata
24+
type: generic
25+
config:
26+
commands:
27+
- [ "sh", "-c", "mysql -h 127.0.0.1 --port 23306 -u root -ptest gitpod < testdata/workspaces.sql" ]
28+
1629
- name: docker
1730
type: docker
1831
deps:

components/usage/go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ replace k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.23.5 /
5656

5757
require (
5858
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
59+
github.com/go-sql-driver/mysql v1.6.0
5960
github.com/spf13/cobra v1.4.0
61+
github.com/stretchr/testify v1.7.0
62+
gorm.io/driver/mysql v1.3.3
63+
gorm.io/gorm v1.23.5
6064
)
6165

6266
require (
@@ -69,6 +73,8 @@ require (
6973
github.com/hashicorp/golang-lru v0.5.1 // indirect
7074
github.com/heptiolabs/healthcheck v0.0.0-20211123025425-613501dd5deb // indirect
7175
github.com/inconshreveable/mousetrap v1.0.0 // indirect
76+
github.com/jinzhu/inflection v1.0.0 // indirect
77+
github.com/jinzhu/now v1.1.4 // indirect
7278
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
7379
github.com/opentracing/opentracing-go v1.2.0 // indirect
7480
github.com/pmezard/go-difflib v1.0.0 // indirect
@@ -78,7 +84,6 @@ require (
7884
github.com/prometheus/procfs v0.7.3 // indirect
7985
github.com/sirupsen/logrus v1.8.1 // indirect
8086
github.com/spf13/pflag v1.0.5 // indirect
81-
github.com/stretchr/testify v1.7.0 // indirect
8287
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
8388
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
8489
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect

components/usage/go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vb
7777
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
7878
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
7979
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
80+
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
81+
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
8082
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
8183
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
8284
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
@@ -148,6 +150,10 @@ github.com/heptiolabs/healthcheck v0.0.0-20211123025425-613501dd5deb/go.mod h1:N
148150
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
149151
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
150152
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
153+
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
154+
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
155+
github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
156+
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
151157
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
152158
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
153159
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
@@ -526,6 +532,11 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
526532
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
527533
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
528534
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
535+
gorm.io/driver/mysql v1.3.3 h1:jXG9ANrwBc4+bMvBcSl8zCfPBaVoPyBEBshA8dA93X8=
536+
gorm.io/driver/mysql v1.3.3/go.mod h1:ChK6AHbHgDCFZyJp0F+BmVGb06PSIoh9uVYKAlRbb2U=
537+
gorm.io/gorm v1.23.1/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
538+
gorm.io/gorm v1.23.5 h1:TnlF26wScKSvknUC/Rn8t0NLLM22fypYBlvj1+aH6dM=
539+
gorm.io/gorm v1.23.5/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
529540
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
530541
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
531542
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

components/usage/pkg/db/conn.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package db
6+
7+
import (
8+
"fmt"
9+
driver_mysql "github.com/go-sql-driver/mysql"
10+
"github.com/stretchr/testify/require"
11+
"gorm.io/driver/mysql"
12+
"gorm.io/gorm"
13+
"testing"
14+
"time"
15+
)
16+
17+
type ConnectionParams struct {
18+
User string
19+
Password string
20+
Host string
21+
Database string
22+
}
23+
24+
func Connect(p ConnectionParams) (*gorm.DB, error) {
25+
loc, err := time.LoadLocation("UTC")
26+
if err != nil {
27+
return nil, fmt.Errorf("failed to load UT location: %w", err)
28+
}
29+
cfg := driver_mysql.Config{
30+
User: p.User,
31+
Passwd: p.Password,
32+
Net: "tcp",
33+
Addr: p.Host,
34+
DBName: p.Database,
35+
Loc: loc,
36+
AllowNativePasswords: true,
37+
ParseTime: true,
38+
}
39+
40+
// refer to https://github.com/go-sql-driver/mysql#dsn-data-source-name for details
41+
dsn := cfg.FormatDSN()
42+
fmt.Println(dsn)
43+
return gorm.Open(mysql.Open(cfg.FormatDSN()), &gorm.Config{})
44+
}
45+
46+
func ConnectForTests(t *testing.T) *gorm.DB {
47+
t.Helper()
48+
49+
conn, err := Connect(ConnectionParams{
50+
User: "root",
51+
Password: "test",
52+
Host: "localhost:23306",
53+
Database: "gitpod",
54+
})
55+
require.NoError(t, err, "Failed to establish connection to DB. In a workspace, run `leeway components/usage:init-testdb` once to bootstrap the DB.")
56+
57+
t.Cleanup(func() {
58+
rawConn, err := conn.DB()
59+
require.NoError(t, err)
60+
61+
require.NoError(t, rawConn.Close(), "must close database connection")
62+
})
63+
64+
return conn
65+
}

components/usage/pkg/db/conn_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package db
6+
7+
import (
8+
"github.com/stretchr/testify/require"
9+
"testing"
10+
)
11+
12+
func TestConnectForTests(t *testing.T) {
13+
conn := ConnectForTests(t)
14+
require.NotNil(t, conn)
15+
}

0 commit comments

Comments
 (0)