Skip to content

Commit 4412749

Browse files
committed
Fix
1 parent ee90a13 commit 4412749

File tree

7 files changed

+113
-67
lines changed

7 files changed

+113
-67
lines changed

.idea/misc.xml

Lines changed: 30 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/gitpod-db-go/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/gitpod-io/gitpod/gitpod-db
33
go 1.17
44

55
require (
6+
github.com/google/uuid v1.3.0
67
github.com/stretchr/testify v1.7.1
78
gorm.io/driver/mysql v1.3.2
89
gorm.io/gorm v1.23.3

components/gitpod-db-go/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
44
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
5+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
6+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
57
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
68
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
79
github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=

components/gitpod-db-go/team.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package db
22

33
import (
44
"context"
5+
"github.com/google/uuid"
56
)
67

78
type Team struct {
8-
ID string `gorm:"primaryKey"`
9+
ID uuid.UUID `gorm:"primaryKey"`
910
Name string
1011
Slug string
1112
CreationTime StringlyTime `gorm:"column:creationTime"`
12-
MarkedDeleted bool
13+
MarkedDeleted bool `gorm:"column:markedDeleted"`
1314
}
1415

1516
// TableName overrides default GORM handling of table name generation

components/gitpod-db-go/team_test.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package db
22

33
import (
4-
"fmt"
4+
"github.com/google/uuid"
55
"github.com/stretchr/testify/require"
66
"os"
77
"testing"
8+
"time"
89
)
910

1011
func TestTeam(t *testing.T) {
@@ -20,9 +21,39 @@ func TestTeam(t *testing.T) {
2021
require.NoError(t, err)
2122

2223
team := Team{}
23-
if tx := db.First(&team); tx.Error != nil {
24-
require.NoError(t, tx.Error)
24+
tx := db.First(&team)
25+
require.NoError(t, tx.Error)
26+
27+
teamToCreate := Team{
28+
ID: uuid.New(),
29+
Name: "foo-bar",
30+
Slug: "foobar",
31+
CreationTime: NewStringlyTime(time.Now()),
32+
MarkedDeleted: false,
2533
}
34+
tx = db.Create(&teamToCreate)
35+
require.NoError(t, tx.Error)
36+
}
37+
38+
func TestTeam_Create(t *testing.T) {
39+
pass := os.Getenv("MYSQL_DB_PASS")
40+
require.NotEmpty(t, pass)
41+
42+
db, err := Connect(ConnectionParams{
43+
User: "gitpod",
44+
Password: pass,
45+
Host: "tcp(127.0.0.1:3306)",
46+
Database: "gitpod",
47+
})
48+
require.NoError(t, err)
2649

27-
fmt.Println(team)
50+
team := Team{
51+
ID: uuid.New(),
52+
Name: "foo-bar",
53+
Slug: "foobar",
54+
CreationTime: NewStringlyTime(time.Now()),
55+
MarkedDeleted: false,
56+
}
57+
tx := db.Create(&team)
58+
require.NoError(t, tx.Error)
2859
}

components/gitpod-db-go/time.go

Lines changed: 0 additions & 51 deletions
This file was deleted.

components/gitpod-db-go/types.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package db
2+
3+
import (
4+
"database/sql/driver"
5+
"fmt"
6+
"time"
7+
)
8+
9+
func NewStringlyTime(t time.Time) StringlyTime {
10+
return StringlyTime(t)
11+
}
12+
13+
// StringlyTime exists for cases where records are inserted into the DB as VARCHAR but actually contain a timestamp which is time.RFC3339
14+
type StringlyTime time.Time
15+
16+
// Scan implements the Scanner interface.
17+
func (n StringlyTime) Scan(value interface{}) error {
18+
if value == nil {
19+
return fmt.Errorf("nil value")
20+
}
21+
22+
switch s := value.(type) {
23+
case []uint8:
24+
parsed, err := time.Parse(time.RFC3339, string(s))
25+
if err != nil {
26+
return fmt.Errorf("failed to parse %v: %w", value, err)
27+
}
28+
n = StringlyTime(parsed)
29+
}
30+
31+
return nil
32+
}
33+
34+
// Value implements the driver Valuer interface.
35+
func (n StringlyTime) Value() (driver.Value, error) {
36+
37+
return time.Time(n).UTC(), nil
38+
}
39+
40+
//func (n *StringlyTime) String() string {
41+
// return n.Time.Format(time.RFC3339)
42+
//}

0 commit comments

Comments
 (0)