Skip to content

Commit 4d3e646

Browse files
Laurie T. MalaueasyCZ
authored andcommitted
[db] Move db models to gitpod-db/go module
1 parent 96874fa commit 4d3e646

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+648
-352
lines changed

components/common-go/go.mod

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ require (
3434
k8s.io/apimachinery v0.24.4
3535
)
3636

37-
require (
38-
github.com/go-sql-driver/mysql v1.6.0
39-
github.com/relvacode/iso8601 v1.1.0
40-
gorm.io/driver/mysql v1.4.4
41-
gorm.io/gorm v1.24.1
42-
)
43-
4437
require (
4538
github.com/beorn7/perks v1.0.1 // indirect
4639
github.com/blang/semver v3.5.1+incompatible // indirect
@@ -54,8 +47,6 @@ require (
5447
github.com/gogo/protobuf v1.3.2 // indirect
5548
github.com/golang/protobuf v1.5.2 // indirect
5649
github.com/google/gofuzz v1.1.0 // indirect
57-
github.com/jinzhu/inflection v1.0.0 // indirect
58-
github.com/jinzhu/now v1.1.5 // indirect
5950
github.com/json-iterator/go v1.1.12 // indirect
6051
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
6152
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect

components/common-go/go.sum

Lines changed: 0 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/gitpod-db/go/BUILD.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ packages:
66
- name: lib
77
type: go
88
srcs:
9-
- "**"
9+
- "**/*.go"
10+
- "go.mod"
11+
- "go.sum"
12+
deps:
13+
- :init-testdb
14+
- components/common-go:lib
1015
config:
1116
packaging: library
17+
18+
- name: init-testdb
19+
type: generic
20+
deps:
21+
- components/gitpod-db:dbtest-init
22+
ephemeral: true

components/common-go/db/conn.go renamed to components/gitpod-db/go/conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the GNU Affero General Public License (AGPL).
33
// See License-AGPL.txt in the project root for license information.
44

5-
package common_db
5+
package db
66

77
import (
88
"fmt"

components/usage/pkg/db/cost_center.go renamed to components/gitpod-db/go/cost_center.go

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"fmt"
1111
"time"
1212

13-
common_db "github.com/gitpod-io/gitpod/common-go/db"
1413
"github.com/gitpod-io/gitpod/common-go/log"
1514
"github.com/google/uuid"
1615
"google.golang.org/grpc/codes"
@@ -28,13 +27,13 @@ const (
2827
)
2928

3029
type CostCenter struct {
31-
ID AttributionID `gorm:"primary_key;column:id;type:char;size:36;" json:"id"`
32-
CreationTime common_db.VarcharTime `gorm:"primary_key;column:creationTime;type:varchar;size:255;" json:"creationTime"`
33-
SpendingLimit int32 `gorm:"column:spendingLimit;type:int;default:0;" json:"spendingLimit"`
34-
BillingStrategy BillingStrategy `gorm:"column:billingStrategy;type:varchar;size:255;" json:"billingStrategy"`
35-
BillingCycleStart common_db.VarcharTime `gorm:"column:billingCycleStart;type:varchar;size:255;" json:"billingCycleStart"`
36-
NextBillingTime common_db.VarcharTime `gorm:"column:nextBillingTime;type:varchar;size:255;" json:"nextBillingTime"`
37-
LastModified time.Time `gorm:"->;column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"`
30+
ID AttributionID `gorm:"primary_key;column:id;type:char;size:36;" json:"id"`
31+
CreationTime VarcharTime `gorm:"primary_key;column:creationTime;type:varchar;size:255;" json:"creationTime"`
32+
SpendingLimit int32 `gorm:"column:spendingLimit;type:int;default:0;" json:"spendingLimit"`
33+
BillingStrategy BillingStrategy `gorm:"column:billingStrategy;type:varchar;size:255;" json:"billingStrategy"`
34+
BillingCycleStart VarcharTime `gorm:"column:billingCycleStart;type:varchar;size:255;" json:"billingCycleStart"`
35+
NextBillingTime VarcharTime `gorm:"column:nextBillingTime;type:varchar;size:255;" json:"nextBillingTime"`
36+
LastModified time.Time `gorm:"->;column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"`
3837
}
3938

4039
// TableName sets the insert table name for this struct type
@@ -84,11 +83,11 @@ func (c *CostCenterManager) GetOrCreateCostCenter(ctx context.Context, attributi
8483
}
8584
result = CostCenter{
8685
ID: attributionID,
87-
CreationTime: common_db.NewVarCharTime(now),
86+
CreationTime: NewVarCharTime(now),
8887
BillingStrategy: CostCenter_Other,
8988
SpendingLimit: defaultSpendingLimit,
90-
BillingCycleStart: common_db.NewVarCharTime(now),
91-
NextBillingTime: common_db.NewVarCharTime(now.AddDate(0, 1, 0)),
89+
BillingCycleStart: NewVarCharTime(now),
90+
NextBillingTime: NewVarCharTime(now.AddDate(0, 1, 0)),
9291
}
9392
err := c.conn.Save(&result).Error
9493
if err != nil {
@@ -147,7 +146,7 @@ func (c *CostCenterManager) UpdateCostCenter(ctx context.Context, newCC CostCent
147146
now := time.Now()
148147

149148
// we always update the creationTime
150-
newCC.CreationTime = common_db.NewVarCharTime(now)
149+
newCC.CreationTime = NewVarCharTime(now)
151150
// we don't allow setting billingCycleStart or nextBillingTime from outside
152151
newCC.BillingCycleStart = existingCC.BillingCycleStart
153152
newCC.NextBillingTime = existingCC.NextBillingTime
@@ -173,9 +172,9 @@ func (c *CostCenterManager) UpdateCostCenter(ctx context.Context, newCC CostCent
173172
// Downgrading from stripe
174173
if existingCC.BillingStrategy == CostCenter_Stripe && newCC.BillingStrategy == CostCenter_Other {
175174
newCC.SpendingLimit = c.cfg.ForUsers
176-
newCC.BillingCycleStart = common_db.NewVarCharTime(now)
175+
newCC.BillingCycleStart = NewVarCharTime(now)
177176
// see you next month
178-
newCC.NextBillingTime = common_db.NewVarCharTime(now.AddDate(0, 1, 0))
177+
newCC.NextBillingTime = NewVarCharTime(now.AddDate(0, 1, 0))
179178
}
180179

181180
// Upgrading to Stripe
@@ -185,9 +184,9 @@ func (c *CostCenterManager) UpdateCostCenter(ctx context.Context, newCC CostCent
185184
return CostCenter{}, err
186185
}
187186

188-
newCC.BillingCycleStart = common_db.NewVarCharTime(now)
187+
newCC.BillingCycleStart = NewVarCharTime(now)
189188
// set an informative nextBillingTime, even though we don't manage Stripe billing cycle
190-
newCC.NextBillingTime = common_db.NewVarCharTime(now.AddDate(0, 1, 0))
189+
newCC.NextBillingTime = NewVarCharTime(now.AddDate(0, 1, 0))
191190
}
192191
} else if isTeam {
193192
// Billing strategy is Other, and it remains unchanged
@@ -201,9 +200,9 @@ func (c *CostCenterManager) UpdateCostCenter(ctx context.Context, newCC CostCent
201200
// Downgrading from stripe
202201
if existingCC.BillingStrategy == CostCenter_Stripe && newCC.BillingStrategy == CostCenter_Other {
203202
newCC.SpendingLimit = c.cfg.ForTeams
204-
newCC.BillingCycleStart = common_db.NewVarCharTime(now)
203+
newCC.BillingCycleStart = NewVarCharTime(now)
205204
// see you next month
206-
newCC.NextBillingTime = common_db.NewVarCharTime(now.AddDate(0, 1, 0))
205+
newCC.NextBillingTime = NewVarCharTime(now.AddDate(0, 1, 0))
207206
}
208207

209208
// Upgrading to Stripe
@@ -213,9 +212,9 @@ func (c *CostCenterManager) UpdateCostCenter(ctx context.Context, newCC CostCent
213212
return CostCenter{}, err
214213
}
215214

216-
newCC.BillingCycleStart = common_db.NewVarCharTime(now)
215+
newCC.BillingCycleStart = NewVarCharTime(now)
217216
// set an informative nextBillingTime, even though we don't manage Stripe billing cycle
218-
newCC.NextBillingTime = common_db.NewVarCharTime(now.AddDate(0, 1, 0))
217+
newCC.NextBillingTime = NewVarCharTime(now.AddDate(0, 1, 0))
219218
}
220219
} else {
221220
return CostCenter{}, status.Errorf(codes.InvalidArgument, "Unknown attribution entity %s", string(attributionID))
@@ -260,7 +259,7 @@ func (c *CostCenterManager) NewInvoiceUsageRecord(ctx context.Context, attributi
260259
AttributionID: attributionID,
261260
Description: "Credits",
262261
CreditCents: creditCents * -1,
263-
EffectiveTime: common_db.NewVarCharTime(now),
262+
EffectiveTime: NewVarCharTime(now),
264263
Kind: InvoiceUsageKind,
265264
Draft: false,
266265
}, nil
@@ -272,18 +271,16 @@ func (c *CostCenterManager) ListLatestCostCentersWithBillingTimeBefore(ctx conte
272271
var results []CostCenter
273272
var batch []CostCenter
274273

275-
subquery := db.
276-
Table((&CostCenter{}).TableName()).
274+
subquery := db.Table((&CostCenter{}).TableName()).
277275
// Retrieve the latest CostCenter for a given (attribution) ID.
278276
Select("DISTINCT id, MAX(creationTime) AS creationTime").
279277
Group("id")
280-
tx := db.
281-
Table(fmt.Sprintf("%s as cc", (&CostCenter{}).TableName())).
278+
tx := db.Table(fmt.Sprintf("%s as cc", (&CostCenter{}).TableName())).
282279
// Join on our set of latest CostCenter records
283280
Joins("INNER JOIN (?) AS expiredCC on cc.id = expiredCC.id AND cc.creationTime = expiredCC.creationTime", subquery).
284281
Where("cc.billingStrategy = ?", strategy).
285282
Where("nextBillingTime != ?", "").
286-
Where("nextBillingTime < ?", common_db.TimeToISO8601(billingTimeBefore)).
283+
Where("nextBillingTime < ?", TimeToISO8601(billingTimeBefore)).
287284
FindInBatches(&batch, 1000, func(tx *gorm.DB, iteration int) error {
288285
results = append(results, batch...)
289286
return nil
@@ -336,9 +333,9 @@ func (c *CostCenterManager) ResetUsage(ctx context.Context, cc CostCenter) (Cost
336333
ID: cc.ID,
337334
SpendingLimit: spendingLimit,
338335
BillingStrategy: cc.BillingStrategy,
339-
BillingCycleStart: common_db.NewVarCharTime(now),
340-
NextBillingTime: common_db.NewVarCharTime(nextBillingTime),
341-
CreationTime: common_db.NewVarCharTime(now),
336+
BillingCycleStart: NewVarCharTime(now),
337+
NextBillingTime: NewVarCharTime(nextBillingTime),
338+
CreationTime: NewVarCharTime(now),
342339
}
343340
err = c.conn.Save(&newCostCenter).Error
344341
if err != nil {

components/usage/pkg/db/cost_center_test.go renamed to components/gitpod-db/go/cost_center_test.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
"testing"
1010
"time"
1111

12-
common_db "github.com/gitpod-io/gitpod/common-go/db"
13-
"github.com/gitpod-io/gitpod/usage/pkg/db"
14-
"github.com/gitpod-io/gitpod/usage/pkg/db/dbtest"
12+
db "github.com/gitpod-io/gitpod/components/gitpod-db/go"
13+
14+
"github.com/gitpod-io/gitpod/components/gitpod-db/go/dbtest"
1515
"github.com/google/uuid"
1616
"github.com/stretchr/testify/require"
1717
"google.golang.org/grpc/codes"
@@ -73,28 +73,28 @@ func TestCostCenterManager_GetOrCreateCostCenter_ResetsExpired(t *testing.T) {
7373

7474
expiredCC := db.CostCenter{
7575
ID: db.NewTeamAttributionID(uuid.New().String()),
76-
CreationTime: common_db.NewVarCharTime(now),
76+
CreationTime: db.NewVarCharTime(now),
7777
SpendingLimit: 0,
7878
BillingStrategy: db.CostCenter_Other,
79-
NextBillingTime: common_db.NewVarCharTime(expired),
80-
BillingCycleStart: common_db.NewVarCharTime(now),
79+
NextBillingTime: db.NewVarCharTime(expired),
80+
BillingCycleStart: db.NewVarCharTime(now),
8181
}
8282
unexpiredCC := db.CostCenter{
8383
ID: db.NewUserAttributionID(uuid.New().String()),
84-
CreationTime: common_db.NewVarCharTime(now),
84+
CreationTime: db.NewVarCharTime(now),
8585
SpendingLimit: 500,
8686
BillingStrategy: db.CostCenter_Other,
87-
NextBillingTime: common_db.NewVarCharTime(unexpired),
88-
BillingCycleStart: common_db.NewVarCharTime(now),
87+
NextBillingTime: db.NewVarCharTime(unexpired),
88+
BillingCycleStart: db.NewVarCharTime(now),
8989
}
9090
// Stripe billing strategy should not be reset
9191
stripeCC := db.CostCenter{
9292
ID: db.NewUserAttributionID(uuid.New().String()),
93-
CreationTime: common_db.NewVarCharTime(now),
93+
CreationTime: db.NewVarCharTime(now),
9494
SpendingLimit: 0,
9595
BillingStrategy: db.CostCenter_Stripe,
96-
NextBillingTime: common_db.VarcharTime{},
97-
BillingCycleStart: common_db.NewVarCharTime(now),
96+
NextBillingTime: db.VarcharTime{},
97+
BillingCycleStart: db.NewVarCharTime(now),
9898
}
9999

100100
dbtest.CreateCostCenters(t, conn,
@@ -103,21 +103,21 @@ func TestCostCenterManager_GetOrCreateCostCenter_ResetsExpired(t *testing.T) {
103103
dbtest.NewCostCenter(t, stripeCC),
104104
)
105105

106-
// expired CostCenter should be reset, so we get a new CreationTime
106+
// expired db.CostCenter should be reset, so we get a new CreationTime
107107
retrievedExpiredCC, err := mnr.GetOrCreateCostCenter(context.Background(), expiredCC.ID)
108108
require.NoError(t, err)
109109
t.Cleanup(func() {
110110
conn.Model(&db.CostCenter{}).Delete(retrievedExpiredCC.ID)
111111
})
112-
require.Equal(t, common_db.NewVarCharTime(expired).Time().AddDate(0, 1, 0), retrievedExpiredCC.NextBillingTime.Time())
112+
require.Equal(t, db.NewVarCharTime(expired).Time().AddDate(0, 1, 0), retrievedExpiredCC.NextBillingTime.Time())
113113
require.Equal(t, expiredCC.ID, retrievedExpiredCC.ID)
114114
require.Equal(t, expiredCC.BillingStrategy, retrievedExpiredCC.BillingStrategy)
115115
require.WithinDuration(t, now, expiredCC.CreationTime.Time(), 3*time.Second, "new cost center creation time must be within 3 seconds of now")
116116

117117
// unexpired cost center must not be reset
118118
retrievedUnexpiredCC, err := mnr.GetOrCreateCostCenter(context.Background(), unexpiredCC.ID)
119119
require.NoError(t, err)
120-
require.Equal(t, common_db.NewVarCharTime(unexpired).Time(), retrievedUnexpiredCC.NextBillingTime.Time())
120+
require.Equal(t, db.NewVarCharTime(unexpired).Time(), retrievedUnexpiredCC.NextBillingTime.Time())
121121
require.Equal(t, unexpiredCC.ID, retrievedUnexpiredCC.ID)
122122
require.Equal(t, unexpiredCC.BillingStrategy, retrievedUnexpiredCC.BillingStrategy)
123123
require.WithinDuration(t, unexpiredCC.CreationTime.Time(), retrievedUnexpiredCC.CreationTime.Time(), 100*time.Millisecond)
@@ -349,16 +349,16 @@ func TestCostCenter_ListLatestCostCentersWithBillingTimeBefore(t *testing.T) {
349349
dbtest.NewCostCenter(t, db.CostCenter{
350350
ID: db.NewTeamAttributionID(attributionID),
351351
SpendingLimit: 100,
352-
CreationTime: common_db.NewVarCharTime(firstCreation),
352+
CreationTime: db.NewVarCharTime(firstCreation),
353353
BillingStrategy: db.CostCenter_Other,
354-
NextBillingTime: common_db.NewVarCharTime(firstCreation),
354+
NextBillingTime: db.NewVarCharTime(firstCreation),
355355
}),
356356
dbtest.NewCostCenter(t, db.CostCenter{
357357
ID: db.NewTeamAttributionID(attributionID),
358358
SpendingLimit: 100,
359-
CreationTime: common_db.NewVarCharTime(secondCreation),
359+
CreationTime: db.NewVarCharTime(secondCreation),
360360
BillingStrategy: db.CostCenter_Other,
361-
NextBillingTime: common_db.NewVarCharTime(secondCreation),
361+
NextBillingTime: db.NewVarCharTime(secondCreation),
362362
}),
363363
}
364364

@@ -386,14 +386,14 @@ func TestCostCenter_ListLatestCostCentersWithBillingTimeBefore(t *testing.T) {
386386
dbtest.NewCostCenter(t, db.CostCenter{
387387
ID: db.NewTeamAttributionID(attributionID),
388388
SpendingLimit: 100,
389-
CreationTime: common_db.NewVarCharTime(firstCreation),
389+
CreationTime: db.NewVarCharTime(firstCreation),
390390
BillingStrategy: db.CostCenter_Other,
391-
NextBillingTime: common_db.NewVarCharTime(firstCreation),
391+
NextBillingTime: db.NewVarCharTime(firstCreation),
392392
}),
393393
dbtest.NewCostCenter(t, db.CostCenter{
394394
ID: db.NewTeamAttributionID(attributionID),
395395
SpendingLimit: 100,
396-
CreationTime: common_db.NewVarCharTime(secondCreation),
396+
CreationTime: db.NewVarCharTime(secondCreation),
397397
BillingStrategy: db.CostCenter_Stripe,
398398
}),
399399
}
@@ -418,7 +418,7 @@ func TestCostCenterManager_ResetUsage(t *testing.T) {
418418
})
419419
_, err := mnr.ResetUsage(context.Background(), db.CostCenter{
420420
ID: db.NewUserAttributionID(uuid.New().String()),
421-
CreationTime: common_db.NewVarCharTime(time.Now()),
421+
CreationTime: db.NewVarCharTime(time.Now()),
422422
SpendingLimit: 500,
423423
BillingStrategy: db.CostCenter_Stripe,
424424
})
@@ -433,10 +433,10 @@ func TestCostCenterManager_ResetUsage(t *testing.T) {
433433
})
434434
oldCC := db.CostCenter{
435435
ID: db.NewTeamAttributionID(uuid.New().String()),
436-
CreationTime: common_db.NewVarCharTime(time.Now()),
436+
CreationTime: db.NewVarCharTime(time.Now()),
437437
SpendingLimit: 0,
438438
BillingStrategy: db.CostCenter_Other,
439-
NextBillingTime: common_db.NewVarCharTime(ts),
439+
NextBillingTime: db.NewVarCharTime(ts),
440440
}
441441
newCC, err := mnr.ResetUsage(context.Background(), oldCC)
442442
require.NoError(t, err)

0 commit comments

Comments
 (0)