From 3533c3f67b1548a83e7d4388c4a672eb2310cb20 Mon Sep 17 00:00:00 2001 From: Jan Keromnes Date: Thu, 4 Aug 2022 08:49:05 +0000 Subject: [PATCH] [db][usage] Add d_b_billed_session table migration and Go definitions Co-authored-by: Laurie T. Malau --- .../migration/1659601647550-BilledSession.ts | 17 ++++++++ components/usage/pkg/db/billed_session.go | 29 +++++++++++++ .../usage/pkg/db/billed_session_test.go | 43 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 components/gitpod-db/src/typeorm/migration/1659601647550-BilledSession.ts create mode 100644 components/usage/pkg/db/billed_session.go create mode 100644 components/usage/pkg/db/billed_session_test.go diff --git a/components/gitpod-db/src/typeorm/migration/1659601647550-BilledSession.ts b/components/gitpod-db/src/typeorm/migration/1659601647550-BilledSession.ts new file mode 100644 index 00000000000000..71b58793643ddf --- /dev/null +++ b/components/gitpod-db/src/typeorm/migration/1659601647550-BilledSession.ts @@ -0,0 +1,17 @@ +/** + * Copyright (c) 2022 Gitpod GmbH. All rights reserved. + * Licensed under the GNU Affero General Public License (AGPL). + * See License-AGPL.txt in the project root for license information. + */ + +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class BilledSession1659601647550 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + "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` ENUM('chargebee','stripe') 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;", + ); + } + + public async down(queryRunner: QueryRunner): Promise {} +} diff --git a/components/usage/pkg/db/billed_session.go b/components/usage/pkg/db/billed_session.go new file mode 100644 index 00000000000000..8290ced8fec8aa --- /dev/null +++ b/components/usage/pkg/db/billed_session.go @@ -0,0 +1,29 @@ +// Copyright (c) 2022 Gitpod GmbH. All rights reserved. +// Licensed under the GNU Affero General Public License (AGPL). +// See License-AGPL.txt in the project root for license information. + +package db + +import ( + "time" + + "github.com/google/uuid" +) + +// BilledSession represents the underlying DB object +type BilledSession struct { + InstanceID uuid.UUID `gorm:"primary_key;column:instanceId;type:char;size:36;" json:"instanceId"` + From VarcharTime `gorm:"primary_key;column:from;type:varchar;size:255;" json:"from"` + To VarcharTime `gorm:"column:to;type:varchar;size:255;" json:"to"` + System string `gorm:"column:system;type:varchar;size:255;" json:"system"` + InvoiceID string `gorm:"column:invoiceId;type:varchar;size:255;" json:"invoiceId"` + LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"` + + // deleted is restricted for use by db-sync + _ bool `gorm:"column:deleted;type:tinyint;default:0;" json:"deleted"` +} + +// TableName sets the insert table name for this struct type +func (d *BilledSession) TableName() string { + return "d_b_billed_session" +} diff --git a/components/usage/pkg/db/billed_session_test.go b/components/usage/pkg/db/billed_session_test.go new file mode 100644 index 00000000000000..8324cbe00fca23 --- /dev/null +++ b/components/usage/pkg/db/billed_session_test.go @@ -0,0 +1,43 @@ +// Copyright (c) 2022 Gitpod GmbH. All rights reserved. +// Licensed under the GNU Affero General Public License (AGPL). +// See License-AGPL.txt in the project root for license information. + +package db_test + +import ( + "testing" + "time" + + "github.com/gitpod-io/gitpod/usage/pkg/db" + "github.com/gitpod-io/gitpod/usage/pkg/db/dbtest" + "github.com/google/uuid" + "github.com/stretchr/testify/require" +) + +func TestBilledSession_WriteRead(t *testing.T) { + conn := dbtest.ConnectForTests(t) + + billedSession := &db.BilledSession{ + InstanceID: uuid.New(), + From: db.NewVarcharTime(time.Date(2022, 8, 04, 12, 00, 00, 00, time.UTC)), + To: db.NewVarcharTime(time.Date(2022, 9, 04, 12, 00, 00, 00, time.UTC)), + System: "chargebee", + InvoiceID: "some-invoice-ID", + } + + tx := conn.Create(billedSession) + require.NoError(t, tx.Error) + + read := &db.BilledSession{InstanceID: billedSession.InstanceID} + tx = conn.First(read) + require.NoError(t, tx.Error) + require.Equal(t, billedSession.InstanceID, read.InstanceID) + require.Equal(t, billedSession.From, read.From) + require.Equal(t, billedSession.To, read.To) + require.Equal(t, billedSession.System, read.System) + require.Equal(t, billedSession.InvoiceID, read.InvoiceID) + + t.Cleanup(func() { + conn.Model(&db.BilledSession{}).Delete(billedSession) + }) +}