diff --git a/components/usage/pkg/db/conn.go b/components/common-go/db/conn.go similarity index 98% rename from components/usage/pkg/db/conn.go rename to components/common-go/db/conn.go index 08db84eb8474d8..b0417ad9d1a4d0 100644 --- a/components/usage/pkg/db/conn.go +++ b/components/common-go/db/conn.go @@ -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 { diff --git a/components/usage/pkg/db/types.go b/components/common-go/db/types.go similarity index 94% rename from components/usage/pkg/db/types.go rename to components/common-go/db/types.go index fbb8ddd62e8b21..0ff4c3cf8c5d84 100644 --- a/components/usage/pkg/db/types.go +++ b/components/common-go/db/types.go @@ -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 diff --git a/components/usage/pkg/db/types_test.go b/components/common-go/db/types_test.go similarity index 92% rename from components/usage/pkg/db/types_test.go rename to components/common-go/db/types_test.go index a2659f76096f1b..87b5612d52a61f 100644 --- a/components/usage/pkg/db/types_test.go +++ b/components/common-go/db/types_test.go @@ -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) { @@ -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", }, { @@ -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", }, { @@ -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) diff --git a/components/common-go/go.mod b/components/common-go/go.mod index bf71d3089eac55..83a306f2850c41 100644 --- a/components/common-go/go.mod +++ b/components/common-go/go.mod @@ -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 @@ -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 diff --git a/components/common-go/go.sum b/components/common-go/go.sum index d503de8c5e8fd6..9b991c2cb3d914 100644 --- a/components/common-go/go.sum +++ b/components/common-go/go.sum @@ -113,6 +113,8 @@ github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34 github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= github.com/go-openapi/jsonreference v0.19.3/go.mod h1:rjx6GuL8TTa9VaixXglHmQmIL98+wF9xc8zWvFonSJ8= github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk= +github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= +github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-test/deep v1.0.5 h1:AKODKU3pDH1RzZzm6YZu77YWtEAq6uh1rLIAQlay2qc= github.com/go-test/deep v1.0.5/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8= @@ -194,6 +196,11 @@ github.com/heptiolabs/healthcheck v0.0.0-20211123025425-613501dd5deb h1:tsEKRC3P github.com/heptiolabs/healthcheck v0.0.0-20211123025425-613501dd5deb/go.mod h1:NtmN9h8vrTveVQRLHcX2HQ5wIPBDCsZ351TGbZWgg38= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= @@ -281,6 +288,8 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1 github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5mo= github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4= +github.com/relvacode/iso8601 v1.1.0 h1:2nV8sp0eOjpoKQ2vD3xSDygsjAx37NHG2UlZiCkDH4I= +github.com/relvacode/iso8601 v1.1.0/go.mod h1:FlNp+jz+TXpyRqgmM7tnzHHzBnz776kmAH2h3sZCn0I= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/segmentio/backo-go v0.0.0-20200129164019-23eae7c10bd3 h1:ZuhckGJ10ulaKkdvJtiAqsLTiPrLaXSdnVgXJKJkTxE= github.com/segmentio/backo-go v0.0.0-20200129164019-23eae7c10bd3/go.mod h1:9/Rh6yILuLysoQnZ2oNooD2g7aBnvM7r/fNVxRNWfBc= @@ -653,6 +662,11 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/mysql v1.4.4 h1:MX0K9Qvy0Na4o7qSC/YI7XxqUw5KDw01umqgID+svdQ= +gorm.io/driver/mysql v1.4.4/go.mod h1:BCg8cKI+R0j/rZRQxeKis/forqRwRSYOR8OM3Wo6hOM= +gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= +gorm.io/gorm v1.24.1 h1:CgvzRniUdG67hBAzsxDGOAuq4Te1osVMYsa1eQbd4fs= +gorm.io/gorm v1.24.1/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/components/public-api-server/go.mod b/components/public-api-server/go.mod index 1c795d2a57375e..b778b7926187e3 100644 --- a/components/public-api-server/go.mod +++ b/components/public-api-server/go.mod @@ -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 @@ -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 @@ -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 diff --git a/components/service-waiter/go.mod b/components/service-waiter/go.mod index 8c01d66db0de8f..9dcda16e2416ba 100644 --- a/components/service-waiter/go.mod +++ b/components/service-waiter/go.mod @@ -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 @@ -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 ) diff --git a/components/service-waiter/go.sum b/components/service-waiter/go.sum index 4d66c45b0f8d3c..dbdaf5d7f97060 100644 --- a/components/service-waiter/go.sum +++ b/components/service-waiter/go.sum @@ -46,8 +46,8 @@ github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9 github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= -github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA= -github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= +github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= +github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= @@ -290,7 +290,6 @@ google.golang.org/api v0.13.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsb google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -google.golang.org/appengine v1.6.1 h1:QzqyMA1tlu6CgqCDUtU9V+ZKhLFT2dkJuANu5QaxI3I= google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= diff --git a/components/usage/go.mod b/components/usage/go.mod index a4b5bc01516e93..6ef4c304347b9b 100644 --- a/components/usage/go.mod +++ b/components/usage/go.mod @@ -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 @@ -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 diff --git a/components/usage/go.sum b/components/usage/go.sum index ba7c0dbfa0c237..242e75e9da9ffd 100644 --- a/components/usage/go.sum +++ b/components/usage/go.sum @@ -215,8 +215,9 @@ github.com/jackc/puddle v1.2.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dv github.com/jackc/puddle v1.2.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= -github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas= github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= @@ -672,8 +673,8 @@ gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C gorm.io/datatypes v1.0.6 h1:3cqbakp1DIgC+P7wyODb5k+lSjW8g3mjkg/BIsmhjlE= gorm.io/datatypes v1.0.6/go.mod h1:Gh/Xd/iUWWybMEk8CzYCK/swqlni2r+ROeM1HGIM0ck= gorm.io/driver/mysql v1.3.2/go.mod h1:ChK6AHbHgDCFZyJp0F+BmVGb06PSIoh9uVYKAlRbb2U= -gorm.io/driver/mysql v1.3.3 h1:jXG9ANrwBc4+bMvBcSl8zCfPBaVoPyBEBshA8dA93X8= -gorm.io/driver/mysql v1.3.3/go.mod h1:ChK6AHbHgDCFZyJp0F+BmVGb06PSIoh9uVYKAlRbb2U= +gorm.io/driver/mysql v1.4.4 h1:MX0K9Qvy0Na4o7qSC/YI7XxqUw5KDw01umqgID+svdQ= +gorm.io/driver/mysql v1.4.4/go.mod h1:BCg8cKI+R0j/rZRQxeKis/forqRwRSYOR8OM3Wo6hOM= gorm.io/driver/postgres v1.3.1 h1:Pyv+gg1Gq1IgsLYytj/S2k7ebII3CzEdpqQkPOdH24g= gorm.io/driver/postgres v1.3.1/go.mod h1:WwvWOuR9unCLpGWCL6Y3JOeBWvbKi6JLhayiVclSZZU= gorm.io/driver/sqlite v1.3.1 h1:bwfE+zTEWklBYoEodIOIBwuWHpnx52Z9zJFW5F33WLk= @@ -682,8 +683,9 @@ gorm.io/driver/sqlserver v1.3.1 h1:F5t6ScMzOgy1zukRTIZgLZwKahgt3q1woAILVolKpOI= gorm.io/driver/sqlserver v1.3.1/go.mod h1:w25Vrx2BG+CJNUu/xKbFhaKlGxT/nzRkhWCCoptX8tQ= gorm.io/gorm v1.23.1/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= gorm.io/gorm v1.23.2/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= -gorm.io/gorm v1.23.5 h1:TnlF26wScKSvknUC/Rn8t0NLLM22fypYBlvj1+aH6dM= -gorm.io/gorm v1.23.5/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= +gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= +gorm.io/gorm v1.24.1 h1:CgvzRniUdG67hBAzsxDGOAuq4Te1osVMYsa1eQbd4fs= +gorm.io/gorm v1.24.1/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/components/usage/pkg/apiv1/billing.go b/components/usage/pkg/apiv1/billing.go index e15cb75bb3c0d2..23058398039347 100644 --- a/components/usage/pkg/apiv1/billing.go +++ b/components/usage/pkg/apiv1/billing.go @@ -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" @@ -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.") @@ -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, @@ -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 diff --git a/components/usage/pkg/apiv1/usage.go b/components/usage/pkg/apiv1/usage.go index 8899a740879efa..004438aa7d67c4 100644 --- a/components/usage/pkg/apiv1/usage.go +++ b/components/usage/pkg/apiv1/usage.go @@ -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" @@ -390,7 +391,7 @@ 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, @@ -398,11 +399,11 @@ func newUsageFromInstance(instance db.WorkspaceInstanceForUsage, pricer *Workspa 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, diff --git a/components/usage/pkg/apiv1/usage_test.go b/components/usage/pkg/apiv1/usage_test.go index 9f4c4d6184d578..54de0509c15076 100644 --- a/components/usage/pkg/apiv1/usage_test.go +++ b/components/usage/pkg/apiv1/usage_test.go @@ -12,6 +12,7 @@ import ( "time" "github.com/gitpod-io/gitpod/common-go/baseserver" + common_db "github.com/gitpod-io/gitpod/common-go/db" v1 "github.com/gitpod-io/gitpod/usage-api/v1" "github.com/gitpod-io/gitpod/usage/pkg/db" "github.com/gitpod-io/gitpod/usage/pkg/db/dbtest" @@ -36,14 +37,14 @@ func TestUsageService_ReconcileUsage(t *testing.T) { // stopped instances instance := dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ UsageAttributionID: attributionID, - StartedTime: db.NewVarcharTime(from), - StoppingTime: db.NewVarcharTime(to.Add(-1 * time.Minute)), + StartedTime: common_db.NewVarCharTime(from), + StoppingTime: common_db.NewVarCharTime(to.Add(-1 * time.Minute)), }) dbtest.CreateWorkspaceInstances(t, dbconn, instance) // running instances dbtest.CreateWorkspaceInstances(t, dbconn, dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ - StartedTime: db.NewVarcharTime(to.Add(-1 * time.Minute)), + StartedTime: common_db.NewVarCharTime(to.Add(-1 * time.Minute)), UsageAttributionID: attributionID, })) @@ -134,7 +135,7 @@ func TestReconcile(t *testing.T) { WorkspaceClass: db.WorkspaceClass_Default, Type: db.WorkspaceType_Regular, UsageAttributionID: db.NewTeamAttributionID(uuid.New().String()), - StartedTime: db.NewVarcharTime(now.Add(1 * time.Minute)), + StartedTime: common_db.NewVarCharTime(now.Add(1 * time.Minute)), } inserts, updates, err := reconcileUsage([]db.WorkspaceInstanceForUsage{instance, instance}, nil, pricer, now) @@ -146,7 +147,7 @@ func TestReconcile(t *testing.T) { AttributionID: instance.UsageAttributionID, Description: usageDescriptionFromController, CreditCents: db.NewCreditCents(pricer.CreditsUsedByInstance(&instance, now)), - EffectiveTime: db.NewVarcharTime(now), + EffectiveTime: common_db.NewVarCharTime(now), Kind: db.WorkspaceInstanceUsageKind, WorkspaceInstanceID: &instance.ID, Draft: true, @@ -157,7 +158,7 @@ func TestReconcile(t *testing.T) { WorkspaceType: instance.Type, WorkspaceClass: instance.WorkspaceClass, ContextURL: instance.ContextURL, - StartTime: db.TimeToISO8601(instance.StartedTime.Time()), + StartTime: common_db.TimeToISO8601(instance.StartedTime.Time()), EndTime: "", UserName: instance.UserName, UserAvatarURL: instance.UserAvatarURL, @@ -177,7 +178,7 @@ func TestReconcile(t *testing.T) { WorkspaceClass: db.WorkspaceClass_Default, Type: db.WorkspaceType_Regular, UsageAttributionID: db.NewTeamAttributionID(uuid.New().String()), - StartedTime: db.NewVarcharTime(now.Add(1 * time.Minute)), + StartedTime: common_db.NewVarCharTime(now.Add(1 * time.Minute)), } // the fields in the usage record deliberately do not match the instance, except for the Instance ID. @@ -187,7 +188,7 @@ func TestReconcile(t *testing.T) { AttributionID: db.NewUserAttributionID(uuid.New().String()), Description: "Some description", CreditCents: 1, - EffectiveTime: db.VarcharTime{}, + EffectiveTime: common_db.VarcharTime{}, Kind: db.WorkspaceInstanceUsageKind, WorkspaceInstanceID: &instance.ID, Draft: true, @@ -204,7 +205,7 @@ func TestReconcile(t *testing.T) { AttributionID: instance.UsageAttributionID, Description: usageDescriptionFromController, CreditCents: db.NewCreditCents(pricer.CreditsUsedByInstance(&instance, now)), - EffectiveTime: db.NewVarcharTime(now), + EffectiveTime: common_db.NewVarCharTime(now), Kind: db.WorkspaceInstanceUsageKind, WorkspaceInstanceID: &instance.ID, Draft: true, @@ -215,7 +216,7 @@ func TestReconcile(t *testing.T) { WorkspaceType: instance.Type, WorkspaceClass: instance.WorkspaceClass, ContextURL: instance.ContextURL, - StartTime: db.TimeToISO8601(instance.StartedTime.Time()), + StartTime: common_db.TimeToISO8601(instance.StartedTime.Time()), EndTime: "", UserName: instance.UserName, UserAvatarURL: instance.UserAvatarURL, @@ -272,34 +273,34 @@ func TestListUsage(t *testing.T) { draftBefore := dbtest.NewUsage(t, db.Usage{ AttributionID: attributionID, - EffectiveTime: db.NewVarcharTime(start.Add(-1 * 23 * time.Hour)), + EffectiveTime: common_db.NewVarCharTime(start.Add(-1 * 23 * time.Hour)), CreditCents: 100, Draft: true, }) nondraftBefore := dbtest.NewUsage(t, db.Usage{ AttributionID: attributionID, - EffectiveTime: db.NewVarcharTime(start.Add(-1 * 23 * time.Hour)), + EffectiveTime: common_db.NewVarCharTime(start.Add(-1 * 23 * time.Hour)), CreditCents: 200, Draft: false, }) draftInside := dbtest.NewUsage(t, db.Usage{ AttributionID: attributionID, - EffectiveTime: db.NewVarcharTime(start.Add(2 * time.Hour)), + EffectiveTime: common_db.NewVarCharTime(start.Add(2 * time.Hour)), CreditCents: 300, Draft: true, }) nonDraftInside := dbtest.NewUsage(t, db.Usage{ AttributionID: attributionID, - EffectiveTime: db.NewVarcharTime(start.Add(2 * time.Hour)), + EffectiveTime: common_db.NewVarCharTime(start.Add(2 * time.Hour)), CreditCents: 400, Draft: false, }) nonDraftAfter := dbtest.NewUsage(t, db.Usage{ AttributionID: attributionID, - EffectiveTime: db.NewVarcharTime(end.Add(2 * time.Hour)), + EffectiveTime: common_db.NewVarCharTime(end.Add(2 * time.Hour)), CreditCents: 1000, }) diff --git a/components/usage/pkg/db/cost_center.go b/components/usage/pkg/db/cost_center.go index 1e3e99c42c9caa..2215ebeebaa56c 100644 --- a/components/usage/pkg/db/cost_center.go +++ b/components/usage/pkg/db/cost_center.go @@ -10,6 +10,7 @@ import ( "fmt" "time" + common_db "github.com/gitpod-io/gitpod/common-go/db" "github.com/gitpod-io/gitpod/common-go/log" "github.com/google/uuid" "google.golang.org/grpc/codes" @@ -27,13 +28,13 @@ const ( ) type CostCenter struct { - ID AttributionID `gorm:"primary_key;column:id;type:char;size:36;" json:"id"` - CreationTime VarcharTime `gorm:"primary_key;column:creationTime;type:varchar;size:255;" json:"creationTime"` - SpendingLimit int32 `gorm:"column:spendingLimit;type:int;default:0;" json:"spendingLimit"` - BillingStrategy BillingStrategy `gorm:"column:billingStrategy;type:varchar;size:255;" json:"billingStrategy"` - BillingCycleStart VarcharTime `gorm:"column:billingCycleStart;type:varchar;size:255;" json:"billingCycleStart"` - NextBillingTime VarcharTime `gorm:"column:nextBillingTime;type:varchar;size:255;" json:"nextBillingTime"` - LastModified time.Time `gorm:"->;column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"` + ID AttributionID `gorm:"primary_key;column:id;type:char;size:36;" json:"id"` + CreationTime common_db.VarcharTime `gorm:"primary_key;column:creationTime;type:varchar;size:255;" json:"creationTime"` + SpendingLimit int32 `gorm:"column:spendingLimit;type:int;default:0;" json:"spendingLimit"` + BillingStrategy BillingStrategy `gorm:"column:billingStrategy;type:varchar;size:255;" json:"billingStrategy"` + BillingCycleStart common_db.VarcharTime `gorm:"column:billingCycleStart;type:varchar;size:255;" json:"billingCycleStart"` + NextBillingTime common_db.VarcharTime `gorm:"column:nextBillingTime;type:varchar;size:255;" json:"nextBillingTime"` + LastModified time.Time `gorm:"->;column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"` } // TableName sets the insert table name for this struct type @@ -83,11 +84,11 @@ func (c *CostCenterManager) GetOrCreateCostCenter(ctx context.Context, attributi } result = CostCenter{ ID: attributionID, - CreationTime: NewVarcharTime(now), + CreationTime: common_db.NewVarCharTime(now), BillingStrategy: CostCenter_Other, SpendingLimit: defaultSpendingLimit, - BillingCycleStart: NewVarcharTime(now), - NextBillingTime: NewVarcharTime(now.AddDate(0, 1, 0)), + BillingCycleStart: common_db.NewVarCharTime(now), + NextBillingTime: common_db.NewVarCharTime(now.AddDate(0, 1, 0)), } err := c.conn.Save(&result).Error if err != nil { @@ -146,7 +147,7 @@ func (c *CostCenterManager) UpdateCostCenter(ctx context.Context, newCC CostCent now := time.Now() // we always update the creationTime - newCC.CreationTime = NewVarcharTime(now) + newCC.CreationTime = common_db.NewVarCharTime(now) // we don't allow setting billingCycleStart or nextBillingTime from outside newCC.BillingCycleStart = existingCC.BillingCycleStart newCC.NextBillingTime = existingCC.NextBillingTime @@ -172,9 +173,9 @@ func (c *CostCenterManager) UpdateCostCenter(ctx context.Context, newCC CostCent // Downgrading from stripe if existingCC.BillingStrategy == CostCenter_Stripe && newCC.BillingStrategy == CostCenter_Other { newCC.SpendingLimit = c.cfg.ForUsers - newCC.BillingCycleStart = NewVarcharTime(now) + newCC.BillingCycleStart = common_db.NewVarCharTime(now) // see you next month - newCC.NextBillingTime = NewVarcharTime(now.AddDate(0, 1, 0)) + newCC.NextBillingTime = common_db.NewVarCharTime(now.AddDate(0, 1, 0)) } // Upgrading to Stripe @@ -184,9 +185,9 @@ func (c *CostCenterManager) UpdateCostCenter(ctx context.Context, newCC CostCent return CostCenter{}, err } - newCC.BillingCycleStart = NewVarcharTime(now) + newCC.BillingCycleStart = common_db.NewVarCharTime(now) // we don't manage stripe billing cycle - newCC.NextBillingTime = VarcharTime{} + newCC.NextBillingTime = common_db.VarcharTime{} } } else if isTeam { // Billing strategy is Other, and it remains unchanged @@ -200,9 +201,9 @@ func (c *CostCenterManager) UpdateCostCenter(ctx context.Context, newCC CostCent // Downgrading from stripe if existingCC.BillingStrategy == CostCenter_Stripe && newCC.BillingStrategy == CostCenter_Other { newCC.SpendingLimit = c.cfg.ForTeams - newCC.BillingCycleStart = NewVarcharTime(now) + newCC.BillingCycleStart = common_db.NewVarCharTime(now) // see you next month - newCC.NextBillingTime = NewVarcharTime(now.AddDate(0, 1, 0)) + newCC.NextBillingTime = common_db.NewVarCharTime(now.AddDate(0, 1, 0)) } // Upgrading to Stripe @@ -212,9 +213,9 @@ func (c *CostCenterManager) UpdateCostCenter(ctx context.Context, newCC CostCent return CostCenter{}, err } - newCC.BillingCycleStart = NewVarcharTime(now) + newCC.BillingCycleStart = common_db.NewVarCharTime(now) // we don't manage stripe billing cycle - newCC.NextBillingTime = VarcharTime{} + newCC.NextBillingTime = common_db.VarcharTime{} } } else { return CostCenter{}, status.Errorf(codes.InvalidArgument, "Unknown attribution entity %s", string(attributionID)) @@ -259,7 +260,7 @@ func (c *CostCenterManager) NewInvoiceUsageRecord(ctx context.Context, attributi AttributionID: attributionID, Description: "Credits", CreditCents: creditCents * -1, - EffectiveTime: NewVarcharTime(now), + EffectiveTime: common_db.NewVarCharTime(now), Kind: InvoiceUsageKind, Draft: false, }, nil @@ -282,7 +283,7 @@ func (c *CostCenterManager) ListLatestCostCentersWithBillingTimeBefore(ctx conte Joins("INNER JOIN (?) AS expiredCC on cc.id = expiredCC.id AND cc.creationTime = expiredCC.creationTime", subquery). Where("cc.billingStrategy = ?", strategy). Where("nextBillingTime != ?", ""). - Where("nextBillingTime < ?", TimeToISO8601(billingTimeBefore)). + Where("nextBillingTime < ?", common_db.TimeToISO8601(billingTimeBefore)). FindInBatches(&batch, 1000, func(tx *gorm.DB, iteration int) error { results = append(results, batch...) return nil @@ -338,9 +339,9 @@ func (c *CostCenterManager) ResetUsage(ctx context.Context, cc CostCenter) (Cost ID: cc.ID, SpendingLimit: spendingLimit, BillingStrategy: cc.BillingStrategy, - BillingCycleStart: NewVarcharTime(billingCycleStart), - NextBillingTime: NewVarcharTime(nextBillingTime), - CreationTime: NewVarcharTime(now), + BillingCycleStart: common_db.NewVarCharTime(billingCycleStart), + NextBillingTime: common_db.NewVarCharTime(nextBillingTime), + CreationTime: common_db.NewVarCharTime(now), } err = c.conn.Save(&newCostCenter).Error if err != nil { diff --git a/components/usage/pkg/db/cost_center_test.go b/components/usage/pkg/db/cost_center_test.go index 6d2a544092fb89..ad7a2886ce3854 100644 --- a/components/usage/pkg/db/cost_center_test.go +++ b/components/usage/pkg/db/cost_center_test.go @@ -9,6 +9,7 @@ import ( "testing" "time" + common_db "github.com/gitpod-io/gitpod/common-go/db" "github.com/gitpod-io/gitpod/usage/pkg/db" "github.com/gitpod-io/gitpod/usage/pkg/db/dbtest" "github.com/google/uuid" @@ -72,28 +73,28 @@ func TestCostCenterManager_GetOrCreateCostCenter_ResetsExpired(t *testing.T) { expiredCC := db.CostCenter{ ID: db.NewTeamAttributionID(uuid.New().String()), - CreationTime: db.NewVarcharTime(now), + CreationTime: common_db.NewVarCharTime(now), SpendingLimit: 0, BillingStrategy: db.CostCenter_Other, - NextBillingTime: db.NewVarcharTime(expired), - BillingCycleStart: db.NewVarcharTime(now), + NextBillingTime: common_db.NewVarCharTime(expired), + BillingCycleStart: common_db.NewVarCharTime(now), } unexpiredCC := db.CostCenter{ ID: db.NewUserAttributionID(uuid.New().String()), - CreationTime: db.NewVarcharTime(now), + CreationTime: common_db.NewVarCharTime(now), SpendingLimit: 500, BillingStrategy: db.CostCenter_Other, - NextBillingTime: db.NewVarcharTime(unexpired), - BillingCycleStart: db.NewVarcharTime(now), + NextBillingTime: common_db.NewVarCharTime(unexpired), + BillingCycleStart: common_db.NewVarCharTime(now), } // Stripe billing strategy should not be reset stripeCC := db.CostCenter{ ID: db.NewUserAttributionID(uuid.New().String()), - CreationTime: db.NewVarcharTime(now), + CreationTime: common_db.NewVarCharTime(now), SpendingLimit: 0, BillingStrategy: db.CostCenter_Stripe, - NextBillingTime: db.VarcharTime{}, - BillingCycleStart: db.NewVarcharTime(now), + NextBillingTime: common_db.VarcharTime{}, + BillingCycleStart: common_db.NewVarCharTime(now), } dbtest.CreateCostCenters(t, conn, @@ -108,7 +109,7 @@ func TestCostCenterManager_GetOrCreateCostCenter_ResetsExpired(t *testing.T) { t.Cleanup(func() { conn.Model(&db.CostCenter{}).Delete(retrievedExpiredCC.ID) }) - require.Equal(t, db.NewVarcharTime(expired).Time().AddDate(0, 1, 0), retrievedExpiredCC.NextBillingTime.Time()) + require.Equal(t, common_db.NewVarCharTime(expired).Time().AddDate(0, 1, 0), retrievedExpiredCC.NextBillingTime.Time()) require.Equal(t, expiredCC.ID, retrievedExpiredCC.ID) require.Equal(t, expiredCC.BillingStrategy, retrievedExpiredCC.BillingStrategy) require.WithinDuration(t, now, expiredCC.CreationTime.Time(), 3*time.Second, "new cost center creation time must be within 3 seconds of now") @@ -116,7 +117,7 @@ func TestCostCenterManager_GetOrCreateCostCenter_ResetsExpired(t *testing.T) { // unexpired cost center must not be reset retrievedUnexpiredCC, err := mnr.GetOrCreateCostCenter(context.Background(), unexpiredCC.ID) require.NoError(t, err) - require.Equal(t, db.NewVarcharTime(unexpired).Time(), retrievedUnexpiredCC.NextBillingTime.Time()) + require.Equal(t, common_db.NewVarCharTime(unexpired).Time(), retrievedUnexpiredCC.NextBillingTime.Time()) require.Equal(t, unexpiredCC.ID, retrievedUnexpiredCC.ID) require.Equal(t, unexpiredCC.BillingStrategy, retrievedUnexpiredCC.BillingStrategy) require.WithinDuration(t, unexpiredCC.CreationTime.Time(), retrievedUnexpiredCC.CreationTime.Time(), 100*time.Millisecond) @@ -288,7 +289,7 @@ func TestSaveCostCenterMovedToStripe(t *testing.T) { teamCC, err = mnr.UpdateCostCenter(context.Background(), teamCC) require.NoError(t, err) require.Equal(t, db.CostCenter_Stripe, teamCC.BillingStrategy) - require.Equal(t, db.VarcharTime{}, teamCC.NextBillingTime) + require.Equal(t, common_db.VarcharTime{}, teamCC.NextBillingTime) require.Equal(t, int32(400050), teamCC.SpendingLimit) teamCC.BillingStrategy = db.CostCenter_Other @@ -348,16 +349,16 @@ func TestCostCenter_ListLatestCostCentersWithBillingTimeBefore(t *testing.T) { dbtest.NewCostCenter(t, db.CostCenter{ ID: db.NewTeamAttributionID(attributionID), SpendingLimit: 100, - CreationTime: db.NewVarcharTime(firstCreation), + CreationTime: common_db.NewVarCharTime(firstCreation), BillingStrategy: db.CostCenter_Other, - NextBillingTime: db.NewVarcharTime(firstCreation), + NextBillingTime: common_db.NewVarCharTime(firstCreation), }), dbtest.NewCostCenter(t, db.CostCenter{ ID: db.NewTeamAttributionID(attributionID), SpendingLimit: 100, - CreationTime: db.NewVarcharTime(secondCreation), + CreationTime: common_db.NewVarCharTime(secondCreation), BillingStrategy: db.CostCenter_Other, - NextBillingTime: db.NewVarcharTime(secondCreation), + NextBillingTime: common_db.NewVarCharTime(secondCreation), }), } @@ -385,14 +386,14 @@ func TestCostCenter_ListLatestCostCentersWithBillingTimeBefore(t *testing.T) { dbtest.NewCostCenter(t, db.CostCenter{ ID: db.NewTeamAttributionID(attributionID), SpendingLimit: 100, - CreationTime: db.NewVarcharTime(firstCreation), + CreationTime: common_db.NewVarCharTime(firstCreation), BillingStrategy: db.CostCenter_Other, - NextBillingTime: db.NewVarcharTime(firstCreation), + NextBillingTime: common_db.NewVarCharTime(firstCreation), }), dbtest.NewCostCenter(t, db.CostCenter{ ID: db.NewTeamAttributionID(attributionID), SpendingLimit: 100, - CreationTime: db.NewVarcharTime(secondCreation), + CreationTime: common_db.NewVarCharTime(secondCreation), BillingStrategy: db.CostCenter_Stripe, }), } @@ -417,7 +418,7 @@ func TestCostCenterManager_ResetUsage(t *testing.T) { }) _, err := mnr.ResetUsage(context.Background(), db.CostCenter{ ID: db.NewUserAttributionID(uuid.New().String()), - CreationTime: db.NewVarcharTime(time.Now()), + CreationTime: common_db.NewVarCharTime(time.Now()), SpendingLimit: 500, BillingStrategy: db.CostCenter_Stripe, }) @@ -432,10 +433,10 @@ func TestCostCenterManager_ResetUsage(t *testing.T) { }) oldCC := db.CostCenter{ ID: db.NewTeamAttributionID(uuid.New().String()), - CreationTime: db.NewVarcharTime(time.Now()), + CreationTime: common_db.NewVarCharTime(time.Now()), SpendingLimit: 0, BillingStrategy: db.CostCenter_Other, - NextBillingTime: db.NewVarcharTime(ts), + NextBillingTime: common_db.NewVarCharTime(ts), } newCC, err := mnr.ResetUsage(context.Background(), oldCC) require.NoError(t, err) diff --git a/components/usage/pkg/db/dbtest/conn.go b/components/usage/pkg/db/dbtest/conn.go index 7f8857c1b244c0..1f14313d4a133d 100644 --- a/components/usage/pkg/db/dbtest/conn.go +++ b/components/usage/pkg/db/dbtest/conn.go @@ -5,11 +5,12 @@ package dbtest import ( - "github.com/gitpod-io/gitpod/usage/pkg/db" - "github.com/stretchr/testify/require" - "gorm.io/gorm" "sync" "testing" + + common_db "github.com/gitpod-io/gitpod/common-go/db" + "github.com/stretchr/testify/require" + "gorm.io/gorm" ) var ( @@ -30,7 +31,7 @@ func ConnectForTests(t *testing.T) *gorm.DB { // These are static connection details for tests, started by `leeway components/usage:init-testdb`. // We use the same static credentials for CI & local instance of MySQL Server. var err error - conn, err = db.Connect(db.ConnectionParams{ + conn, err = common_db.Connect(common_db.ConnectionParams{ User: "root", Password: "test", Host: "localhost:23306", diff --git a/components/usage/pkg/db/dbtest/cost_center.go b/components/usage/pkg/db/dbtest/cost_center.go index 378b7c2d2e6434..619e1b2d3f751c 100644 --- a/components/usage/pkg/db/dbtest/cost_center.go +++ b/components/usage/pkg/db/dbtest/cost_center.go @@ -8,6 +8,7 @@ import ( "testing" "time" + common_db "github.com/gitpod-io/gitpod/common-go/db" "github.com/gitpod-io/gitpod/usage/pkg/db" "github.com/google/uuid" "github.com/stretchr/testify/require" @@ -19,11 +20,11 @@ func NewCostCenter(t *testing.T, record db.CostCenter) db.CostCenter { result := db.CostCenter{ ID: db.NewUserAttributionID(uuid.New().String()), - CreationTime: db.NewVarcharTime(time.Now()), + CreationTime: common_db.NewVarCharTime(time.Now()), SpendingLimit: 100, BillingStrategy: db.CostCenter_Stripe, - BillingCycleStart: db.NewVarcharTime(time.Now()), - NextBillingTime: db.NewVarcharTime(time.Now().Add(10 * time.Hour)), + BillingCycleStart: common_db.NewVarCharTime(time.Now()), + NextBillingTime: common_db.NewVarCharTime(time.Now().Add(10 * time.Hour)), } if record.ID != "" { diff --git a/components/usage/pkg/db/dbtest/stripe_customer.go b/components/usage/pkg/db/dbtest/stripe_customer.go index db39bf379d1947..389422ee93301d 100644 --- a/components/usage/pkg/db/dbtest/stripe_customer.go +++ b/components/usage/pkg/db/dbtest/stripe_customer.go @@ -9,6 +9,7 @@ import ( "testing" "time" + common_db "github.com/gitpod-io/gitpod/common-go/db" "github.com/gitpod-io/gitpod/usage/pkg/db" "github.com/google/uuid" "github.com/stretchr/testify/require" @@ -21,7 +22,7 @@ func NewStripeCustomer(t *testing.T, customer db.StripeCustomer) db.StripeCustom result := db.StripeCustomer{ StripeCustomerID: fmt.Sprintf("cus_%s", uuid.New().String()), AttributionID: db.NewUserAttributionID(uuid.New().String()), - CreationTime: db.NewVarcharTime(time.Now()), + CreationTime: common_db.NewVarCharTime(time.Now()), } if customer.StripeCustomerID != "" { diff --git a/components/usage/pkg/db/dbtest/usage.go b/components/usage/pkg/db/dbtest/usage.go index ea2c466734689f..2b84e84aa55bee 100644 --- a/components/usage/pkg/db/dbtest/usage.go +++ b/components/usage/pkg/db/dbtest/usage.go @@ -8,6 +8,7 @@ import ( "context" "testing" + common_db "github.com/gitpod-io/gitpod/common-go/db" "github.com/gitpod-io/gitpod/usage/pkg/db" "github.com/google/uuid" "github.com/stretchr/testify/require" @@ -24,7 +25,7 @@ func NewUsage(t *testing.T, record db.Usage) db.Usage { AttributionID: db.NewUserAttributionID(uuid.New().String()), Description: "some description", CreditCents: 42, - EffectiveTime: db.VarcharTime{}, + EffectiveTime: common_db.VarcharTime{}, Kind: db.WorkspaceInstanceUsageKind, WorkspaceInstanceID: &workspaceInstanceId, } diff --git a/components/usage/pkg/db/dbtest/user.go b/components/usage/pkg/db/dbtest/user.go index af5151de99f356..82b7551b67468f 100644 --- a/components/usage/pkg/db/dbtest/user.go +++ b/components/usage/pkg/db/dbtest/user.go @@ -8,18 +8,18 @@ import ( "testing" "time" - "github.com/gitpod-io/gitpod/usage/pkg/db" + common_db "github.com/gitpod-io/gitpod/common-go/db" "github.com/google/uuid" "github.com/stretchr/testify/require" "gorm.io/gorm" ) type User struct { - ID uuid.UUID `gorm:"primary_key;column:id;type:char;size:36;"` - AvatarURL string `gorm:"column:avatarUrl;type:char;size:255;"` - Name string `gorm:"column:name;type:char;size:255;"` - FullName string `gorm:"column:fullName;type:char;size:255;"` - CreationDate db.VarcharTime `gorm:"column:creationDate;type:varchar;size:255;"` + ID uuid.UUID `gorm:"primary_key;column:id;type:char;size:36;"` + AvatarURL string `gorm:"column:avatarUrl;type:char;size:255;"` + Name string `gorm:"column:name;type:char;size:255;"` + FullName string `gorm:"column:fullName;type:char;size:255;"` + CreationDate common_db.VarcharTime `gorm:"column:creationDate;type:varchar;size:255;"` // user has more field but we don't care here as they are just used in tests. } @@ -36,7 +36,7 @@ func NewUser(t *testing.T, user User) User { AvatarURL: "https://avatars.githubusercontent.com/u/9071", Name: "HomerJSimpson", FullName: "Homer Simpson", - CreationDate: db.NewVarcharTime(time.Now()), + CreationDate: common_db.NewVarCharTime(time.Now()), } if user.ID != uuid.Nil { diff --git a/components/usage/pkg/db/dbtest/workspace_instance.go b/components/usage/pkg/db/dbtest/workspace_instance.go index 8a1be3b808c07a..30e40d89970efa 100644 --- a/components/usage/pkg/db/dbtest/workspace_instance.go +++ b/components/usage/pkg/db/dbtest/workspace_instance.go @@ -10,6 +10,7 @@ import ( "testing" "time" + common_db "github.com/gitpod-io/gitpod/common-go/db" "github.com/gitpod-io/gitpod/usage/pkg/db" "github.com/google/uuid" "github.com/stretchr/testify/require" @@ -33,31 +34,31 @@ func NewWorkspaceInstance(t *testing.T, instance db.WorkspaceInstance) db.Worksp workspaceID = instance.WorkspaceID } - creationTime := db.VarcharTime{} + creationTime := common_db.VarcharTime{} if instance.CreationTime.IsSet() { creationTime = instance.CreationTime } else if instance.StartedTime.IsSet() { creationTime = instance.StartedTime } - startedTime := db.VarcharTime{} + startedTime := common_db.VarcharTime{} if instance.StartedTime.IsSet() { startedTime = instance.StartedTime } - deployedTime := db.VarcharTime{} + deployedTime := common_db.VarcharTime{} if instance.DeployedTime.IsSet() { deployedTime = instance.DeployedTime } - stoppedTime := db.VarcharTime{} + stoppedTime := common_db.VarcharTime{} if instance.StoppedTime.IsSet() { stoppedTime = instance.StoppedTime } else if instance.StoppingTime.IsSet() { creationTime = instance.StoppingTime } - stoppingTime := db.VarcharTime{} + stoppingTime := common_db.VarcharTime{} if instance.StoppingTime.IsSet() { stoppingTime = instance.StoppingTime } diff --git a/components/usage/pkg/db/project.go b/components/usage/pkg/db/project.go index dfabf3996778f9..c12ed2b6b50cc8 100644 --- a/components/usage/pkg/db/project.go +++ b/components/usage/pkg/db/project.go @@ -6,9 +6,11 @@ package db import ( "database/sql" + "time" + + common_db "github.com/gitpod-io/gitpod/common-go/db" "github.com/google/uuid" "gorm.io/datatypes" - "time" ) type Project struct { @@ -20,8 +22,8 @@ type Project struct { AppInstallationID string `gorm:"column:appInstallationId;type:varchar;size:255;" json:"appInstallationId"` - CreationTime VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"` - LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"` + CreationTime common_db.VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"` + LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"` TeamID sql.NullString `gorm:"column:teamId;type:char;size:36;" json:"teamId"` UserID sql.NullString `gorm:"column:userId;type:char;size:36;" json:"userId"` diff --git a/components/usage/pkg/db/stripe_customer.go b/components/usage/pkg/db/stripe_customer.go index ef4690b1c27152..3e101f4d370721 100644 --- a/components/usage/pkg/db/stripe_customer.go +++ b/components/usage/pkg/db/stripe_customer.go @@ -10,13 +10,14 @@ import ( "fmt" "time" + common_db "github.com/gitpod-io/gitpod/common-go/db" "gorm.io/gorm" ) type StripeCustomer struct { - StripeCustomerID string `gorm:"primary_key;column:stripeCustomerId;type:char;size:255;" json:"stripeCustomerId"` - AttributionID AttributionID `gorm:"column:attributionId;type:varchar;size:255;" json:"attributionId"` - CreationTime VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"` + StripeCustomerID string `gorm:"primary_key;column:stripeCustomerId;type:char;size:255;" json:"stripeCustomerId"` + AttributionID AttributionID `gorm:"column:attributionId;type:varchar;size:255;" json:"attributionId"` + CreationTime common_db.VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"` LastModified time.Time `gorm:"->;column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"` // deleted is reserved for use by db-sync. @@ -30,7 +31,7 @@ func (d *StripeCustomer) TableName() string { func CreateStripeCustomer(ctx context.Context, conn *gorm.DB, customer StripeCustomer) error { if !customer.CreationTime.IsSet() { - customer.CreationTime = NewVarcharTime(time.Now()) + customer.CreationTime = common_db.NewVarCharTime(time.Now()) } tx := conn.WithContext(ctx).Create(customer) diff --git a/components/usage/pkg/db/stripe_customer_test.go b/components/usage/pkg/db/stripe_customer_test.go index f85149620c108d..ad06351fe6c814 100644 --- a/components/usage/pkg/db/stripe_customer_test.go +++ b/components/usage/pkg/db/stripe_customer_test.go @@ -9,6 +9,7 @@ import ( "testing" "time" + common_db "github.com/gitpod-io/gitpod/common-go/db" "github.com/gitpod-io/gitpod/usage/pkg/db" "github.com/gitpod-io/gitpod/usage/pkg/db/dbtest" "github.com/google/uuid" @@ -21,7 +22,7 @@ func TestCreateStripeCustomer(t *testing.T) { customer := db.StripeCustomer{ StripeCustomerID: "cus_1234", AttributionID: db.NewUserAttributionID(uuid.New().String()), - CreationTime: db.NewVarcharTime(time.Now()), + CreationTime: common_db.NewVarCharTime(time.Now()), } t.Cleanup(func() { require.NoError(t, conn.Delete(&customer).Error) @@ -60,11 +61,11 @@ func TestGetStripeCustomerByAttributionID_ReturnsLatestRecord(t *testing.T) { attributionID := db.NewTeamAttributionID(uuid.New().String()) first := dbtest.NewStripeCustomer(t, db.StripeCustomer{ AttributionID: attributionID, - CreationTime: db.NewVarcharTime(now.Add(-1 * time.Hour)), + CreationTime: common_db.NewVarCharTime(now.Add(-1 * time.Hour)), }) second := dbtest.NewStripeCustomer(t, db.StripeCustomer{ AttributionID: attributionID, - CreationTime: db.NewVarcharTime(now), + CreationTime: common_db.NewVarCharTime(now), }) dbtest.CreateStripeCustomers(t, conn, first, second) diff --git a/components/usage/pkg/db/team.go b/components/usage/pkg/db/team.go index 1f20ff27d728b2..15754aa964b155 100644 --- a/components/usage/pkg/db/team.go +++ b/components/usage/pkg/db/team.go @@ -5,8 +5,10 @@ package db import ( - "github.com/google/uuid" "time" + + common_db "github.com/gitpod-io/gitpod/common-go/db" + "github.com/google/uuid" ) type Team struct { @@ -14,8 +16,8 @@ type Team struct { Name string `gorm:"column:name;type:varchar;size:255;" json:"name"` Slug string `gorm:"column:slug;type:varchar;size:255;" json:"slug"` - CreationTime VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"` - LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"` + CreationTime common_db.VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"` + LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"` MarkedDeleted bool `gorm:"column:markedDeleted;type:tinyint;default:0;" json:"marked_deleted"` diff --git a/components/usage/pkg/db/team_membership.go b/components/usage/pkg/db/team_membership.go index b34b8024948045..3d2c96464c3581 100644 --- a/components/usage/pkg/db/team_membership.go +++ b/components/usage/pkg/db/team_membership.go @@ -7,9 +7,11 @@ package db import ( "context" "fmt" + "time" + + common_db "github.com/gitpod-io/gitpod/common-go/db" "github.com/google/uuid" "gorm.io/gorm" - "time" ) type TeamMembership struct { @@ -20,7 +22,7 @@ type TeamMembership struct { Role TeamMembershipRole `gorm:"column:role;type:varchar;size:255;" json:"role"` SubscriptionID uuid.UUID `gorm:"column:subscriptionId;type:char;size:36;" json:"subscriptionId"` - CreationTime VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"` + CreationTime common_db.VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"` // Read-only (-> property). LastModified time.Time `gorm:"->:column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"` diff --git a/components/usage/pkg/db/usage.go b/components/usage/pkg/db/usage.go index 5f6af872993ec2..d7fecece85801b 100644 --- a/components/usage/pkg/db/usage.go +++ b/components/usage/pkg/db/usage.go @@ -11,6 +11,7 @@ import ( "math" "time" + common_db "github.com/gitpod-io/gitpod/common-go/db" "github.com/google/uuid" "gorm.io/datatypes" "gorm.io/gorm" @@ -36,15 +37,15 @@ func (cc CreditCents) ToCredits() float64 { } type Usage struct { - ID uuid.UUID `gorm:"primary_key;column:id;type:char;size:36;" json:"id"` - AttributionID AttributionID `gorm:"column:attributionId;type:varchar;size:255;" json:"attributionId"` - Description string `gorm:"column:description;type:varchar;size:255;" json:"description"` - CreditCents CreditCents `gorm:"column:creditCents;type:bigint;" json:"creditCents"` - EffectiveTime VarcharTime `gorm:"column:effectiveTime;type:varchar;size:255;" json:"effectiveTime"` - Kind UsageKind `gorm:"column:kind;type:char;size:10;" json:"kind"` - WorkspaceInstanceID *uuid.UUID `gorm:"column:workspaceInstanceId;type:char;size:36;" json:"workspaceInstanceId"` - Draft bool `gorm:"column:draft;type:boolean;" json:"draft"` - Metadata datatypes.JSON `gorm:"column:metadata;type:text;size:65535" json:"metadata"` + ID uuid.UUID `gorm:"primary_key;column:id;type:char;size:36;" json:"id"` + AttributionID AttributionID `gorm:"column:attributionId;type:varchar;size:255;" json:"attributionId"` + Description string `gorm:"column:description;type:varchar;size:255;" json:"description"` + CreditCents CreditCents `gorm:"column:creditCents;type:bigint;" json:"creditCents"` + EffectiveTime common_db.VarcharTime `gorm:"column:effectiveTime;type:varchar;size:255;" json:"effectiveTime"` + Kind UsageKind `gorm:"column:kind;type:char;size:10;" json:"kind"` + WorkspaceInstanceID *uuid.UUID `gorm:"column:workspaceInstanceId;type:char;size:36;" json:"workspaceInstanceId"` + Draft bool `gorm:"column:draft;type:boolean;" json:"draft"` + Metadata datatypes.JSON `gorm:"column:metadata;type:text;size:65535" json:"metadata"` } func (u *Usage) SetMetadataWithWorkspaceInstance(data WorkspaceInstanceUsageData) error { @@ -138,7 +139,7 @@ func FindUsage(ctx context.Context, conn *gorm.DB, params *FindUsageParams) ([]U db := conn.WithContext(ctx). Where("attributionId = ?", params.AttributionId). - Where("effectiveTime >= ? AND effectiveTime < ?", TimeToISO8601(params.From), TimeToISO8601(params.To)). + Where("effectiveTime >= ? AND effectiveTime < ?", common_db.TimeToISO8601(params.From), common_db.TimeToISO8601(params.To)). Where("kind = ?", WorkspaceInstanceUsageKind) if params.ExcludeDrafts { db = db.Where("draft = ?", false) @@ -178,7 +179,7 @@ func GetUsageSummary(ctx context.Context, conn *gorm.DB, params GetUsageSummaryP query1 := db.Table((&Usage{}).TableName()). Select("sum(creditCents) as CreditCentsUsed, count(*) as NumberOfRecords"). Where("attributionId = ?", params.AttributionId). - Where("effectiveTime >= ? AND effectiveTime < ?", TimeToISO8601(params.From), TimeToISO8601(params.To)). + Where("effectiveTime >= ? AND effectiveTime < ?", common_db.TimeToISO8601(params.From), common_db.TimeToISO8601(params.To)). Where("kind = ?", WorkspaceInstanceUsageKind) if params.ExcludeDrafts { query1 = query1.Where("draft = ?", false) diff --git a/components/usage/pkg/db/usage_test.go b/components/usage/pkg/db/usage_test.go index 71be3c187e8c5f..be2fc19a5f3e4e 100644 --- a/components/usage/pkg/db/usage_test.go +++ b/components/usage/pkg/db/usage_test.go @@ -10,6 +10,7 @@ import ( "testing" "time" + common_db "github.com/gitpod-io/gitpod/common-go/db" "github.com/gitpod-io/gitpod/usage/pkg/db" "github.com/gitpod-io/gitpod/usage/pkg/db/dbtest" "github.com/google/uuid" @@ -26,18 +27,18 @@ func TestFindUsageInRange(t *testing.T) { entryBefore := dbtest.NewUsage(t, db.Usage{ AttributionID: attributionID, - EffectiveTime: db.NewVarcharTime(start.Add(-1 * 23 * time.Hour)), + EffectiveTime: common_db.NewVarCharTime(start.Add(-1 * 23 * time.Hour)), Draft: true, }) entryInside := dbtest.NewUsage(t, db.Usage{ AttributionID: attributionID, - EffectiveTime: db.NewVarcharTime(start.Add(2 * time.Minute)), + EffectiveTime: common_db.NewVarCharTime(start.Add(2 * time.Minute)), }) entryAfter := dbtest.NewUsage(t, db.Usage{ AttributionID: attributionID, - EffectiveTime: db.NewVarcharTime(end.Add(2 * time.Hour)), + EffectiveTime: common_db.NewVarCharTime(end.Add(2 * time.Hour)), }) usageEntries := []db.Usage{entryBefore, entryInside, entryAfter} @@ -63,40 +64,40 @@ func TestGetUsageSummary(t *testing.T) { draftBefore := dbtest.NewUsage(t, db.Usage{ AttributionID: attributionID, - EffectiveTime: db.NewVarcharTime(start.Add(-1 * 23 * time.Hour)), + EffectiveTime: common_db.NewVarCharTime(start.Add(-1 * 23 * time.Hour)), CreditCents: 100, Draft: true, }) nondraftBefore := dbtest.NewUsage(t, db.Usage{ AttributionID: attributionID, - EffectiveTime: db.NewVarcharTime(start.Add(-1 * 23 * time.Hour)), + EffectiveTime: common_db.NewVarCharTime(start.Add(-1 * 23 * time.Hour)), CreditCents: 200, Draft: false, }) draftInside := dbtest.NewUsage(t, db.Usage{ AttributionID: attributionID, - EffectiveTime: db.NewVarcharTime(start.Add(2 * time.Hour)), + EffectiveTime: common_db.NewVarCharTime(start.Add(2 * time.Hour)), CreditCents: 300, Draft: true, }) nonDraftInside := dbtest.NewUsage(t, db.Usage{ AttributionID: attributionID, - EffectiveTime: db.NewVarcharTime(start.Add(2 * time.Hour)), + EffectiveTime: common_db.NewVarCharTime(start.Add(2 * time.Hour)), CreditCents: 400, Draft: false, }) nonDraftAfter := dbtest.NewUsage(t, db.Usage{ AttributionID: attributionID, - EffectiveTime: db.NewVarcharTime(end.Add(2 * time.Hour)), + EffectiveTime: common_db.NewVarCharTime(end.Add(2 * time.Hour)), CreditCents: 1000, }) invoice := dbtest.NewUsage(t, db.Usage{ AttributionID: attributionID, Kind: db.InvoiceUsageKind, - EffectiveTime: db.NewVarcharTime(start.Add(2 * time.Hour)), + EffectiveTime: common_db.NewVarCharTime(start.Add(2 * time.Hour)), CreditCents: -400, Draft: false, }) @@ -143,7 +144,7 @@ func TestInsertUsageRecords(t *testing.T) { usage := dbtest.NewUsage(t, db.Usage{ AttributionID: attributionID, - EffectiveTime: db.NewVarcharTime(start.Add(2 * time.Hour)), + EffectiveTime: common_db.NewVarCharTime(start.Add(2 * time.Hour)), Draft: true, }) @@ -178,7 +179,7 @@ func TestUpdateUsageRecords(t *testing.T) { usage := dbtest.NewUsage(t, db.Usage{ AttributionID: attributionID, - EffectiveTime: db.NewVarcharTime(start.Add(2 * time.Hour)), + EffectiveTime: common_db.NewVarCharTime(start.Add(2 * time.Hour)), Draft: true, }) @@ -203,17 +204,17 @@ func TestFindAllDraftUsage(t *testing.T) { usage1 := dbtest.NewUsage(t, db.Usage{ AttributionID: attributionID, - EffectiveTime: db.NewVarcharTime(start.Add(2 * time.Hour)), + EffectiveTime: common_db.NewVarCharTime(start.Add(2 * time.Hour)), Draft: true, }) usage2 := dbtest.NewUsage(t, db.Usage{ AttributionID: attributionID, - EffectiveTime: db.NewVarcharTime(start.Add(2 * time.Hour)), + EffectiveTime: common_db.NewVarCharTime(start.Add(2 * time.Hour)), Draft: true, }) usage3 := dbtest.NewUsage(t, db.Usage{ AttributionID: attributionID, - EffectiveTime: db.NewVarcharTime(start.Add(2 * time.Hour)), + EffectiveTime: common_db.NewVarCharTime(start.Add(2 * time.Hour)), Draft: false, }) diff --git a/components/usage/pkg/db/varchar_test.go b/components/usage/pkg/db/varchar_test.go index 7cc109ca788eeb..063ab321564345 100644 --- a/components/usage/pkg/db/varchar_test.go +++ b/components/usage/pkg/db/varchar_test.go @@ -5,18 +5,19 @@ package db_test import ( - "github.com/gitpod-io/gitpod/usage/pkg/db" + "testing" + + common_db "github.com/gitpod-io/gitpod/common-go/db" "github.com/gitpod-io/gitpod/usage/pkg/db/dbtest" "github.com/stretchr/testify/require" "gorm.io/gorm" - "testing" ) func TestVarcharTime_SerializeAndDeserialize(t *testing.T) { // Custom table to be able to exercise serialization easily, independent of other models type VarcharModel struct { - ID int `gorm:"primaryKey"` - Time db.VarcharTime `gorm:"column:time;type:varchar(255);"` + ID int `gorm:"primaryKey"` + Time common_db.VarcharTime `gorm:"column:time;type:varchar(255);"` } conn := dbtest.ConnectForTests(t) @@ -33,11 +34,11 @@ func TestVarcharTime_SerializeAndDeserialize(t *testing.T) { Description: "empty value for VarcharTime", Input: VarcharModel{ ID: 1, - Time: db.VarcharTime{}, + Time: common_db.VarcharTime{}, }, Expected: VarcharModel{ ID: 1, - Time: db.VarcharTime{}, + Time: common_db.VarcharTime{}, }, }, } { diff --git a/components/usage/pkg/db/workspace.go b/components/usage/pkg/db/workspace.go index 827fb2d2a0847c..829822aeb493a2 100644 --- a/components/usage/pkg/db/workspace.go +++ b/components/usage/pkg/db/workspace.go @@ -8,11 +8,13 @@ import ( "context" "database/sql" "fmt" + "math" + "time" + + common_db "github.com/gitpod-io/gitpod/common-go/db" "github.com/google/uuid" "gorm.io/datatypes" "gorm.io/gorm" - "math" - "time" ) // Workspace represents the underlying DB object @@ -33,10 +35,10 @@ type Workspace struct { ImageNameResolved string `gorm:"column:imageNameResolved;type:varchar;size:255;" json:"imageNameResolved"` BaseImageNameResolved string `gorm:"column:baseImageNameResolved;type:varchar;size:255;" json:"baseImageNameResolved"` - CreationTime VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"` - LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"` - SoftDeletedTime VarcharTime `gorm:"column:softDeletedTime;type:varchar;size:255;" json:"softDeletedTime"` - ContentDeletedTime VarcharTime `gorm:"column:contentDeletedTime;type:varchar;size:255;" json:"contentDeletedTime"` + CreationTime common_db.VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"` + LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"` + SoftDeletedTime common_db.VarcharTime `gorm:"column:softDeletedTime;type:varchar;size:255;" json:"softDeletedTime"` + ContentDeletedTime common_db.VarcharTime `gorm:"column:contentDeletedTime;type:varchar;size:255;" json:"contentDeletedTime"` Archived bool `gorm:"column:archived;type:tinyint;default:0;" json:"archived"` Shareable bool `gorm:"column:shareable;type:tinyint;default:0;" json:"shareable"` diff --git a/components/usage/pkg/db/workspace_instance.go b/components/usage/pkg/db/workspace_instance.go index f622057581aa35..4bf111b2023187 100644 --- a/components/usage/pkg/db/workspace_instance.go +++ b/components/usage/pkg/db/workspace_instance.go @@ -11,6 +11,7 @@ import ( "strings" "time" + common_db "github.com/gitpod-io/gitpod/common-go/db" "github.com/gitpod-io/gitpod/common-go/log" "github.com/google/uuid" @@ -30,12 +31,12 @@ type WorkspaceInstance struct { UsageAttributionID AttributionID `gorm:"column:usageAttributionId;type:varchar;size:60;" json:"usageAttributionId"` WorkspaceClass string `gorm:"column:workspaceClass;type:varchar;size:255;" json:"workspaceClass"` - CreationTime VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"` - StartedTime VarcharTime `gorm:"column:startedTime;type:varchar;size:255;" json:"startedTime"` - DeployedTime VarcharTime `gorm:"column:deployedTime;type:varchar;size:255;" json:"deployedTime"` - StoppedTime VarcharTime `gorm:"column:stoppedTime;type:varchar;size:255;" json:"stoppedTime"` - LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"` - StoppingTime VarcharTime `gorm:"column:stoppingTime;type:varchar;size:255;" json:"stoppingTime"` + CreationTime common_db.VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"` + StartedTime common_db.VarcharTime `gorm:"column:startedTime;type:varchar;size:255;" json:"startedTime"` + DeployedTime common_db.VarcharTime `gorm:"column:deployedTime;type:varchar;size:255;" json:"deployedTime"` + StoppedTime common_db.VarcharTime `gorm:"column:stoppedTime;type:varchar;size:255;" json:"stoppedTime"` + LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"` + StoppingTime common_db.VarcharTime `gorm:"column:stoppingTime;type:varchar;size:255;" json:"stoppingTime"` LastHeartbeat string `gorm:"column:lastHeartbeat;type:varchar;size:255;" json:"lastHeartbeat"` StatusOld sql.NullString `gorm:"column:status_old;type:varchar;size:255;" json:"status_old"` @@ -59,8 +60,8 @@ func FindStoppedWorkspaceInstancesInRange(ctx context.Context, conn *gorm.DB, fr var instancesInBatch []WorkspaceInstanceForUsage tx := queryWorkspaceInstanceForUsage(ctx, conn). - Where("wsi.stoppingTime >= ?", TimeToISO8601(from)). - Where("wsi.stoppingTime < ?", TimeToISO8601(to)). + Where("wsi.stoppingTime >= ?", common_db.TimeToISO8601(from)). + Where("wsi.stoppingTime < ?", common_db.TimeToISO8601(to)). Where("wsi.stoppingTime != ?", ""). Where("wsi.usageAttributionId != ?", ""). FindInBatches(&instancesInBatch, 1000, func(_ *gorm.DB, _ int) error { @@ -83,7 +84,7 @@ func FindRunningWorkspaceInstances(ctx context.Context, conn *gorm.DB) ([]Worksp Where("wsi.stoppingTime = ?", ""). Where("wsi.usageAttributionId != ?", ""). // We cannot guarantee data quality before this date - Where("wsi.startedTime > ?", TimeToISO8601(time.Date(2022, 8, 1, 0, 0, 0, 0, time.UTC))). + Where("wsi.startedTime > ?", common_db.TimeToISO8601(time.Date(2022, 8, 1, 0, 0, 0, 0, time.UTC))). FindInBatches(&instancesInBatch, 1000, func(_ *gorm.DB, _ int) error { instances = append(instances, instancesInBatch...) return nil @@ -219,8 +220,8 @@ type WorkspaceInstanceForUsage struct { UserName string `gorm:"column:userName;type:varchar;size:255;" json:"userName"` UserAvatarURL string `gorm:"column:userAvatarURL;type:varchar;size:255;" json:"userAvatarURL"` - StartedTime VarcharTime `gorm:"column:startedTime;type:varchar;size:255;" json:"startedTime"` - StoppingTime VarcharTime `gorm:"column:stoppingTime;type:varchar;size:255;" json:"stoppingTime"` + StartedTime common_db.VarcharTime `gorm:"column:startedTime;type:varchar;size:255;" json:"startedTime"` + StoppingTime common_db.VarcharTime `gorm:"column:stoppingTime;type:varchar;size:255;" json:"stoppingTime"` } // WorkspaceRuntimeSeconds computes how long this WorkspaceInstance has been running. @@ -237,10 +238,10 @@ func (i *WorkspaceInstanceForUsage) WorkspaceRuntimeSeconds(stopTimeIfInstanceIs log. WithField("instance_id", i.ID). WithField("workspace_id", i.WorkspaceID). - WithField("started_time", TimeToISO8601(i.StartedTime.Time())). + WithField("started_time", common_db.TimeToISO8601(i.StartedTime.Time())). WithField("started_time_set", i.StartedTime.IsSet()). WithField("stopping_time_set", i.StartedTime.IsSet()). - WithField("stopping_time", TimeToISO8601(i.StartedTime.Time())). + WithField("stopping_time", common_db.TimeToISO8601(i.StartedTime.Time())). WithField("stop_time_if_instance_still_running", stopTimeIfInstanceIsStillRunning). Errorf("Instance %s had stop time before start time. Using startedTime as stop time.", i.ID) diff --git a/components/usage/pkg/db/workspace_instance_test.go b/components/usage/pkg/db/workspace_instance_test.go index df1a924e253cb8..ff777a82398006 100644 --- a/components/usage/pkg/db/workspace_instance_test.go +++ b/components/usage/pkg/db/workspace_instance_test.go @@ -7,10 +7,12 @@ package db_test import ( "context" "fmt" - "github.com/gitpod-io/gitpod/usage/pkg/apiv1" "testing" "time" + "github.com/gitpod-io/gitpod/usage/pkg/apiv1" + + common_db "github.com/gitpod-io/gitpod/common-go/db" "github.com/gitpod-io/gitpod/usage/pkg/db" "github.com/gitpod-io/gitpod/usage/pkg/db/dbtest" "github.com/google/uuid" @@ -31,29 +33,29 @@ func TestFindStoppedWorkspaceInstancesInRange(t *testing.T) { // In the middle of May dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ WorkspaceID: workspace.ID, - StartedTime: db.NewVarcharTime(time.Date(2022, 05, 15, 12, 00, 00, 00, time.UTC)), - StoppingTime: db.NewVarcharTime(time.Date(2022, 05, 15, 13, 00, 00, 00, time.UTC)), + StartedTime: common_db.NewVarCharTime(time.Date(2022, 05, 15, 12, 00, 00, 00, time.UTC)), + StoppingTime: common_db.NewVarCharTime(time.Date(2022, 05, 15, 13, 00, 00, 00, time.UTC)), }), // Start of May dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ ID: uuid.New(), WorkspaceID: workspace.ID, - StartedTime: db.NewVarcharTime(time.Date(2022, 05, 1, 0, 00, 00, 00, time.UTC)), - StoppingTime: db.NewVarcharTime(time.Date(2022, 05, 1, 1, 00, 00, 00, time.UTC)), + StartedTime: common_db.NewVarCharTime(time.Date(2022, 05, 1, 0, 00, 00, 00, time.UTC)), + StoppingTime: common_db.NewVarCharTime(time.Date(2022, 05, 1, 1, 00, 00, 00, time.UTC)), }), // End of May dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ ID: uuid.New(), WorkspaceID: workspace.ID, - StartedTime: db.NewVarcharTime(time.Date(2022, 05, 31, 23, 00, 00, 00, time.UTC)), - StoppingTime: db.NewVarcharTime(time.Date(2022, 05, 31, 23, 59, 59, 999999, time.UTC)), + StartedTime: common_db.NewVarCharTime(time.Date(2022, 05, 31, 23, 00, 00, 00, time.UTC)), + StoppingTime: common_db.NewVarCharTime(time.Date(2022, 05, 31, 23, 59, 59, 999999, time.UTC)), }), // Started in April, but continued into May dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ ID: uuid.New(), WorkspaceID: workspace.ID, - StartedTime: db.NewVarcharTime(time.Date(2022, 04, 30, 23, 00, 00, 00, time.UTC)), - StoppingTime: db.NewVarcharTime(time.Date(2022, 05, 1, 0, 0, 0, 0, time.UTC)), + StartedTime: common_db.NewVarCharTime(time.Date(2022, 04, 30, 23, 00, 00, 00, time.UTC)), + StoppingTime: common_db.NewVarCharTime(time.Date(2022, 05, 1, 0, 0, 0, 0, time.UTC)), }), } invalid := []db.WorkspaceInstance{ @@ -61,28 +63,28 @@ func TestFindStoppedWorkspaceInstancesInRange(t *testing.T) { dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ ID: uuid.New(), WorkspaceID: workspace.ID, - StartedTime: db.NewVarcharTime(time.Date(2022, 04, 31, 23, 00, 00, 00, time.UTC)), + StartedTime: common_db.NewVarCharTime(time.Date(2022, 04, 31, 23, 00, 00, 00, time.UTC)), }), // Started in May, but continued into June dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ ID: uuid.New(), WorkspaceID: workspace.ID, - StartedTime: db.NewVarcharTime(time.Date(2022, 05, 31, 23, 00, 00, 00, time.UTC)), - StoppingTime: db.NewVarcharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)), + StartedTime: common_db.NewVarCharTime(time.Date(2022, 05, 31, 23, 00, 00, 00, time.UTC)), + StoppingTime: common_db.NewVarCharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)), }), // Started in April, but continued into June (ran for all of May) dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ ID: uuid.New(), WorkspaceID: workspace.ID, - StartedTime: db.NewVarcharTime(time.Date(2022, 04, 31, 23, 00, 00, 00, time.UTC)), - StoppingTime: db.NewVarcharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)), + StartedTime: common_db.NewVarCharTime(time.Date(2022, 04, 31, 23, 00, 00, 00, time.UTC)), + StoppingTime: common_db.NewVarCharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)), }), // Start of June dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ ID: uuid.New(), WorkspaceID: workspace.ID, - StartedTime: db.NewVarcharTime(time.Date(2022, 06, 1, 00, 00, 00, 00, time.UTC)), - StoppingTime: db.NewVarcharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)), + StartedTime: common_db.NewVarCharTime(time.Date(2022, 06, 1, 00, 00, 00, 00, time.UTC)), + StoppingTime: common_db.NewVarCharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)), }), } @@ -128,24 +130,24 @@ func TestFindRunningWorkspace(t *testing.T) { // one stopped instance dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ WorkspaceID: workspace.ID, - StartedTime: db.NewVarcharTime(time.Date(2022, 05, 15, 12, 00, 00, 00, time.UTC)), - StoppingTime: db.NewVarcharTime(time.Date(2022, 05, 15, 13, 00, 00, 00, time.UTC)), + StartedTime: common_db.NewVarCharTime(time.Date(2022, 05, 15, 12, 00, 00, 00, time.UTC)), + StoppingTime: common_db.NewVarCharTime(time.Date(2022, 05, 15, 13, 00, 00, 00, time.UTC)), }), // one before August 2022, excluded dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ WorkspaceID: workspace.ID, - StartedTime: db.NewVarcharTime(time.Date(2022, 05, 15, 12, 00, 00, 00, time.UTC)), + StartedTime: common_db.NewVarCharTime(time.Date(2022, 05, 15, 12, 00, 00, 00, time.UTC)), }), // Two running instances dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ ID: uuid.New(), WorkspaceID: workspace.ID, - StartedTime: db.NewVarcharTime(time.Date(2022, 9, 1, 0, 00, 00, 00, time.UTC)), + StartedTime: common_db.NewVarCharTime(time.Date(2022, 9, 1, 0, 00, 00, 00, time.UTC)), }), dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ ID: uuid.New(), WorkspaceID: workspace.ID, - StartedTime: db.NewVarcharTime(time.Date(2022, 9, 30, 23, 00, 00, 00, time.UTC)), + StartedTime: common_db.NewVarCharTime(time.Date(2022, 9, 30, 23, 00, 00, 00, time.UTC)), }), } @@ -169,19 +171,19 @@ func TestFindWorkspacesByInstanceId(t *testing.T) { // one stopped instance dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ WorkspaceID: workspace.ID, - StartedTime: db.NewVarcharTime(time.Date(2022, 05, 15, 12, 00, 00, 00, time.UTC)), - StoppingTime: db.NewVarcharTime(time.Date(2022, 05, 15, 13, 00, 00, 00, time.UTC)), + StartedTime: common_db.NewVarCharTime(time.Date(2022, 05, 15, 12, 00, 00, 00, time.UTC)), + StoppingTime: common_db.NewVarCharTime(time.Date(2022, 05, 15, 13, 00, 00, 00, time.UTC)), }), // Two running instances dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ ID: uuid.New(), WorkspaceID: workspace.ID, - StartedTime: db.NewVarcharTime(time.Date(2022, 05, 1, 0, 00, 00, 00, time.UTC)), + StartedTime: common_db.NewVarCharTime(time.Date(2022, 05, 1, 0, 00, 00, 00, time.UTC)), }), dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ ID: uuid.New(), WorkspaceID: workspace.ID, - StartedTime: db.NewVarcharTime(time.Date(2022, 04, 30, 23, 00, 00, 00, time.UTC)), + StartedTime: common_db.NewVarCharTime(time.Date(2022, 04, 30, 23, 00, 00, 00, time.UTC)), }), } @@ -212,8 +214,8 @@ func TestWorkspaceInstanceForUsage_WorkspaceRuntimeSeconds(t *testing.T) { Name: "does not use stop time if still running if the instance stopped", Instance: &db.WorkspaceInstanceForUsage{ WorkspaceClass: db.WorkspaceClass_Default, - StartedTime: db.NewVarcharTime(time.Date(2022, 9, 8, 12, 0, 0, 0, time.UTC)), - StoppingTime: db.NewVarcharTime(time.Date(2022, 9, 8, 12, 6, 0, 0, time.UTC)), + StartedTime: common_db.NewVarCharTime(time.Date(2022, 9, 8, 12, 0, 0, 0, time.UTC)), + StoppingTime: common_db.NewVarCharTime(time.Date(2022, 9, 8, 12, 6, 0, 0, time.UTC)), }, // Override is before actual stop time StopTimeIfStillRunning: time.Date(2022, 9, 8, 11, 21, 29, 00, time.UTC), @@ -223,7 +225,7 @@ func TestWorkspaceInstanceForUsage_WorkspaceRuntimeSeconds(t *testing.T) { Name: "uses stop time when instance is not stopped", Instance: &db.WorkspaceInstanceForUsage{ WorkspaceClass: db.WorkspaceClass_Default, - StartedTime: db.NewVarcharTime(time.Date(2022, 9, 8, 12, 0, 0, 0, time.UTC)), + StartedTime: common_db.NewVarCharTime(time.Date(2022, 9, 8, 12, 0, 0, 0, time.UTC)), }, StopTimeIfStillRunning: time.Date(2022, 9, 8, 12, 12, 0, 00, time.UTC), ExpectedCredits: 2, @@ -232,7 +234,7 @@ func TestWorkspaceInstanceForUsage_WorkspaceRuntimeSeconds(t *testing.T) { Name: "uses creation time when stop time if still running is less than started time", Instance: &db.WorkspaceInstanceForUsage{ WorkspaceClass: db.WorkspaceClass_Default, - StartedTime: db.NewVarcharTime(time.Date(2022, 9, 8, 12, 0, 0, 0, time.UTC)), + StartedTime: common_db.NewVarCharTime(time.Date(2022, 9, 8, 12, 0, 0, 0, time.UTC)), }, StopTimeIfStillRunning: time.Date(2022, 9, 8, 11, 0, 0, 00, time.UTC), ExpectedCredits: 0, @@ -250,11 +252,11 @@ func TestListWorkspaceInstanceIDsWithPhaseStoppedButNoStoppingTime(t *testing.T) instances := dbtest.CreateWorkspaceInstances(t, dbconn, // started but not stopped, should be ignored dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ - StartedTime: db.NewVarcharTime(time.Now()), + StartedTime: common_db.NewVarCharTime(time.Now()), }), // stopped, but no stopping time, should be detected dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{ - StartedTime: db.NewVarcharTime(time.Now()), + StartedTime: common_db.NewVarCharTime(time.Now()), PhasePersisted: "stopped", }), ) diff --git a/components/usage/pkg/db/workspace_test.go b/components/usage/pkg/db/workspace_test.go index fdbd6467188666..6207bce00a8d8c 100644 --- a/components/usage/pkg/db/workspace_test.go +++ b/components/usage/pkg/db/workspace_test.go @@ -6,16 +6,18 @@ package db_test import ( "context" + "testing" + + common_db "github.com/gitpod-io/gitpod/common-go/db" "github.com/gitpod-io/gitpod/usage/pkg/db" "github.com/gitpod-io/gitpod/usage/pkg/db/dbtest" "github.com/stretchr/testify/require" - "testing" ) -func stringToVarchar(t *testing.T, s string) db.VarcharTime { +func stringToVarchar(t *testing.T, s string) common_db.VarcharTime { t.Helper() - converted, err := db.NewVarcharTimeFromStr(s) + converted, err := common_db.NewVarCharTimeFromStr(s) require.NoError(t, err) return converted } diff --git a/components/usage/pkg/server/server.go b/components/usage/pkg/server/server.go index b7d99a6a6f85e2..79047207950a71 100644 --- a/components/usage/pkg/server/server.go +++ b/components/usage/pkg/server/server.go @@ -18,6 +18,7 @@ import ( "google.golang.org/grpc/credentials/insecure" "github.com/gitpod-io/gitpod/common-go/baseserver" + 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/apiv1" @@ -50,7 +51,7 @@ type Config struct { func Start(cfg Config, version string) error { log.WithField("config", cfg).Info("Starting usage component.") - conn, err := db.Connect(db.ConnectionParams{ + conn, err := common_db.Connect(common_db.ConnectionParams{ User: os.Getenv("DB_USERNAME"), Password: os.Getenv("DB_PASSWORD"), Host: net.JoinHostPort(os.Getenv("DB_HOST"), os.Getenv("DB_PORT")), diff --git a/install/installer/go.mod b/install/installer/go.mod index b23e5e6160fa6a..4761c244267969 100644 --- a/install/installer/go.mod +++ b/install/installer/go.mod @@ -181,7 +181,7 @@ require ( github.com/jbenet/goprocess v0.1.4 // indirect github.com/jinzhu/copier v0.3.5 // 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/jmoiron/sqlx v1.3.5 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/josharian/native v0.0.0-20200817173448-b6b71def0850 // indirect @@ -303,8 +303,8 @@ require ( gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect gorm.io/datatypes v1.0.6 // indirect - gorm.io/driver/mysql v1.3.3 // indirect - gorm.io/gorm v1.23.5 // indirect + gorm.io/driver/mysql v1.4.4 // indirect + gorm.io/gorm v1.24.1 // indirect honnef.co/go/tools v0.2.2 // indirect k8s.io/apiextensions-apiserver v0.24.2 // indirect k8s.io/apiserver v0.24.4 // indirect diff --git a/install/installer/go.sum b/install/installer/go.sum index 6f4825a93e399f..ffac3977c30ec5 100644 --- a/install/installer/go.sum +++ b/install/installer/go.sum @@ -1175,8 +1175,9 @@ github.com/jinzhu/copier v0.3.5 h1:GlvfUwHk62RokgqVNvYsku0TATCF7bAHVwEXoBh3iJg= github.com/jinzhu/copier v0.3.5/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= -github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas= github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmespath/go-jmespath v0.0.0-20160803190731-bd40a432e4c7/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= @@ -2984,8 +2985,8 @@ gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/datatypes v1.0.6 h1:3cqbakp1DIgC+P7wyODb5k+lSjW8g3mjkg/BIsmhjlE= gorm.io/datatypes v1.0.6/go.mod h1:Gh/Xd/iUWWybMEk8CzYCK/swqlni2r+ROeM1HGIM0ck= gorm.io/driver/mysql v1.3.2/go.mod h1:ChK6AHbHgDCFZyJp0F+BmVGb06PSIoh9uVYKAlRbb2U= -gorm.io/driver/mysql v1.3.3 h1:jXG9ANrwBc4+bMvBcSl8zCfPBaVoPyBEBshA8dA93X8= -gorm.io/driver/mysql v1.3.3/go.mod h1:ChK6AHbHgDCFZyJp0F+BmVGb06PSIoh9uVYKAlRbb2U= +gorm.io/driver/mysql v1.4.4 h1:MX0K9Qvy0Na4o7qSC/YI7XxqUw5KDw01umqgID+svdQ= +gorm.io/driver/mysql v1.4.4/go.mod h1:BCg8cKI+R0j/rZRQxeKis/forqRwRSYOR8OM3Wo6hOM= gorm.io/driver/postgres v1.3.1 h1:Pyv+gg1Gq1IgsLYytj/S2k7ebII3CzEdpqQkPOdH24g= gorm.io/driver/postgres v1.3.1/go.mod h1:WwvWOuR9unCLpGWCL6Y3JOeBWvbKi6JLhayiVclSZZU= gorm.io/driver/sqlite v1.3.1 h1:bwfE+zTEWklBYoEodIOIBwuWHpnx52Z9zJFW5F33WLk= @@ -2994,8 +2995,9 @@ gorm.io/driver/sqlserver v1.3.1 h1:F5t6ScMzOgy1zukRTIZgLZwKahgt3q1woAILVolKpOI= gorm.io/driver/sqlserver v1.3.1/go.mod h1:w25Vrx2BG+CJNUu/xKbFhaKlGxT/nzRkhWCCoptX8tQ= gorm.io/gorm v1.23.1/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= gorm.io/gorm v1.23.2/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= -gorm.io/gorm v1.23.5 h1:TnlF26wScKSvknUC/Rn8t0NLLM22fypYBlvj1+aH6dM= -gorm.io/gorm v1.23.5/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= +gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= +gorm.io/gorm v1.24.1 h1:CgvzRniUdG67hBAzsxDGOAuq4Te1osVMYsa1eQbd4fs= +gorm.io/gorm v1.24.1/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= diff --git a/test/go.mod b/test/go.mod index 49060b0305416f..62803d7922ffad 100644 --- a/test/go.mod +++ b/test/go.mod @@ -10,9 +10,10 @@ require ( github.com/gitpod-io/gitpod/image-builder/api v0.0.0-00010101000000-000000000000 github.com/gitpod-io/gitpod/supervisor/api v0.0.0-00010101000000-000000000000 github.com/gitpod-io/gitpod/ws-manager/api v0.0.0-00010101000000-000000000000 - github.com/go-sql-driver/mysql v1.5.0 + github.com/go-sql-driver/mysql v1.6.0 github.com/google/go-github/v42 v42.0.0 github.com/google/uuid v1.3.0 + github.com/helloyi/go-sshclient v1.1.1 github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0 github.com/prometheus/procfs v0.8.0 golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e @@ -74,7 +75,6 @@ require ( github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3 // indirect github.com/hashicorp/golang-lru v0.5.4 // indirect - github.com/helloyi/go-sshclient v1.1.1 // indirect github.com/heptiolabs/healthcheck v0.0.0-20211123025425-613501dd5deb // indirect github.com/imdario/mergo v0.3.12 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect diff --git a/test/go.sum b/test/go.sum index 7b3f2d4f0c36c8..a1262ac9e26ec4 100644 --- a/test/go.sum +++ b/test/go.sum @@ -206,8 +206,8 @@ github.com/go-openapi/swag v0.19.14 h1:gm3vOOXfiuw5i9p5N9xJvfjvuofpyvLA9Wr6QfK5F github.com/go-openapi/swag v0.19.14/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ= github.com/go-ozzo/ozzo-validation v3.6.0+incompatible h1:msy24VGS42fKO9K1vLz82/GeYW1cILu7Nuuj1N3BBkE= github.com/go-ozzo/ozzo-validation v3.6.0+incompatible/go.mod h1:gsEKFIVnabGBt6mXmxK0MoFy+cZoTJY6mu5Ll3LVLBU= -github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= -github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= +github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=