Skip to content

Commit 1eea42d

Browse files
committed
[common-go] Move base database connection & types to common-go
1 parent eade024 commit 1eea42d

38 files changed

+286
-229
lines changed

components/usage/pkg/db/conn.go renamed to components/common-go/db/conn.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
// Licensed under the GNU Affero General Public License (AGPL).
33
// See License-AGPL.txt in the project root for license information.
44

5-
package db
5+
package common_db
66

77
import (
88
"fmt"
9+
"time"
10+
911
"github.com/gitpod-io/gitpod/common-go/log"
1012
driver_mysql "github.com/go-sql-driver/mysql"
1113
"github.com/sirupsen/logrus"
1214
"gorm.io/driver/mysql"
1315
"gorm.io/gorm"
1416
"gorm.io/gorm/logger"
15-
"time"
1617
)
1718

1819
type ConnectionParams struct {

components/usage/pkg/db/types.go renamed to components/common-go/db/types.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@
22
// Licensed under the GNU Affero General Public License (AGPL).
33
// See License-AGPL.txt in the project root for license information.
44

5-
package db
5+
package common_db
66

77
import (
88
"database/sql/driver"
99
"fmt"
10-
"github.com/relvacode/iso8601"
1110
"time"
11+
12+
"github.com/relvacode/iso8601"
1213
)
1314

14-
func NewVarcharTime(t time.Time) VarcharTime {
15+
func NewVarCharTime(t time.Time) VarcharTime {
1516
return VarcharTime{
1617
t: t.UTC(),
1718
valid: true,
1819
}
1920
}
2021

21-
func NewVarcharTimeFromStr(s string) (VarcharTime, error) {
22+
func NewVarCharTimeFromStr(s string) (VarcharTime, error) {
2223
var vt VarcharTime
2324
err := vt.Scan(s)
2425
return vt, err

components/usage/pkg/db/types_test.go renamed to components/common-go/db/types_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
// Licensed under the GNU Affero General Public License (AGPL).
33
// See License-AGPL.txt in the project root for license information.
44

5-
package db
5+
package common_db
66

77
import (
88
"encoding/json"
9-
"github.com/stretchr/testify/require"
109
"testing"
1110
"time"
11+
12+
"github.com/stretchr/testify/require"
1213
)
1314

1415
func TestVarcharTime_Scan(t *testing.T) {
@@ -85,7 +86,7 @@ func TestVarcharTime_Value_ISO8601(t *testing.T) {
8586
Expected string
8687
}{
8788
{
88-
Time: NewVarcharTime(time.Date(2019, 05, 10, 9, 54, 28, 185000000, time.UTC)),
89+
Time: NewVarCharTime(time.Date(2019, 05, 10, 9, 54, 28, 185000000, time.UTC)),
8990
Expected: "2019-05-10T09:54:28.185Z",
9091
},
9192
{
@@ -105,7 +106,7 @@ func TestVarcharTime_String_ISO8601(t *testing.T) {
105106
Expected string
106107
}{
107108
{
108-
Time: NewVarcharTime(time.Date(2019, 05, 10, 9, 54, 28, 185000000, time.UTC)),
109+
Time: NewVarCharTime(time.Date(2019, 05, 10, 9, 54, 28, 185000000, time.UTC)),
109110
Expected: "2019-05-10T09:54:28.185Z",
110111
},
111112
{
@@ -118,7 +119,7 @@ func TestVarcharTime_String_ISO8601(t *testing.T) {
118119
}
119120

120121
func TestVarCharTime_ToJSON(t *testing.T) {
121-
vt := NewVarcharTime(time.Date(2022, 7, 25, 11, 17, 23, 300, time.UTC))
122+
vt := NewVarCharTime(time.Date(2022, 7, 25, 11, 17, 23, 300, time.UTC))
122123

123124
serialized, err := json.Marshal(vt)
124125
require.NoError(t, err)

components/common-go/go.mod

+9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ require (
3434
k8s.io/apimachinery v0.24.4
3535
)
3636

37+
require (
38+
github.com/go-sql-driver/mysql v1.6.0
39+
github.com/relvacode/iso8601 v1.1.0
40+
gorm.io/driver/mysql v1.4.4
41+
gorm.io/gorm v1.24.1
42+
)
43+
3744
require (
3845
github.com/beorn7/perks v1.0.1 // indirect
3946
github.com/blang/semver v3.5.1+incompatible // indirect
@@ -47,6 +54,8 @@ require (
4754
github.com/gogo/protobuf v1.3.2 // indirect
4855
github.com/golang/protobuf v1.5.2 // indirect
4956
github.com/google/gofuzz v1.1.0 // indirect
57+
github.com/jinzhu/inflection v1.0.0 // indirect
58+
github.com/jinzhu/now v1.1.5 // indirect
5059
github.com/json-iterator/go v1.1.12 // indirect
5160
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
5261
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect

components/common-go/go.sum

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/public-api-server/go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ require (
66
github.com/AdaLogics/go-fuzz-headers v0.0.0-20220708163326-82d177caec6e
77
github.com/bufbuild/connect-go v1.0.0
88
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
9-
github.com/gitpod-io/gitpod/gitpod-protocol v0.0.0-00010101000000-000000000000
109
github.com/gitpod-io/gitpod/components/public-api/go v0.0.0-00010101000000-000000000000
10+
github.com/gitpod-io/gitpod/gitpod-protocol v0.0.0-00010101000000-000000000000
1111
github.com/gitpod-io/gitpod/usage-api v0.0.0-00010101000000-000000000000
1212
github.com/golang/mock v1.6.0
1313
github.com/google/go-cmp v0.5.9
@@ -18,6 +18,7 @@ require (
1818
github.com/prometheus/client_golang v1.13.0
1919
github.com/relvacode/iso8601 v1.1.0
2020
github.com/sirupsen/logrus v1.8.1
21+
github.com/sourcegraph/jsonrpc2 v0.0.0-20200429184054-15c2290dcb37
2122
github.com/spf13/cobra v1.4.0
2223
github.com/stretchr/testify v1.7.0
2324
github.com/stripe/stripe-go/v72 v72.122.0
@@ -44,7 +45,6 @@ require (
4445
github.com/prometheus/common v0.37.0 // indirect
4546
github.com/prometheus/procfs v0.8.0 // indirect
4647
github.com/slok/go-http-metrics v0.10.0 // indirect
47-
github.com/sourcegraph/jsonrpc2 v0.0.0-20200429184054-15c2290dcb37 // indirect
4848
github.com/spf13/pflag v1.0.5 // indirect
4949
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
5050
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect

components/service-waiter/go.mod

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.19
44

55
require (
66
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
7-
github.com/go-sql-driver/mysql v1.4.1
7+
github.com/go-sql-driver/mysql v1.6.0
88
github.com/mitchellh/go-homedir v1.1.0
99
github.com/spf13/cobra v1.4.0
1010
github.com/spf13/viper v1.7.0
@@ -26,7 +26,6 @@ require (
2626
github.com/subosito/gotenv v1.2.0 // indirect
2727
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
2828
golang.org/x/text v0.3.7 // indirect
29-
google.golang.org/appengine v1.6.1 // indirect
3029
gopkg.in/ini.v1 v1.51.0 // indirect
3130
gopkg.in/yaml.v2 v2.4.0 // indirect
3231
)

components/service-waiter/go.sum

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/usage/go.mod

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,40 @@ go 1.19
55
require (
66
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
77
github.com/gitpod-io/gitpod/usage-api v0.0.0-00010101000000-000000000000
8-
github.com/go-sql-driver/mysql v1.6.0
98
github.com/google/go-cmp v0.5.8
109
github.com/google/uuid v1.1.2
1110
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
1211
github.com/prometheus/client_golang v1.13.0
13-
github.com/relvacode/iso8601 v1.1.0
1412
github.com/robfig/cron v1.2.0
15-
github.com/sirupsen/logrus v1.8.1
1613
github.com/spf13/cobra v1.4.0
1714
github.com/stretchr/testify v1.7.0
1815
github.com/stripe/stripe-go/v72 v72.114.0
1916
google.golang.org/grpc v1.49.0
2017
google.golang.org/protobuf v1.28.1
2118
gorm.io/datatypes v1.0.6
22-
gorm.io/driver/mysql v1.3.3
23-
gorm.io/gorm v1.23.5
19+
gorm.io/gorm v1.24.1
2420
)
2521

2622
require (
2723
github.com/beorn7/perks v1.0.1 // indirect
2824
github.com/cespare/xxhash/v2 v2.1.2 // indirect
2925
github.com/davecgh/go-spew v1.1.1 // indirect
26+
github.com/go-sql-driver/mysql v1.6.0 // indirect
3027
github.com/golang/protobuf v1.5.2 // indirect
3128
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
3229
github.com/hashicorp/golang-lru v0.5.1 // indirect
3330
github.com/heptiolabs/healthcheck v0.0.0-20211123025425-613501dd5deb // indirect
3431
github.com/inconshreveable/mousetrap v1.0.0 // indirect
3532
github.com/jinzhu/inflection v1.0.0 // indirect
36-
github.com/jinzhu/now v1.1.4 // indirect
33+
github.com/jinzhu/now v1.1.5 // indirect
3734
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
3835
github.com/opentracing/opentracing-go v1.2.0 // indirect
3936
github.com/pmezard/go-difflib v1.0.0 // indirect
4037
github.com/prometheus/client_model v0.2.0 // indirect
4138
github.com/prometheus/common v0.37.0 // indirect
4239
github.com/prometheus/procfs v0.8.0 // indirect
40+
github.com/relvacode/iso8601 v1.1.0 // indirect
41+
github.com/sirupsen/logrus v1.8.1 // indirect
4342
github.com/slok/go-http-metrics v0.10.0 // indirect
4443
github.com/spf13/pflag v1.0.5 // indirect
4544
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
@@ -50,6 +49,7 @@ require (
5049
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
5150
google.golang.org/genproto v0.0.0-20201019141844-1ed22bb0c154 // indirect
5251
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
52+
gorm.io/driver/mysql v1.4.4 // indirect
5353
)
5454

5555
replace github.com/gitpod-io/gitpod/common-go => ../common-go // leeway

components/usage/go.sum

+7-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/usage/pkg/apiv1/billing.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"math"
1313
"time"
1414

15+
common_db "github.com/gitpod-io/gitpod/common-go/db"
1516
"github.com/gitpod-io/gitpod/common-go/log"
1617
v1 "github.com/gitpod-io/gitpod/usage-api/v1"
1718
"github.com/gitpod-io/gitpod/usage/pkg/db"
@@ -154,7 +155,7 @@ func (s *BillingService) CreateStripeCustomer(ctx context.Context, req *v1.Creat
154155
err = db.CreateStripeCustomer(ctx, s.conn, db.StripeCustomer{
155156
StripeCustomerID: customer.ID,
156157
AttributionID: attributionID,
157-
CreationTime: db.NewVarcharTime(time.Unix(customer.Created, 0)),
158+
CreationTime: common_db.NewVarCharTime(time.Unix(customer.Created, 0)),
158159
})
159160
if err != nil {
160161
log.WithField("attribution_id", attributionID).WithField("stripe_customer_id", customer.ID).WithError(err).Error("Failed to store Stripe Customer in the database.")
@@ -318,7 +319,7 @@ func (s *BillingService) FinalizeInvoice(ctx context.Context, in *v1.FinalizeInv
318319
Description: fmt.Sprintf("Invoice %s finalized in Stripe", invoice.ID),
319320
// Apply negative value of credits to reduce accrued credit usage
320321
CreditCents: db.NewCreditCents(float64(-creditsOnInvoice)),
321-
EffectiveTime: db.NewVarcharTime(finalizedAt),
322+
EffectiveTime: common_db.NewVarCharTime(finalizedAt),
322323
Kind: db.InvoiceUsageKind,
323324
Draft: false,
324325
Metadata: nil,
@@ -369,7 +370,7 @@ func (s *BillingService) storeStripeCustomer(ctx context.Context, cus *stripe_ap
369370
StripeCustomerID: cus.ID,
370371
AttributionID: attributionID,
371372
// We use the original Stripe supplied creation timestamp, this ensures that we stay true to our ordering of customer creation records.
372-
CreationTime: db.NewVarcharTime(time.Unix(cus.Created, 0)),
373+
CreationTime: common_db.NewVarCharTime(time.Unix(cus.Created, 0)),
373374
})
374375
if err != nil {
375376
return nil, err

components/usage/pkg/apiv1/usage.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/google/uuid"
1414

15+
common_db "github.com/gitpod-io/gitpod/common-go/db"
1516
"github.com/gitpod-io/gitpod/common-go/log"
1617
v1 "github.com/gitpod-io/gitpod/usage-api/v1"
1718
"github.com/gitpod-io/gitpod/usage/pkg/db"
@@ -390,19 +391,19 @@ func newUsageFromInstance(instance db.WorkspaceInstanceForUsage, pricer *Workspa
390391
AttributionID: instance.UsageAttributionID,
391392
Description: usageDescriptionFromController,
392393
CreditCents: db.NewCreditCents(pricer.CreditsUsedByInstance(&instance, now)),
393-
EffectiveTime: db.NewVarcharTime(effectiveTime),
394+
EffectiveTime: common_db.NewVarCharTime(effectiveTime),
394395
Kind: db.WorkspaceInstanceUsageKind,
395396
WorkspaceInstanceID: &instance.ID,
396397
Draft: draft,
397398
}
398399

399400
startedTime := ""
400401
if instance.StartedTime.IsSet() {
401-
startedTime = db.TimeToISO8601(instance.StartedTime.Time())
402+
startedTime = common_db.TimeToISO8601(instance.StartedTime.Time())
402403
}
403404
endTime := ""
404405
if instance.StoppingTime.IsSet() {
405-
endTime = db.TimeToISO8601(instance.StoppingTime.Time())
406+
endTime = common_db.TimeToISO8601(instance.StoppingTime.Time())
406407
}
407408
err := usage.SetMetadataWithWorkspaceInstance(db.WorkspaceInstanceUsageData{
408409
WorkspaceId: instance.WorkspaceID,

0 commit comments

Comments
 (0)