Skip to content

Commit 65abd99

Browse files
committed
[common-go] Move db models to common-go
1 parent 3a9e9bf commit 65abd99

Some content is hidden

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

51 files changed

+436
-316
lines changed

components/common-go/BUILD.yaml

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,13 @@ packages:
77
type: go
88
srcs:
99
- "**"
10+
deps:
11+
- :init-testdb
1012
config:
11-
packaging: library
13+
packaging: library
14+
15+
- name: init-testdb
16+
type: generic
17+
deps:
18+
- components/gitpod-db:dbtest-init
19+
ephemeral: true

components/common-go/db/conn.go

+1-1
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/common-go/db/cost_center.go

+24-25
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
// we don't manage stripe billing cycle
190-
newCC.NextBillingTime = common_db.VarcharTime{}
189+
newCC.NextBillingTime = VarcharTime{}
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
// we don't manage stripe billing cycle
218-
newCC.NextBillingTime = common_db.VarcharTime{}
217+
newCC.NextBillingTime = VarcharTime{}
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
@@ -283,7 +282,7 @@ func (c *CostCenterManager) ListLatestCostCentersWithBillingTimeBefore(ctx conte
283282
Joins("INNER JOIN (?) AS expiredCC on cc.id = expiredCC.id AND cc.creationTime = expiredCC.creationTime", subquery).
284283
Where("cc.billingStrategy = ?", strategy).
285284
Where("nextBillingTime != ?", "").
286-
Where("nextBillingTime < ?", common_db.TimeToISO8601(billingTimeBefore)).
285+
Where("nextBillingTime < ?", TimeToISO8601(billingTimeBefore)).
287286
FindInBatches(&batch, 1000, func(tx *gorm.DB, iteration int) error {
288287
results = append(results, batch...)
289288
return nil
@@ -339,9 +338,9 @@ func (c *CostCenterManager) ResetUsage(ctx context.Context, cc CostCenter) (Cost
339338
ID: cc.ID,
340339
SpendingLimit: spendingLimit,
341340
BillingStrategy: cc.BillingStrategy,
342-
BillingCycleStart: common_db.NewVarCharTime(billingCycleStart),
343-
NextBillingTime: common_db.NewVarCharTime(nextBillingTime),
344-
CreationTime: common_db.NewVarCharTime(now),
341+
BillingCycleStart: NewVarCharTime(billingCycleStart),
342+
NextBillingTime: NewVarCharTime(nextBillingTime),
343+
CreationTime: NewVarCharTime(now),
345344
}
346345
err = c.conn.Save(&newCostCenter).Error
347346
if err != nil {

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

+24-25
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ 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+
"github.com/gitpod-io/gitpod/common-go/db"
13+
"github.com/gitpod-io/gitpod/common-go/db/dbtest"
1514
"github.com/google/uuid"
1615
"github.com/stretchr/testify/require"
1716
"google.golang.org/grpc/codes"
@@ -73,28 +72,28 @@ func TestCostCenterManager_GetOrCreateCostCenter_ResetsExpired(t *testing.T) {
7372

7473
expiredCC := db.CostCenter{
7574
ID: db.NewTeamAttributionID(uuid.New().String()),
76-
CreationTime: common_db.NewVarCharTime(now),
75+
CreationTime: db.NewVarCharTime(now),
7776
SpendingLimit: 0,
7877
BillingStrategy: db.CostCenter_Other,
79-
NextBillingTime: common_db.NewVarCharTime(expired),
80-
BillingCycleStart: common_db.NewVarCharTime(now),
78+
NextBillingTime: db.NewVarCharTime(expired),
79+
BillingCycleStart: db.NewVarCharTime(now),
8180
}
8281
unexpiredCC := db.CostCenter{
8382
ID: db.NewUserAttributionID(uuid.New().String()),
84-
CreationTime: common_db.NewVarCharTime(now),
83+
CreationTime: db.NewVarCharTime(now),
8584
SpendingLimit: 500,
8685
BillingStrategy: db.CostCenter_Other,
87-
NextBillingTime: common_db.NewVarCharTime(unexpired),
88-
BillingCycleStart: common_db.NewVarCharTime(now),
86+
NextBillingTime: db.NewVarCharTime(unexpired),
87+
BillingCycleStart: db.NewVarCharTime(now),
8988
}
9089
// Stripe billing strategy should not be reset
9190
stripeCC := db.CostCenter{
9291
ID: db.NewUserAttributionID(uuid.New().String()),
93-
CreationTime: common_db.NewVarCharTime(now),
92+
CreationTime: db.NewVarCharTime(now),
9493
SpendingLimit: 0,
9594
BillingStrategy: db.CostCenter_Stripe,
96-
NextBillingTime: common_db.VarcharTime{},
97-
BillingCycleStart: common_db.NewVarCharTime(now),
95+
NextBillingTime: db.VarcharTime{},
96+
BillingCycleStart: db.NewVarCharTime(now),
9897
}
9998

10099
dbtest.CreateCostCenters(t, conn,
@@ -109,15 +108,15 @@ func TestCostCenterManager_GetOrCreateCostCenter_ResetsExpired(t *testing.T) {
109108
t.Cleanup(func() {
110109
conn.Model(&db.CostCenter{}).Delete(retrievedExpiredCC.ID)
111110
})
112-
require.Equal(t, common_db.NewVarCharTime(expired).Time().AddDate(0, 1, 0), retrievedExpiredCC.NextBillingTime.Time())
111+
require.Equal(t, db.NewVarCharTime(expired).Time().AddDate(0, 1, 0), retrievedExpiredCC.NextBillingTime.Time())
113112
require.Equal(t, expiredCC.ID, retrievedExpiredCC.ID)
114113
require.Equal(t, expiredCC.BillingStrategy, retrievedExpiredCC.BillingStrategy)
115114
require.WithinDuration(t, now, expiredCC.CreationTime.Time(), 3*time.Second, "new cost center creation time must be within 3 seconds of now")
116115

117116
// unexpired cost center must not be reset
118117
retrievedUnexpiredCC, err := mnr.GetOrCreateCostCenter(context.Background(), unexpiredCC.ID)
119118
require.NoError(t, err)
120-
require.Equal(t, common_db.NewVarCharTime(unexpired).Time(), retrievedUnexpiredCC.NextBillingTime.Time())
119+
require.Equal(t, db.NewVarCharTime(unexpired).Time(), retrievedUnexpiredCC.NextBillingTime.Time())
121120
require.Equal(t, unexpiredCC.ID, retrievedUnexpiredCC.ID)
122121
require.Equal(t, unexpiredCC.BillingStrategy, retrievedUnexpiredCC.BillingStrategy)
123122
require.WithinDuration(t, unexpiredCC.CreationTime.Time(), retrievedUnexpiredCC.CreationTime.Time(), 100*time.Millisecond)
@@ -289,7 +288,7 @@ func TestSaveCostCenterMovedToStripe(t *testing.T) {
289288
teamCC, err = mnr.UpdateCostCenter(context.Background(), teamCC)
290289
require.NoError(t, err)
291290
require.Equal(t, db.CostCenter_Stripe, teamCC.BillingStrategy)
292-
require.Equal(t, common_db.VarcharTime{}, teamCC.NextBillingTime)
291+
require.Equal(t, db.VarcharTime{}, teamCC.NextBillingTime)
293292
require.Equal(t, int32(400050), teamCC.SpendingLimit)
294293

295294
teamCC.BillingStrategy = db.CostCenter_Other
@@ -349,16 +348,16 @@ func TestCostCenter_ListLatestCostCentersWithBillingTimeBefore(t *testing.T) {
349348
dbtest.NewCostCenter(t, db.CostCenter{
350349
ID: db.NewTeamAttributionID(attributionID),
351350
SpendingLimit: 100,
352-
CreationTime: common_db.NewVarCharTime(firstCreation),
351+
CreationTime: db.NewVarCharTime(firstCreation),
353352
BillingStrategy: db.CostCenter_Other,
354-
NextBillingTime: common_db.NewVarCharTime(firstCreation),
353+
NextBillingTime: db.NewVarCharTime(firstCreation),
355354
}),
356355
dbtest.NewCostCenter(t, db.CostCenter{
357356
ID: db.NewTeamAttributionID(attributionID),
358357
SpendingLimit: 100,
359-
CreationTime: common_db.NewVarCharTime(secondCreation),
358+
CreationTime: db.NewVarCharTime(secondCreation),
360359
BillingStrategy: db.CostCenter_Other,
361-
NextBillingTime: common_db.NewVarCharTime(secondCreation),
360+
NextBillingTime: db.NewVarCharTime(secondCreation),
362361
}),
363362
}
364363

@@ -386,14 +385,14 @@ func TestCostCenter_ListLatestCostCentersWithBillingTimeBefore(t *testing.T) {
386385
dbtest.NewCostCenter(t, db.CostCenter{
387386
ID: db.NewTeamAttributionID(attributionID),
388387
SpendingLimit: 100,
389-
CreationTime: common_db.NewVarCharTime(firstCreation),
388+
CreationTime: db.NewVarCharTime(firstCreation),
390389
BillingStrategy: db.CostCenter_Other,
391-
NextBillingTime: common_db.NewVarCharTime(firstCreation),
390+
NextBillingTime: db.NewVarCharTime(firstCreation),
392391
}),
393392
dbtest.NewCostCenter(t, db.CostCenter{
394393
ID: db.NewTeamAttributionID(attributionID),
395394
SpendingLimit: 100,
396-
CreationTime: common_db.NewVarCharTime(secondCreation),
395+
CreationTime: db.NewVarCharTime(secondCreation),
397396
BillingStrategy: db.CostCenter_Stripe,
398397
}),
399398
}
@@ -418,7 +417,7 @@ func TestCostCenterManager_ResetUsage(t *testing.T) {
418417
})
419418
_, err := mnr.ResetUsage(context.Background(), db.CostCenter{
420419
ID: db.NewUserAttributionID(uuid.New().String()),
421-
CreationTime: common_db.NewVarCharTime(time.Now()),
420+
CreationTime: db.NewVarCharTime(time.Now()),
422421
SpendingLimit: 500,
423422
BillingStrategy: db.CostCenter_Stripe,
424423
})
@@ -433,10 +432,10 @@ func TestCostCenterManager_ResetUsage(t *testing.T) {
433432
})
434433
oldCC := db.CostCenter{
435434
ID: db.NewTeamAttributionID(uuid.New().String()),
436-
CreationTime: common_db.NewVarCharTime(time.Now()),
435+
CreationTime: db.NewVarCharTime(time.Now()),
437436
SpendingLimit: 0,
438437
BillingStrategy: db.CostCenter_Other,
439-
NextBillingTime: common_db.NewVarCharTime(ts),
438+
NextBillingTime: db.NewVarCharTime(ts),
440439
}
441440
newCC, err := mnr.ResetUsage(context.Background(), oldCC)
442441
require.NoError(t, err)

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"sync"
99
"testing"
1010

11-
common_db "github.com/gitpod-io/gitpod/common-go/db"
11+
"github.com/gitpod-io/gitpod/common-go/db"
1212
"github.com/stretchr/testify/require"
1313
"gorm.io/gorm"
1414
)
@@ -31,13 +31,13 @@ func ConnectForTests(t *testing.T) *gorm.DB {
3131
// These are static connection details for tests, started by `leeway components/usage:init-testdb`.
3232
// We use the same static credentials for CI & local instance of MySQL Server.
3333
var err error
34-
conn, err = common_db.Connect(common_db.ConnectionParams{
34+
conn, err = db.Connect(db.ConnectionParams{
3535
User: "root",
3636
Password: "test",
3737
Host: "localhost:23306",
3838
Database: "gitpod",
3939
})
40-
require.NoError(t, err, "Failed to establish connection to DB. In a workspace, run `leeway build components/usage:init-testdb` once to bootstrap the DB.")
40+
require.NoError(t, err, "Failed to establish connection to DB. In a workspace, run `leeway build components/common-go:init-testdb` once to bootstrap the db.")
4141

4242
return conn
4343
}

components/usage/pkg/db/dbtest/cost_center.go renamed to components/common-go/db/dbtest/cost_center.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import (
88
"testing"
99
"time"
1010

11-
common_db "github.com/gitpod-io/gitpod/common-go/db"
12-
"github.com/gitpod-io/gitpod/usage/pkg/db"
11+
"github.com/gitpod-io/gitpod/common-go/db"
1312
"github.com/google/uuid"
1413
"github.com/stretchr/testify/require"
1514
"gorm.io/gorm"
@@ -20,11 +19,11 @@ func NewCostCenter(t *testing.T, record db.CostCenter) db.CostCenter {
2019

2120
result := db.CostCenter{
2221
ID: db.NewUserAttributionID(uuid.New().String()),
23-
CreationTime: common_db.NewVarCharTime(time.Now()),
22+
CreationTime: db.NewVarCharTime(time.Now()),
2423
SpendingLimit: 100,
2524
BillingStrategy: db.CostCenter_Stripe,
26-
BillingCycleStart: common_db.NewVarCharTime(time.Now()),
27-
NextBillingTime: common_db.NewVarCharTime(time.Now().Add(10 * time.Hour)),
25+
BillingCycleStart: db.NewVarCharTime(time.Now()),
26+
NextBillingTime: db.NewVarCharTime(time.Now().Add(10 * time.Hour)),
2827
}
2928

3029
if record.ID != "" {

components/usage/pkg/db/dbtest/stripe_customer.go renamed to components/common-go/db/dbtest/stripe_customer.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ 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"
12+
"github.com/gitpod-io/gitpod/common-go/db"
1413
"github.com/google/uuid"
1514
"github.com/stretchr/testify/require"
1615
"gorm.io/gorm"
@@ -22,7 +21,7 @@ func NewStripeCustomer(t *testing.T, customer db.StripeCustomer) db.StripeCustom
2221
result := db.StripeCustomer{
2322
StripeCustomerID: fmt.Sprintf("cus_%s", uuid.New().String()),
2423
AttributionID: db.NewUserAttributionID(uuid.New().String()),
25-
CreationTime: common_db.NewVarCharTime(time.Now()),
24+
CreationTime: db.NewVarCharTime(time.Now()),
2625
}
2726

2827
if customer.StripeCustomerID != "" {

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import (
88
"context"
99
"testing"
1010

11-
common_db "github.com/gitpod-io/gitpod/common-go/db"
12-
"github.com/gitpod-io/gitpod/usage/pkg/db"
11+
"github.com/gitpod-io/gitpod/common-go/db"
1312
"github.com/google/uuid"
1413
"github.com/stretchr/testify/require"
1514
"gorm.io/gorm"
@@ -25,7 +24,7 @@ func NewUsage(t *testing.T, record db.Usage) db.Usage {
2524
AttributionID: db.NewUserAttributionID(uuid.New().String()),
2625
Description: "some description",
2726
CreditCents: 42,
28-
EffectiveTime: common_db.VarcharTime{},
27+
EffectiveTime: db.VarcharTime{},
2928
Kind: db.WorkspaceInstanceUsageKind,
3029
WorkspaceInstanceID: &workspaceInstanceId,
3130
}

0 commit comments

Comments
 (0)