Skip to content

Commit 1c32689

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

40 files changed

+697
-579
lines changed

components/common-go/BUILD.yaml

Lines changed: 9 additions & 1 deletion
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/usage/pkg/db/cost_center.go renamed to components/common-go/db/cost_center.go

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
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 db
5+
package common_db
66

77
import (
88
"context"
99
"errors"
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 {

0 commit comments

Comments
 (0)