Skip to content

[usage] Add db.Team model in golang #10369

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
Jun 7, 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
4 changes: 0 additions & 4 deletions components/usage/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ require (
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
github.com/go-sql-driver/mysql v1.6.0
github.com/google/uuid v1.1.2
github.com/google/uuid v1.1.2
github.com/jackc/pgtype v1.10.0
github.com/relvacode/iso8601 v1.1.0
github.com/robfig/cron v1.2.0
github.com/spf13/cobra v1.4.0
Expand All @@ -73,14 +71,12 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gofrs/uuid v4.0.0+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/hashicorp/golang-lru v0.5.1 // indirect
github.com/heptiolabs/healthcheck v0.0.0-20211123025425-613501dd5deb // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.4 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
Expand Down
2 changes: 0 additions & 2 deletions components/usage/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
Expand Down Expand Up @@ -249,7 +248,6 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8=
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
Expand Down
29 changes: 29 additions & 0 deletions components/usage/pkg/db/team.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 (
"github.com/google/uuid"
"time"
)

type Team struct {
ID uuid.UUID `gorm:"primary_key;column:id;type:char;size:36;" json:"id"`
Name string `gorm:"column:name;type:varchar;size:255;" json:"name"`
Slug string `gorm:"column:slug;type:varchar;size:255;" json:"slug"`

CreationTime VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"`
LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"`

MarkedDeleted bool `gorm:"column:markedDeleted;type:tinyint;default:0;" json:"marked_deleted"`

// deleted is reserved for use by db-sync.
_ bool `gorm:"column:deleted;type:tinyint;default:0;" json:"deleted"`
Comment on lines +17 to +23
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you embed gorm.Model here to get these fields for free or do the two meaning of the deleted field prevent that?

}

// TableName sets the insert table name for this struct type
func (d *Team) TableName() string {
return "d_b_team"
}
56 changes: 56 additions & 0 deletions components/usage/pkg/db/team_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 (
"fmt"
"github.com/gitpod-io/gitpod/usage/pkg/db"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
"strings"
"testing"
)

var teamJSON = map[string]interface{}{
"id": "2db3d0ae-9fa2-41ed-9781-acfc7e17294b",
"name": "Gitpod",
"slug": "gitpod",
"creationTime": "2021-07-22T11:46:43.281Z",
"deleted": 0,
"_lastModified": "2021-10-01 10:26:10.718585",
"markedDeleted": 0,
}

func TestTeam_ReadFromExistingData(t *testing.T) {
conn := db.ConnectForTests(t)
id := insertRawTeam(t, conn, teamJSON)

team := db.Team{ID: id}
tx := conn.First(&team)
require.NoError(t, tx.Error)

require.Equal(t, id, team.ID)
require.Equal(t, teamJSON["name"], team.Name)
require.Equal(t, teamJSON["slug"], team.Slug)
require.Equal(t, stringToVarchar(t, teamJSON["creationTime"].(string)), team.CreationTime)
require.EqualValues(t, teamJSON["markedDeleted"] == 1, team.MarkedDeleted)
}

func insertRawTeam(t *testing.T, conn *gorm.DB, object map[string]interface{}) uuid.UUID {
t.Helper()

columns := []string{"id", "name", "slug", "creationTime", "deleted", "_lastModified", "markedDeleted"}
statement := fmt.Sprintf(`INSERT INTO d_b_team (%s) VALUES ?;`, strings.Join(columns, ", "))

id := uuid.MustParse(insertRawObject(t, conn, columns, statement, object))

t.Cleanup(func() {
tx := conn.Delete(&db.Team{ID: id})
require.NoError(t, tx.Error)
})

return id
}