Skip to content

[db][usage] Add d_b_billed_session table migration and Go definitions #11868

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

Merged
merged 1 commit into from
Aug 4, 2022
Merged
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
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you generate this migration? My own process for generating a migration for a new table using typeorm is somewhat convoluted 😬

I think there is an opportunity to improve the README here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh, it's this README instead of the one you linked above. Definitely an opportunity to improve how we point to the right docs 🙈

public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {}
}
29 changes: 29 additions & 0 deletions components/usage/pkg/db/billed_session.go
Original file line number Diff line number Diff line change
@@ -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"
}
43 changes: 43 additions & 0 deletions components/usage/pkg/db/billed_session_test.go
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also delete the created record at the end of the test. For other DB models, there are helpers (in dbtest package) that do this automatically by hooking into the t *testing.T when the test finishes.

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)
})
}