Skip to content

Commit 52ac5a5

Browse files
easyCZroboquat
authored andcommitted
[usage] Add db.Team model in golang
1 parent 5ed1deb commit 52ac5a5

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

components/usage/pkg/db/team.go

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+
"github.com/google/uuid"
9+
"time"
10+
)
11+
12+
type Team struct {
13+
ID uuid.UUID `gorm:"primary_key;column:id;type:char;size:36;" json:"id"`
14+
Name string `gorm:"column:name;type:varchar;size:255;" json:"name"`
15+
Slug string `gorm:"column:slug;type:varchar;size:255;" json:"slug"`
16+
17+
CreationTime VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"`
18+
LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"`
19+
20+
MarkedDeleted bool `gorm:"column:markedDeleted;type:tinyint;default:0;" json:"marked_deleted"`
21+
22+
// deleted is reserved 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 *Team) TableName() string {
28+
return "d_b_team"
29+
}

components/usage/pkg/db/team_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
"fmt"
9+
"github.com/gitpod-io/gitpod/usage/pkg/db"
10+
"github.com/google/uuid"
11+
"github.com/stretchr/testify/require"
12+
"gorm.io/gorm"
13+
"strings"
14+
"testing"
15+
)
16+
17+
var teamJSON = map[string]interface{}{
18+
"id": "2db3d0ae-9fa2-41ed-9781-acfc7e17294b",
19+
"name": "Gitpod",
20+
"slug": "gitpod",
21+
"creationTime": "2021-07-22T11:46:43.281Z",
22+
"deleted": 0,
23+
"_lastModified": "2021-10-01 10:26:10.718585",
24+
"markedDeleted": 0,
25+
}
26+
27+
func TestTeam_ReadFromExistingData(t *testing.T) {
28+
conn := db.ConnectForTests(t)
29+
id := insertRawTeam(t, conn, teamJSON)
30+
31+
team := db.Team{ID: id}
32+
tx := conn.First(&team)
33+
require.NoError(t, tx.Error)
34+
35+
require.Equal(t, id, team.ID)
36+
require.Equal(t, teamJSON["name"], team.Name)
37+
require.Equal(t, teamJSON["slug"], team.Slug)
38+
require.Equal(t, stringToVarchar(t, teamJSON["creationTime"].(string)), team.CreationTime)
39+
require.EqualValues(t, teamJSON["markedDeleted"] == 1, team.MarkedDeleted)
40+
}
41+
42+
func insertRawTeam(t *testing.T, conn *gorm.DB, object map[string]interface{}) uuid.UUID {
43+
t.Helper()
44+
45+
columns := []string{"id", "name", "slug", "creationTime", "deleted", "_lastModified", "markedDeleted"}
46+
statement := fmt.Sprintf(`INSERT INTO d_b_team (%s) VALUES ?;`, strings.Join(columns, ", "))
47+
48+
id := uuid.MustParse(insertRawObject(t, conn, columns, statement, object))
49+
50+
t.Cleanup(func() {
51+
tx := conn.Delete(&db.Team{ID: id})
52+
require.NoError(t, tx.Error)
53+
})
54+
55+
return id
56+
}

0 commit comments

Comments
 (0)