Skip to content

[common-go] Move db models to common-go #14731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion components/common-go/BUILD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,13 @@ packages:
type: go
srcs:
- "**"
deps:
- :init-testdb
config:
packaging: library
packaging: library

- name: init-testdb
type: generic
deps:
- components/gitpod-db:dbtest-init
ephemeral: true
2 changes: 1 addition & 1 deletion components/common-go/db/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package common_db
package db

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"time"

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

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

// TableName sets the insert table name for this struct type
Expand Down Expand Up @@ -84,11 +83,11 @@ func (c *CostCenterManager) GetOrCreateCostCenter(ctx context.Context, attributi
}
result = CostCenter{
ID: attributionID,
CreationTime: common_db.NewVarCharTime(now),
CreationTime: NewVarCharTime(now),
BillingStrategy: CostCenter_Other,
SpendingLimit: defaultSpendingLimit,
BillingCycleStart: common_db.NewVarCharTime(now),
NextBillingTime: common_db.NewVarCharTime(now.AddDate(0, 1, 0)),
BillingCycleStart: NewVarCharTime(now),
NextBillingTime: NewVarCharTime(now.AddDate(0, 1, 0)),
}
err := c.conn.Save(&result).Error
if err != nil {
Expand Down Expand Up @@ -147,7 +146,7 @@ func (c *CostCenterManager) UpdateCostCenter(ctx context.Context, newCC CostCent
now := time.Now()

// we always update the creationTime
newCC.CreationTime = common_db.NewVarCharTime(now)
newCC.CreationTime = NewVarCharTime(now)
// we don't allow setting billingCycleStart or nextBillingTime from outside
newCC.BillingCycleStart = existingCC.BillingCycleStart
newCC.NextBillingTime = existingCC.NextBillingTime
Expand All @@ -173,9 +172,9 @@ func (c *CostCenterManager) UpdateCostCenter(ctx context.Context, newCC CostCent
// Downgrading from stripe
if existingCC.BillingStrategy == CostCenter_Stripe && newCC.BillingStrategy == CostCenter_Other {
newCC.SpendingLimit = c.cfg.ForUsers
newCC.BillingCycleStart = common_db.NewVarCharTime(now)
newCC.BillingCycleStart = NewVarCharTime(now)
// see you next month
newCC.NextBillingTime = common_db.NewVarCharTime(now.AddDate(0, 1, 0))
newCC.NextBillingTime = NewVarCharTime(now.AddDate(0, 1, 0))
}

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

newCC.BillingCycleStart = common_db.NewVarCharTime(now)
newCC.BillingCycleStart = NewVarCharTime(now)
// set an informative nextBillingTime, even though we don't manage Stripe billing cycle
newCC.NextBillingTime = common_db.NewVarCharTime(now.AddDate(0, 1, 0))
newCC.NextBillingTime = NewVarCharTime(now.AddDate(0, 1, 0))
}
} else if isTeam {
// Billing strategy is Other, and it remains unchanged
Expand All @@ -201,9 +200,9 @@ func (c *CostCenterManager) UpdateCostCenter(ctx context.Context, newCC CostCent
// Downgrading from stripe
if existingCC.BillingStrategy == CostCenter_Stripe && newCC.BillingStrategy == CostCenter_Other {
newCC.SpendingLimit = c.cfg.ForTeams
newCC.BillingCycleStart = common_db.NewVarCharTime(now)
newCC.BillingCycleStart = NewVarCharTime(now)
// see you next month
newCC.NextBillingTime = common_db.NewVarCharTime(now.AddDate(0, 1, 0))
newCC.NextBillingTime = NewVarCharTime(now.AddDate(0, 1, 0))
}

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

newCC.BillingCycleStart = common_db.NewVarCharTime(now)
newCC.BillingCycleStart = NewVarCharTime(now)
// set an informative nextBillingTime, even though we don't manage Stripe billing cycle
newCC.NextBillingTime = common_db.NewVarCharTime(now.AddDate(0, 1, 0))
newCC.NextBillingTime = NewVarCharTime(now.AddDate(0, 1, 0))
}
} else {
return CostCenter{}, status.Errorf(codes.InvalidArgument, "Unknown attribution entity %s", string(attributionID))
Expand Down Expand Up @@ -260,7 +259,7 @@ func (c *CostCenterManager) NewInvoiceUsageRecord(ctx context.Context, attributi
AttributionID: attributionID,
Description: "Credits",
CreditCents: creditCents * -1,
EffectiveTime: common_db.NewVarCharTime(now),
EffectiveTime: NewVarCharTime(now),
Kind: InvoiceUsageKind,
Draft: false,
}, nil
Expand All @@ -283,7 +282,7 @@ func (c *CostCenterManager) ListLatestCostCentersWithBillingTimeBefore(ctx conte
Joins("INNER JOIN (?) AS expiredCC on cc.id = expiredCC.id AND cc.creationTime = expiredCC.creationTime", subquery).
Where("cc.billingStrategy = ?", strategy).
Where("nextBillingTime != ?", "").
Where("nextBillingTime < ?", common_db.TimeToISO8601(billingTimeBefore)).
Where("nextBillingTime < ?", TimeToISO8601(billingTimeBefore)).
FindInBatches(&batch, 1000, func(tx *gorm.DB, iteration int) error {
results = append(results, batch...)
return nil
Expand Down Expand Up @@ -336,9 +335,9 @@ func (c *CostCenterManager) ResetUsage(ctx context.Context, cc CostCenter) (Cost
ID: cc.ID,
SpendingLimit: spendingLimit,
BillingStrategy: cc.BillingStrategy,
BillingCycleStart: common_db.NewVarCharTime(now),
NextBillingTime: common_db.NewVarCharTime(nextBillingTime),
CreationTime: common_db.NewVarCharTime(now),
BillingCycleStart: NewVarCharTime(now),
NextBillingTime: NewVarCharTime(nextBillingTime),
CreationTime: NewVarCharTime(now),
}
err = c.conn.Save(&newCostCenter).Error
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
"testing"
"time"

common_db "github.com/gitpod-io/gitpod/common-go/db"
"github.com/gitpod-io/gitpod/usage/pkg/db"
"github.com/gitpod-io/gitpod/usage/pkg/db/dbtest"
"github.com/gitpod-io/gitpod/common-go/db"
"github.com/gitpod-io/gitpod/common-go/db/dbtest"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -73,28 +72,28 @@ func TestCostCenterManager_GetOrCreateCostCenter_ResetsExpired(t *testing.T) {

expiredCC := db.CostCenter{
ID: db.NewTeamAttributionID(uuid.New().String()),
CreationTime: common_db.NewVarCharTime(now),
CreationTime: db.NewVarCharTime(now),
SpendingLimit: 0,
BillingStrategy: db.CostCenter_Other,
NextBillingTime: common_db.NewVarCharTime(expired),
BillingCycleStart: common_db.NewVarCharTime(now),
NextBillingTime: db.NewVarCharTime(expired),
BillingCycleStart: db.NewVarCharTime(now),
}
unexpiredCC := db.CostCenter{
ID: db.NewUserAttributionID(uuid.New().String()),
CreationTime: common_db.NewVarCharTime(now),
CreationTime: db.NewVarCharTime(now),
SpendingLimit: 500,
BillingStrategy: db.CostCenter_Other,
NextBillingTime: common_db.NewVarCharTime(unexpired),
BillingCycleStart: common_db.NewVarCharTime(now),
NextBillingTime: db.NewVarCharTime(unexpired),
BillingCycleStart: db.NewVarCharTime(now),
}
// Stripe billing strategy should not be reset
stripeCC := db.CostCenter{
ID: db.NewUserAttributionID(uuid.New().String()),
CreationTime: common_db.NewVarCharTime(now),
CreationTime: db.NewVarCharTime(now),
SpendingLimit: 0,
BillingStrategy: db.CostCenter_Stripe,
NextBillingTime: common_db.VarcharTime{},
BillingCycleStart: common_db.NewVarCharTime(now),
NextBillingTime: db.VarcharTime{},
BillingCycleStart: db.NewVarCharTime(now),
}

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

// unexpired cost center must not be reset
retrievedUnexpiredCC, err := mnr.GetOrCreateCostCenter(context.Background(), unexpiredCC.ID)
require.NoError(t, err)
require.Equal(t, common_db.NewVarCharTime(unexpired).Time(), retrievedUnexpiredCC.NextBillingTime.Time())
require.Equal(t, db.NewVarCharTime(unexpired).Time(), retrievedUnexpiredCC.NextBillingTime.Time())
require.Equal(t, unexpiredCC.ID, retrievedUnexpiredCC.ID)
require.Equal(t, unexpiredCC.BillingStrategy, retrievedUnexpiredCC.BillingStrategy)
require.WithinDuration(t, unexpiredCC.CreationTime.Time(), retrievedUnexpiredCC.CreationTime.Time(), 100*time.Millisecond)
Expand Down Expand Up @@ -349,16 +348,16 @@ func TestCostCenter_ListLatestCostCentersWithBillingTimeBefore(t *testing.T) {
dbtest.NewCostCenter(t, db.CostCenter{
ID: db.NewTeamAttributionID(attributionID),
SpendingLimit: 100,
CreationTime: common_db.NewVarCharTime(firstCreation),
CreationTime: db.NewVarCharTime(firstCreation),
BillingStrategy: db.CostCenter_Other,
NextBillingTime: common_db.NewVarCharTime(firstCreation),
NextBillingTime: db.NewVarCharTime(firstCreation),
}),
dbtest.NewCostCenter(t, db.CostCenter{
ID: db.NewTeamAttributionID(attributionID),
SpendingLimit: 100,
CreationTime: common_db.NewVarCharTime(secondCreation),
CreationTime: db.NewVarCharTime(secondCreation),
BillingStrategy: db.CostCenter_Other,
NextBillingTime: common_db.NewVarCharTime(secondCreation),
NextBillingTime: db.NewVarCharTime(secondCreation),
}),
}

Expand Down Expand Up @@ -386,14 +385,14 @@ func TestCostCenter_ListLatestCostCentersWithBillingTimeBefore(t *testing.T) {
dbtest.NewCostCenter(t, db.CostCenter{
ID: db.NewTeamAttributionID(attributionID),
SpendingLimit: 100,
CreationTime: common_db.NewVarCharTime(firstCreation),
CreationTime: db.NewVarCharTime(firstCreation),
BillingStrategy: db.CostCenter_Other,
NextBillingTime: common_db.NewVarCharTime(firstCreation),
NextBillingTime: db.NewVarCharTime(firstCreation),
}),
dbtest.NewCostCenter(t, db.CostCenter{
ID: db.NewTeamAttributionID(attributionID),
SpendingLimit: 100,
CreationTime: common_db.NewVarCharTime(secondCreation),
CreationTime: db.NewVarCharTime(secondCreation),
BillingStrategy: db.CostCenter_Stripe,
}),
}
Expand All @@ -418,7 +417,7 @@ func TestCostCenterManager_ResetUsage(t *testing.T) {
})
_, err := mnr.ResetUsage(context.Background(), db.CostCenter{
ID: db.NewUserAttributionID(uuid.New().String()),
CreationTime: common_db.NewVarCharTime(time.Now()),
CreationTime: db.NewVarCharTime(time.Now()),
SpendingLimit: 500,
BillingStrategy: db.CostCenter_Stripe,
})
Expand All @@ -433,10 +432,10 @@ func TestCostCenterManager_ResetUsage(t *testing.T) {
})
oldCC := db.CostCenter{
ID: db.NewTeamAttributionID(uuid.New().String()),
CreationTime: common_db.NewVarCharTime(time.Now()),
CreationTime: db.NewVarCharTime(time.Now()),
SpendingLimit: 0,
BillingStrategy: db.CostCenter_Other,
NextBillingTime: common_db.NewVarCharTime(ts),
NextBillingTime: db.NewVarCharTime(ts),
}
newCC, err := mnr.ResetUsage(context.Background(), oldCC)
require.NoError(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"sync"
"testing"

common_db "github.com/gitpod-io/gitpod/common-go/db"
"github.com/gitpod-io/gitpod/common-go/db"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
)
Expand All @@ -31,13 +31,13 @@ func ConnectForTests(t *testing.T) *gorm.DB {
// These are static connection details for tests, started by `leeway components/usage:init-testdb`.
// We use the same static credentials for CI & local instance of MySQL Server.
var err error
conn, err = common_db.Connect(common_db.ConnectionParams{
conn, err = db.Connect(db.ConnectionParams{
User: "root",
Password: "test",
Host: "localhost:23306",
Database: "gitpod",
})
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.")
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.")

return conn
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
"testing"
"time"

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

result := db.CostCenter{
ID: db.NewUserAttributionID(uuid.New().String()),
CreationTime: common_db.NewVarCharTime(time.Now()),
CreationTime: db.NewVarCharTime(time.Now()),
SpendingLimit: 100,
BillingStrategy: db.CostCenter_Stripe,
BillingCycleStart: common_db.NewVarCharTime(time.Now()),
NextBillingTime: common_db.NewVarCharTime(time.Now().Add(10 * time.Hour)),
BillingCycleStart: db.NewVarCharTime(time.Now()),
NextBillingTime: db.NewVarCharTime(time.Now().Add(10 * time.Hour)),
}

if record.ID != "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
"testing"
"time"

common_db "github.com/gitpod-io/gitpod/common-go/db"
"github.com/gitpod-io/gitpod/usage/pkg/db"
"github.com/gitpod-io/gitpod/common-go/db"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
Expand All @@ -22,7 +21,7 @@ func NewStripeCustomer(t *testing.T, customer db.StripeCustomer) db.StripeCustom
result := db.StripeCustomer{
StripeCustomerID: fmt.Sprintf("cus_%s", uuid.New().String()),
AttributionID: db.NewUserAttributionID(uuid.New().String()),
CreationTime: common_db.NewVarCharTime(time.Now()),
CreationTime: db.NewVarCharTime(time.Now()),
}

if customer.StripeCustomerID != "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
"context"
"testing"

common_db "github.com/gitpod-io/gitpod/common-go/db"
"github.com/gitpod-io/gitpod/usage/pkg/db"
"github.com/gitpod-io/gitpod/common-go/db"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
Expand All @@ -25,7 +24,7 @@ func NewUsage(t *testing.T, record db.Usage) db.Usage {
AttributionID: db.NewUserAttributionID(uuid.New().String()),
Description: "some description",
CreditCents: 42,
EffectiveTime: common_db.VarcharTime{},
EffectiveTime: db.VarcharTime{},
Kind: db.WorkspaceInstanceUsageKind,
WorkspaceInstanceID: &workspaceInstanceId,
}
Expand Down
Loading