Skip to content

[common-go] Move base database connection & types to common-go #14688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package db
package common_db

import (
"fmt"
"time"

"github.com/gitpod-io/gitpod/common-go/log"
driver_mysql "github.com/go-sql-driver/mysql"
"github.com/sirupsen/logrus"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"time"
)

type ConnectionParams struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package db
package common_db

import (
"database/sql/driver"
"fmt"
"github.com/relvacode/iso8601"
"time"

"github.com/relvacode/iso8601"
)

func NewVarcharTime(t time.Time) VarcharTime {
func NewVarCharTime(t time.Time) VarcharTime {
return VarcharTime{
t: t.UTC(),
valid: true,
}
}

func NewVarcharTimeFromStr(s string) (VarcharTime, error) {
func NewVarCharTimeFromStr(s string) (VarcharTime, error) {
var vt VarcharTime
err := vt.Scan(s)
return vt, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package db
package common_db

import (
"encoding/json"
"github.com/stretchr/testify/require"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestVarcharTime_Scan(t *testing.T) {
Expand Down Expand Up @@ -85,7 +86,7 @@ func TestVarcharTime_Value_ISO8601(t *testing.T) {
Expected string
}{
{
Time: NewVarcharTime(time.Date(2019, 05, 10, 9, 54, 28, 185000000, time.UTC)),
Time: NewVarCharTime(time.Date(2019, 05, 10, 9, 54, 28, 185000000, time.UTC)),
Expected: "2019-05-10T09:54:28.185Z",
},
{
Expand All @@ -105,7 +106,7 @@ func TestVarcharTime_String_ISO8601(t *testing.T) {
Expected string
}{
{
Time: NewVarcharTime(time.Date(2019, 05, 10, 9, 54, 28, 185000000, time.UTC)),
Time: NewVarCharTime(time.Date(2019, 05, 10, 9, 54, 28, 185000000, time.UTC)),
Expected: "2019-05-10T09:54:28.185Z",
},
{
Expand All @@ -118,7 +119,7 @@ func TestVarcharTime_String_ISO8601(t *testing.T) {
}

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

serialized, err := json.Marshal(vt)
require.NoError(t, err)
Expand Down
9 changes: 9 additions & 0 deletions components/common-go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ require (
k8s.io/apimachinery v0.24.4
)

require (
github.com/go-sql-driver/mysql v1.6.0
github.com/relvacode/iso8601 v1.1.0
gorm.io/driver/mysql v1.4.4
gorm.io/gorm v1.24.1
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
Expand All @@ -47,6 +54,8 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand Down
14 changes: 14 additions & 0 deletions components/common-go/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions components/public-api-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ require (
github.com/AdaLogics/go-fuzz-headers v0.0.0-20220708163326-82d177caec6e
github.com/bufbuild/connect-go v1.0.0
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
github.com/gitpod-io/gitpod/gitpod-protocol v0.0.0-00010101000000-000000000000
github.com/gitpod-io/gitpod/components/public-api/go v0.0.0-00010101000000-000000000000
github.com/gitpod-io/gitpod/gitpod-protocol v0.0.0-00010101000000-000000000000
github.com/gitpod-io/gitpod/usage-api v0.0.0-00010101000000-000000000000
github.com/golang/mock v1.6.0
github.com/google/go-cmp v0.5.9
Expand All @@ -18,6 +18,7 @@ require (
github.com/prometheus/client_golang v1.13.0
github.com/relvacode/iso8601 v1.1.0
github.com/sirupsen/logrus v1.8.1
github.com/sourcegraph/jsonrpc2 v0.0.0-20200429184054-15c2290dcb37
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.7.0
github.com/stripe/stripe-go/v72 v72.122.0
Expand All @@ -44,7 +45,6 @@ require (
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/slok/go-http-metrics v0.10.0 // indirect
github.com/sourcegraph/jsonrpc2 v0.0.0-20200429184054-15c2290dcb37 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
Expand Down
3 changes: 1 addition & 2 deletions components/service-waiter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
github.com/go-sql-driver/mysql v1.4.1
github.com/go-sql-driver/mysql v1.6.0
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.7.0
Expand All @@ -26,7 +26,6 @@ require (
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/appengine v1.6.1 // indirect
gopkg.in/ini.v1 v1.51.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Expand Down
5 changes: 2 additions & 3 deletions components/service-waiter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions components/usage/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,40 @@ go 1.19
require (
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
github.com/gitpod-io/gitpod/usage-api v0.0.0-00010101000000-000000000000
github.com/go-sql-driver/mysql v1.6.0
github.com/google/go-cmp v0.5.8
github.com/google/uuid v1.1.2
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/prometheus/client_golang v1.13.0
github.com/relvacode/iso8601 v1.1.0
github.com/robfig/cron v1.2.0
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.7.0
github.com/stripe/stripe-go/v72 v72.114.0
google.golang.org/grpc v1.49.0
google.golang.org/protobuf v1.28.1
gorm.io/datatypes v1.0.6
gorm.io/driver/mysql v1.3.3
gorm.io/gorm v1.23.5
gorm.io/gorm v1.24.1
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/hashicorp/golang-lru v0.5.1 // indirect
github.com/heptiolabs/healthcheck v0.0.0-20211123025425-613501dd5deb // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.4 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/relvacode/iso8601 v1.1.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/slok/go-http-metrics v0.10.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
Expand All @@ -50,6 +49,7 @@ require (
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/genproto v0.0.0-20201019141844-1ed22bb0c154 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gorm.io/driver/mysql v1.4.4 // indirect
)

replace github.com/gitpod-io/gitpod/common-go => ../common-go // leeway
Expand Down
12 changes: 7 additions & 5 deletions components/usage/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions components/usage/pkg/apiv1/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"math"
"time"

common_db "github.com/gitpod-io/gitpod/common-go/db"
"github.com/gitpod-io/gitpod/common-go/log"
v1 "github.com/gitpod-io/gitpod/usage-api/v1"
"github.com/gitpod-io/gitpod/usage/pkg/db"
Expand Down Expand Up @@ -154,7 +155,7 @@ func (s *BillingService) CreateStripeCustomer(ctx context.Context, req *v1.Creat
err = db.CreateStripeCustomer(ctx, s.conn, db.StripeCustomer{
StripeCustomerID: customer.ID,
AttributionID: attributionID,
CreationTime: db.NewVarcharTime(time.Unix(customer.Created, 0)),
CreationTime: common_db.NewVarCharTime(time.Unix(customer.Created, 0)),
})
if err != nil {
log.WithField("attribution_id", attributionID).WithField("stripe_customer_id", customer.ID).WithError(err).Error("Failed to store Stripe Customer in the database.")
Expand Down Expand Up @@ -318,7 +319,7 @@ func (s *BillingService) FinalizeInvoice(ctx context.Context, in *v1.FinalizeInv
Description: fmt.Sprintf("Invoice %s finalized in Stripe", invoice.ID),
// Apply negative value of credits to reduce accrued credit usage
CreditCents: db.NewCreditCents(float64(-creditsOnInvoice)),
EffectiveTime: db.NewVarcharTime(finalizedAt),
EffectiveTime: common_db.NewVarCharTime(finalizedAt),
Kind: db.InvoiceUsageKind,
Draft: false,
Metadata: nil,
Expand Down Expand Up @@ -369,7 +370,7 @@ func (s *BillingService) storeStripeCustomer(ctx context.Context, cus *stripe_ap
StripeCustomerID: cus.ID,
AttributionID: attributionID,
// We use the original Stripe supplied creation timestamp, this ensures that we stay true to our ordering of customer creation records.
CreationTime: db.NewVarcharTime(time.Unix(cus.Created, 0)),
CreationTime: common_db.NewVarCharTime(time.Unix(cus.Created, 0)),
})
if err != nil {
return nil, err
Expand Down
7 changes: 4 additions & 3 deletions components/usage/pkg/apiv1/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/google/uuid"

common_db "github.com/gitpod-io/gitpod/common-go/db"
"github.com/gitpod-io/gitpod/common-go/log"
v1 "github.com/gitpod-io/gitpod/usage-api/v1"
"github.com/gitpod-io/gitpod/usage/pkg/db"
Expand Down Expand Up @@ -390,19 +391,19 @@ func newUsageFromInstance(instance db.WorkspaceInstanceForUsage, pricer *Workspa
AttributionID: instance.UsageAttributionID,
Description: usageDescriptionFromController,
CreditCents: db.NewCreditCents(pricer.CreditsUsedByInstance(&instance, now)),
EffectiveTime: db.NewVarcharTime(effectiveTime),
EffectiveTime: common_db.NewVarCharTime(effectiveTime),
Kind: db.WorkspaceInstanceUsageKind,
WorkspaceInstanceID: &instance.ID,
Draft: draft,
}

startedTime := ""
if instance.StartedTime.IsSet() {
startedTime = db.TimeToISO8601(instance.StartedTime.Time())
startedTime = common_db.TimeToISO8601(instance.StartedTime.Time())
}
endTime := ""
if instance.StoppingTime.IsSet() {
endTime = db.TimeToISO8601(instance.StoppingTime.Time())
endTime = common_db.TimeToISO8601(instance.StoppingTime.Time())
}
err := usage.SetMetadataWithWorkspaceInstance(db.WorkspaceInstanceUsageData{
WorkspaceId: instance.WorkspaceID,
Expand Down
Loading