2
2
// Licensed under the GNU Affero General Public License (AGPL).
3
3
// See License-AGPL.txt in the project root for license information.
4
4
5
- package db
5
+ package common_db
6
6
7
7
import (
8
8
"context"
9
9
"errors"
10
10
"fmt"
11
11
"time"
12
12
13
- common_db "github.com/gitpod-io/gitpod/common-go/db"
14
13
"github.com/gitpod-io/gitpod/common-go/log"
15
14
"github.com/google/uuid"
16
15
"google.golang.org/grpc/codes"
@@ -28,13 +27,13 @@ const (
28
27
)
29
28
30
29
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"`
38
37
}
39
38
40
39
// TableName sets the insert table name for this struct type
@@ -84,11 +83,11 @@ func (c *CostCenterManager) GetOrCreateCostCenter(ctx context.Context, attributi
84
83
}
85
84
result = CostCenter {
86
85
ID : attributionID ,
87
- CreationTime : common_db . NewVarCharTime (now ),
86
+ CreationTime : NewVarCharTime (now ),
88
87
BillingStrategy : CostCenter_Other ,
89
88
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 )),
92
91
}
93
92
err := c .conn .Save (& result ).Error
94
93
if err != nil {
@@ -147,7 +146,7 @@ func (c *CostCenterManager) UpdateCostCenter(ctx context.Context, newCC CostCent
147
146
now := time .Now ()
148
147
149
148
// we always update the creationTime
150
- newCC .CreationTime = common_db . NewVarCharTime (now )
149
+ newCC .CreationTime = NewVarCharTime (now )
151
150
// we don't allow setting billingCycleStart or nextBillingTime from outside
152
151
newCC .BillingCycleStart = existingCC .BillingCycleStart
153
152
newCC .NextBillingTime = existingCC .NextBillingTime
@@ -173,9 +172,9 @@ func (c *CostCenterManager) UpdateCostCenter(ctx context.Context, newCC CostCent
173
172
// Downgrading from stripe
174
173
if existingCC .BillingStrategy == CostCenter_Stripe && newCC .BillingStrategy == CostCenter_Other {
175
174
newCC .SpendingLimit = c .cfg .ForUsers
176
- newCC .BillingCycleStart = common_db . NewVarCharTime (now )
175
+ newCC .BillingCycleStart = NewVarCharTime (now )
177
176
// see you next month
178
- newCC .NextBillingTime = common_db . NewVarCharTime (now .AddDate (0 , 1 , 0 ))
177
+ newCC .NextBillingTime = NewVarCharTime (now .AddDate (0 , 1 , 0 ))
179
178
}
180
179
181
180
// Upgrading to Stripe
@@ -185,9 +184,9 @@ func (c *CostCenterManager) UpdateCostCenter(ctx context.Context, newCC CostCent
185
184
return CostCenter {}, err
186
185
}
187
186
188
- newCC .BillingCycleStart = common_db . NewVarCharTime (now )
187
+ newCC .BillingCycleStart = NewVarCharTime (now )
189
188
// we don't manage stripe billing cycle
190
- newCC .NextBillingTime = common_db. VarcharTime {}
189
+ newCC .NextBillingTime = VarcharTime {}
191
190
}
192
191
} else if isTeam {
193
192
// Billing strategy is Other, and it remains unchanged
@@ -201,9 +200,9 @@ func (c *CostCenterManager) UpdateCostCenter(ctx context.Context, newCC CostCent
201
200
// Downgrading from stripe
202
201
if existingCC .BillingStrategy == CostCenter_Stripe && newCC .BillingStrategy == CostCenter_Other {
203
202
newCC .SpendingLimit = c .cfg .ForTeams
204
- newCC .BillingCycleStart = common_db . NewVarCharTime (now )
203
+ newCC .BillingCycleStart = NewVarCharTime (now )
205
204
// see you next month
206
- newCC .NextBillingTime = common_db . NewVarCharTime (now .AddDate (0 , 1 , 0 ))
205
+ newCC .NextBillingTime = NewVarCharTime (now .AddDate (0 , 1 , 0 ))
207
206
}
208
207
209
208
// Upgrading to Stripe
@@ -213,9 +212,9 @@ func (c *CostCenterManager) UpdateCostCenter(ctx context.Context, newCC CostCent
213
212
return CostCenter {}, err
214
213
}
215
214
216
- newCC .BillingCycleStart = common_db . NewVarCharTime (now )
215
+ newCC .BillingCycleStart = NewVarCharTime (now )
217
216
// we don't manage stripe billing cycle
218
- newCC .NextBillingTime = common_db. VarcharTime {}
217
+ newCC .NextBillingTime = VarcharTime {}
219
218
}
220
219
} else {
221
220
return CostCenter {}, status .Errorf (codes .InvalidArgument , "Unknown attribution entity %s" , string (attributionID ))
@@ -260,7 +259,7 @@ func (c *CostCenterManager) NewInvoiceUsageRecord(ctx context.Context, attributi
260
259
AttributionID : attributionID ,
261
260
Description : "Credits" ,
262
261
CreditCents : creditCents * - 1 ,
263
- EffectiveTime : common_db . NewVarCharTime (now ),
262
+ EffectiveTime : NewVarCharTime (now ),
264
263
Kind : InvoiceUsageKind ,
265
264
Draft : false ,
266
265
}, nil
@@ -283,7 +282,7 @@ func (c *CostCenterManager) ListLatestCostCentersWithBillingTimeBefore(ctx conte
283
282
Joins ("INNER JOIN (?) AS expiredCC on cc.id = expiredCC.id AND cc.creationTime = expiredCC.creationTime" , subquery ).
284
283
Where ("cc.billingStrategy = ?" , strategy ).
285
284
Where ("nextBillingTime != ?" , "" ).
286
- Where ("nextBillingTime < ?" , common_db . TimeToISO8601 (billingTimeBefore )).
285
+ Where ("nextBillingTime < ?" , TimeToISO8601 (billingTimeBefore )).
287
286
FindInBatches (& batch , 1000 , func (tx * gorm.DB , iteration int ) error {
288
287
results = append (results , batch ... )
289
288
return nil
@@ -339,9 +338,9 @@ func (c *CostCenterManager) ResetUsage(ctx context.Context, cc CostCenter) (Cost
339
338
ID : cc .ID ,
340
339
SpendingLimit : spendingLimit ,
341
340
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 ),
345
344
}
346
345
err = c .conn .Save (& newCostCenter ).Error
347
346
if err != nil {
0 commit comments