Skip to content

Commit 7a44b8b

Browse files
author
Laurie T. Malau
committed
[public-api] personal access token db model
1 parent 6324566 commit 7a44b8b

Some content is hidden

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

56 files changed

+1004
-383
lines changed

components/common-go/go.mod

-9
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

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

components/gitpod-db/go/BUILD.yaml

+12-1
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

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

+22-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 = defaultSpendingLimit
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

193192
log.WithField("cost_center", newCC).Info("saving cost center.")
@@ -229,7 +228,7 @@ func (c *CostCenterManager) NewInvoiceUsageRecord(ctx context.Context, attributi
229228
AttributionID: attributionID,
230229
Description: "Credits",
231230
CreditCents: creditCents * -1,
232-
EffectiveTime: common_db.NewVarCharTime(now),
231+
EffectiveTime: NewVarCharTime(now),
233232
Kind: InvoiceUsageKind,
234233
Draft: false,
235234
}, nil
@@ -241,18 +240,16 @@ func (c *CostCenterManager) ListLatestCostCentersWithBillingTimeBefore(ctx conte
241240
var results []CostCenter
242241
var batch []CostCenter
243242

244-
subquery := db.
245-
Table((&CostCenter{}).TableName()).
243+
subquery := db.Table((&CostCenter{}).TableName()).
246244
// Retrieve the latest CostCenter for a given (attribution) ID.
247245
Select("DISTINCT id, MAX(creationTime) AS creationTime").
248246
Group("id")
249-
tx := db.
250-
Table(fmt.Sprintf("%s as cc", (&CostCenter{}).TableName())).
247+
tx := db.Table(fmt.Sprintf("%s as cc", (&CostCenter{}).TableName())).
251248
// Join on our set of latest CostCenter records
252249
Joins("INNER JOIN (?) AS expiredCC on cc.id = expiredCC.id AND cc.creationTime = expiredCC.creationTime", subquery).
253250
Where("cc.billingStrategy = ?", strategy).
254251
Where("nextBillingTime != ?", "").
255-
Where("nextBillingTime < ?", common_db.TimeToISO8601(billingTimeBefore)).
252+
Where("nextBillingTime < ?", TimeToISO8601(billingTimeBefore)).
256253
FindInBatches(&batch, 1000, func(tx *gorm.DB, iteration int) error {
257254
results = append(results, batch...)
258255
return nil
@@ -305,9 +302,9 @@ func (c *CostCenterManager) ResetUsage(ctx context.Context, cc CostCenter) (Cost
305302
ID: cc.ID,
306303
SpendingLimit: spendingLimit,
307304
BillingStrategy: cc.BillingStrategy,
308-
BillingCycleStart: common_db.NewVarCharTime(now),
309-
NextBillingTime: common_db.NewVarCharTime(nextBillingTime),
310-
CreationTime: common_db.NewVarCharTime(now),
305+
BillingCycleStart: NewVarCharTime(now),
306+
NextBillingTime: NewVarCharTime(nextBillingTime),
307+
CreationTime: NewVarCharTime(now),
311308
}
312309
err = c.conn.Save(&newCostCenter).Error
313310
if err != nil {

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

+25-25
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)
@@ -341,16 +341,16 @@ func TestCostCenter_ListLatestCostCentersWithBillingTimeBefore(t *testing.T) {
341341
dbtest.NewCostCenter(t, db.CostCenter{
342342
ID: db.NewTeamAttributionID(attributionID),
343343
SpendingLimit: 100,
344-
CreationTime: common_db.NewVarCharTime(firstCreation),
344+
CreationTime: db.NewVarCharTime(firstCreation),
345345
BillingStrategy: db.CostCenter_Other,
346-
NextBillingTime: common_db.NewVarCharTime(firstCreation),
346+
NextBillingTime: db.NewVarCharTime(firstCreation),
347347
}),
348348
dbtest.NewCostCenter(t, db.CostCenter{
349349
ID: db.NewTeamAttributionID(attributionID),
350350
SpendingLimit: 100,
351-
CreationTime: common_db.NewVarCharTime(secondCreation),
351+
CreationTime: db.NewVarCharTime(secondCreation),
352352
BillingStrategy: db.CostCenter_Other,
353-
NextBillingTime: common_db.NewVarCharTime(secondCreation),
353+
NextBillingTime: db.NewVarCharTime(secondCreation),
354354
}),
355355
}
356356

@@ -378,14 +378,14 @@ func TestCostCenter_ListLatestCostCentersWithBillingTimeBefore(t *testing.T) {
378378
dbtest.NewCostCenter(t, db.CostCenter{
379379
ID: db.NewTeamAttributionID(attributionID),
380380
SpendingLimit: 100,
381-
CreationTime: common_db.NewVarCharTime(firstCreation),
381+
CreationTime: db.NewVarCharTime(firstCreation),
382382
BillingStrategy: db.CostCenter_Other,
383-
NextBillingTime: common_db.NewVarCharTime(firstCreation),
383+
NextBillingTime: db.NewVarCharTime(firstCreation),
384384
}),
385385
dbtest.NewCostCenter(t, db.CostCenter{
386386
ID: db.NewTeamAttributionID(attributionID),
387387
SpendingLimit: 100,
388-
CreationTime: common_db.NewVarCharTime(secondCreation),
388+
CreationTime: db.NewVarCharTime(secondCreation),
389389
BillingStrategy: db.CostCenter_Stripe,
390390
}),
391391
}
@@ -410,7 +410,7 @@ func TestCostCenterManager_ResetUsage(t *testing.T) {
410410
})
411411
_, err := mnr.ResetUsage(context.Background(), db.CostCenter{
412412
ID: db.NewUserAttributionID(uuid.New().String()),
413-
CreationTime: common_db.NewVarCharTime(time.Now()),
413+
CreationTime: db.NewVarCharTime(time.Now()),
414414
SpendingLimit: 500,
415415
BillingStrategy: db.CostCenter_Stripe,
416416
})
@@ -425,10 +425,10 @@ func TestCostCenterManager_ResetUsage(t *testing.T) {
425425
})
426426
oldCC := db.CostCenter{
427427
ID: db.NewTeamAttributionID(uuid.New().String()),
428-
CreationTime: common_db.NewVarCharTime(time.Now()),
428+
CreationTime: db.NewVarCharTime(time.Now()),
429429
SpendingLimit: 0,
430430
BillingStrategy: db.CostCenter_Other,
431-
NextBillingTime: common_db.NewVarCharTime(ts),
431+
NextBillingTime: db.NewVarCharTime(ts),
432432
}
433433
newCC, err := mnr.ResetUsage(context.Background(), oldCC)
434434
require.NoError(t, err)

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

+4-4
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+
db "github.com/gitpod-io/gitpod/components/gitpod-db/go"
1212
"github.com/stretchr/testify/require"
1313
"gorm.io/gorm"
1414
)
@@ -28,16 +28,16 @@ func ConnectForTests(t *testing.T) *gorm.DB {
2828
return conn
2929
}
3030

31-
// These are static connection details for tests, started by `leeway components/usage:init-testdb`.
31+
// These are static connection details for tests, started by `leeway build components/gitpod-db/go: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 In a workspace, run `leeway build components/gitpod-db/go:init-testdb` once to bootstrap the ")
4141

4242
return conn
4343
}

0 commit comments

Comments
 (0)