Skip to content

Commit 19776bf

Browse files
easyCZroboquat
authored andcommitted
[usage] Add db.Project model in golang
1 parent 617aef7 commit 19776bf

File tree

5 files changed

+116
-3
lines changed

5 files changed

+116
-3
lines changed

components/usage/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ replace k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.23.5 /
5757
require (
5858
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
5959
github.com/go-sql-driver/mysql v1.6.0
60+
github.com/google/uuid v1.1.2
6061
github.com/relvacode/iso8601 v1.1.0
6162
github.com/robfig/cron v1.2.0
6263
github.com/spf13/cobra v1.4.0

components/usage/go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ github.com/google/pprof v0.0.0-20200229191704-1ebb73c60ed3/go.mod h1:ZgVRPoUq/hf
151151
github.com/google/pprof v0.0.0-20200430221834-fc25d7d30c6d/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
152152
github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
153153
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
154+
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
154155
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
155156
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
156157
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=

components/usage/pkg/db/project.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
"database/sql"
9+
"github.com/google/uuid"
10+
"gorm.io/datatypes"
11+
"time"
12+
)
13+
14+
type Project struct {
15+
ID uuid.UUID `gorm:"primary_key;column:id;type:char;size:36;" json:"id"`
16+
Name string `gorm:"column:name;type:varchar;size:255;" json:"name"`
17+
CloneURL string `gorm:"column:cloneUrl;type:varchar;size:255;" json:"cloneUrl"`
18+
Slug sql.NullString `gorm:"column:slug;type:varchar;size:255;" json:"slug"`
19+
Settings datatypes.JSON `gorm:"column:settings;type:text;size:65535;" json:"settings"`
20+
21+
AppInstallationID string `gorm:"column:appInstallationId;type:varchar;size:255;" json:"appInstallationId"`
22+
23+
CreationTime VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"`
24+
LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"`
25+
26+
TeamID sql.NullString `gorm:"column:teamId;type:char;size:36;" json:"teamId"`
27+
UserID sql.NullString `gorm:"column:userId;type:char;size:36;" json:"userId"`
28+
29+
MarkedDeleted bool `gorm:"column:markedDeleted;type:tinyint;default:0;" json:"markedDeleted"`
30+
31+
// deleted is reserved for use by db-sync
32+
_ int32 `gorm:"column:deleted;type:tinyint;default:0;" json:"deleted"`
33+
}
34+
35+
// TableName sets the insert table name for this struct type
36+
func (d *Project) TableName() string {
37+
return "d_b_project"
38+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 projectJSON = map[string]interface{}{
18+
"id": "49b5d309-bb95-4a79-846c-e3cf42c0d591",
19+
"cloneUrl": "https://github.com/gptest1/gptest1-repo1-private.git",
20+
"teamId": "0e433063-1358-4892-9ed2-68e273d17d07",
21+
"appInstallationId": "20446411",
22+
"creationTime": "2021-11-01T19:36:07.532Z",
23+
"deleted": 0,
24+
"_lastModified": "2021-11-02 10:49:12.473658",
25+
"name": "gptest1-repo1-private",
26+
"config": "{\".gitpod.yml\":\"tasks:\\n - init: |\\n echo 'TODO: build project'\\n command: |\\n echo 'TODO: start app'\"}",
27+
"markedDeleted": 1,
28+
"userId": nil,
29+
"slug": "gptest1-repo1-private",
30+
"settings": nil,
31+
}
32+
33+
func TestProject_ReadExistingRecords(t *testing.T) {
34+
conn := db.ConnectForTests(t)
35+
36+
id := insertRawProject(t, conn, projectJSON)
37+
38+
project := db.Project{ID: id}
39+
tx := conn.First(&project)
40+
require.NoError(t, tx.Error)
41+
42+
require.Equal(t, id, project.ID)
43+
require.Equal(t, projectJSON["teamId"], project.TeamID.String)
44+
require.Equal(t, stringToVarchar(t, "2021-11-01T19:36:07.532Z"), project.CreationTime)
45+
}
46+
47+
func insertRawProject(t *testing.T, conn *gorm.DB, obj map[string]interface{}) uuid.UUID {
48+
columns := []string{
49+
"id", "cloneUrl", "teamId", "appInstallationId", "creationTime", "deleted", "_lastModified", "name", "config", "markedDeleted", "userId", "slug", "settings",
50+
}
51+
statement := fmt.Sprintf(`INSERT INTO d_b_project (%s) VALUES ?;`, strings.Join(columns, ", "))
52+
id := uuid.MustParse(obj["id"].(string))
53+
54+
var values []interface{}
55+
for _, col := range columns {
56+
val, ok := obj[col]
57+
if !ok {
58+
values = append(values, "null")
59+
} else {
60+
values = append(values, val)
61+
}
62+
}
63+
64+
tx := conn.Exec(statement, values)
65+
require.NoError(t, tx.Error)
66+
67+
t.Cleanup(func() {
68+
tx = conn.Delete(&db.Project{ID: id})
69+
require.NoError(t, tx.Error)
70+
})
71+
72+
return id
73+
}

components/usage/pkg/db/workspace.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ type Workspace struct {
3333
SoftDeletedTime VarcharTime `gorm:"column:softDeletedTime;type:varchar;size:255;" json:"softDeletedTime"`
3434
ContentDeletedTime VarcharTime `gorm:"column:contentDeletedTime;type:varchar;size:255;" json:"contentDeletedTime"`
3535

36-
Archived int32 `gorm:"column:archived;type:tinyint;default:0;" json:"archived"`
37-
Shareable int32 `gorm:"column:shareable;type:tinyint;default:0;" json:"shareable"`
36+
Archived bool `gorm:"column:archived;type:tinyint;default:0;" json:"archived"`
37+
Shareable bool `gorm:"column:shareable;type:tinyint;default:0;" json:"shareable"`
3838

3939
SoftDeleted sql.NullString `gorm:"column:softDeleted;type:char;size:4;" json:"softDeleted"`
40-
Pinned int32 `gorm:"column:pinned;type:tinyint;default:0;" json:"pinned"`
40+
Pinned bool `gorm:"column:pinned;type:tinyint;default:0;" json:"pinned"`
4141

4242
// deleted is reserved for use by db-sync
4343
_ int32 `gorm:"column:deleted;type:tinyint;default:0;" json:"deleted"`

0 commit comments

Comments
 (0)