Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
easyCZ committed Jun 8, 2022
1 parent bd3f138 commit 205eeff
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
1 change: 1 addition & 0 deletions components/usage/pkg/db/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func ConnectForTests(t *testing.T) *gorm.DB {
&Workspace{},
&Project{},
&Team{},
&TeamMembership{},
} {
// See https://gorm.io/docs/delete.html#Block-Global-Delete
tx := conn.Session(&gorm.Session{AllowGlobalUpdate: true}).Delete(model)
Expand Down
33 changes: 25 additions & 8 deletions components/usage/pkg/db/team_membership.go
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
// 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 TeamMembership struct {
ID string `gorm:"primary_key;column:id;type:char;size:36;" json:"id"`
TeamID string `gorm:"column:teamId;type:char;size:36;" json:"team_id"`
UserID string `gorm:"column:userId;type:char;size:36;" json:"user_id"`
Role string `gorm:"column:role;type:varchar;size:255;" json:"role"`
CreationTime string `gorm:"column:creationTime;type:varchar;size:255;" json:"creation_time"`
Deleted bool `gorm:"column:deleted;type:tinyint;default:0;" json:"deleted"`
LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_last_modified"`
SubscriptionID string `gorm:"column:subscriptionId;type:char;size:36;" json:"subscription_id"`
ID uuid.UUID `gorm:"primary_key;column:id;type:char;size:36;" json:"id"`

TeamID uuid.UUID `gorm:"column:teamId;type:char;size:36;" json:"teamId"`
UserID uuid.UUID `gorm:"column:userId;type:char;size:36;" json:"userId"`
Role TeamMembershipRole `gorm:"column:role;type:varchar;size:255;" json:"role"`
SubscriptionID uuid.UUID `gorm:"column:subscriptionId;type:char;size:36;" json:"subscriptionId"`

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

// deleted column is reserved 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 *TeamMembership) TableName() string {
return "d_b_team_membership"
}

type TeamMembershipRole string

const (
TeamMembershipRole_Owner = TeamMembershipRole("owner")
TeamMembershipRole_Member = TeamMembershipRole("member")
)
32 changes: 32 additions & 0 deletions components/usage/pkg/db/team_membership_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 (
"github.com/gitpod-io/gitpod/usage/pkg/db"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"testing"
)

func TestTeamMembership_WriteRead(t *testing.T) {
conn := db.ConnectForTests(t)

membership := &db.TeamMembership{
ID: uuid.New(),
TeamID: uuid.New(),
UserID: uuid.New(),
Role: db.TeamMembershipRole_Member,
SubscriptionID: uuid.New(),
}

tx := conn.Create(membership)
require.NoError(t, tx.Error)

read := &db.TeamMembership{ID: membership.ID}
tx = conn.First(read)
require.NoError(t, tx.Error)
require.Equal(t, membership, read)
}

0 comments on commit 205eeff

Please sign in to comment.