-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
components/gitpod-db/src/typeorm/migration/1659601647550-BilledSession.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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> {} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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) | ||
}) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 🙈