Skip to content

Commit e04ef72

Browse files
jankeromnesLaurie T. Malau
andcommitted
[db][usage] Add d_b_billed_session table migration and Go definitions
Co-authored-by: Laurie T. Malau <[email protected]>
1 parent d5c9bd7 commit e04ef72

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License-AGPL.txt in the project root for license information.
5+
*/
6+
7+
import { MigrationInterface, QueryRunner } from "typeorm";
8+
9+
export class BilledSession1659601647550 implements MigrationInterface {
10+
public async up(queryRunner: QueryRunner): Promise<void> {
11+
await queryRunner.query(
12+
"CREATE TABLE IF NOT EXISTS `d_b_billed_session` (`instanceId` char(36) NOT NULL, `from` varchar(255) NOT NULL, `to` varchar(255) NOT NULL DEFAULT '', `system` varchar(255) NOT NULL, `invoiceId` varchar(255) NOT NULL DEFAULT '', `deleted` tinyint(4) NOT NULL DEFAULT '0', `_lastModified` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), PRIMARY KEY (`instanceId`, `from`), KEY `ind_dbsync` (`_lastModified`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;",
13+
);
14+
}
15+
16+
public async down(queryRunner: QueryRunner): Promise<void> {}
17+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package db
6+
7+
import (
8+
"time"
9+
10+
"github.com/google/uuid"
11+
)
12+
13+
// BilledSession represents the underlying DB object
14+
type BilledSession struct {
15+
InstanceID uuid.UUID `gorm:"primary_key;column:instanceId;type:char;size:36;" json:"instanceId"`
16+
From VarcharTime `gorm:"primary_key;column:from;type:varchar;size:255;" json:"from"`
17+
To VarcharTime `gorm:"column:to;type:varchar;size:255;" json:"to"`
18+
System string `gorm:"column:system;type:varchar;size:255;" json:"system"`
19+
InvoiceID string `gorm:"column:invoiceId;type:varchar;size:255;" json:"invoiceId"`
20+
LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"`
21+
22+
// deleted is restricted for use by db-sync
23+
_ bool `gorm:"column:deleted;type:tinyint;default:0;" json:"deleted"`
24+
}
25+
26+
// TableName sets the insert table name for this struct type
27+
func (d *BilledSession) TableName() string {
28+
return "d_b_billed_session"
29+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package db_test
6+
7+
import (
8+
"testing"
9+
"time"
10+
11+
"github.com/gitpod-io/gitpod/usage/pkg/db"
12+
"github.com/gitpod-io/gitpod/usage/pkg/db/dbtest"
13+
"github.com/google/uuid"
14+
"github.com/stretchr/testify/require"
15+
)
16+
17+
func TestBilledSession_WriteRead(t *testing.T) {
18+
conn := dbtest.ConnectForTests(t)
19+
teamID := uuid.New().String()
20+
teamAttributionID := db.NewTeamAttributionID(teamID)
21+
22+
instance := dbtest.NewWorkspaceInstance(t, db.WorkspaceInstance{
23+
ID: uuid.New(),
24+
UsageAttributionID: teamAttributionID,
25+
CreationTime: db.NewVarcharTime(time.Date(2022, 05, 1, 00, 00, 00, 00, time.UTC)),
26+
StartedTime: db.NewVarcharTime(time.Date(2022, 05, 1, 00, 00, 00, 00, time.UTC)),
27+
StoppedTime: db.NewVarcharTime(time.Date(2022, 06, 1, 1, 0, 0, 0, time.UTC)),
28+
})
29+
30+
billedSession := &db.BilledSession{
31+
InstanceID: instance.ID,
32+
From: db.NewVarcharTime(time.Date(2022, 8, 04, 12, 00, 00, 00, time.UTC)),
33+
To: db.NewVarcharTime(time.Date(2022, 9, 04, 12, 00, 00, 00, time.UTC)),
34+
System: "chargebee",
35+
InvoiceID: "some-invoice-ID",
36+
}
37+
38+
tx := conn.Create(billedSession)
39+
require.NoError(t, tx.Error)
40+
41+
read := &db.BilledSession{InstanceID: instance.ID}
42+
tx = conn.First(read)
43+
require.NoError(t, tx.Error)
44+
require.Equal(t, billedSession.InstanceID, read.InstanceID)
45+
require.Equal(t, billedSession.From, read.From)
46+
require.Equal(t, billedSession.To, read.To)
47+
require.Equal(t, billedSession.System, read.System)
48+
require.Equal(t, billedSession.InvoiceID, read.InvoiceID)
49+
}

0 commit comments

Comments
 (0)