File tree Expand file tree Collapse file tree 7 files changed +113
-67
lines changed Expand file tree Collapse file tree 7 files changed +113
-67
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ module github.com/gitpod-io/gitpod/gitpod-db
3
3
go 1.17
4
4
5
5
require (
6
+ github.com/google/uuid v1.3.0
6
7
github.com/stretchr/testify v1.7.1
7
8
gorm.io/driver/mysql v1.3.2
8
9
gorm.io/gorm v1.23.3
Original file line number Diff line number Diff line change @@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
2
2
github.com/davecgh/go-spew v1.1.0 /go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38 =
3
3
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE =
4
4
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 =
5
7
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E =
6
8
github.com/jinzhu/inflection v1.0.0 /go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc =
7
9
github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas =
Original file line number Diff line number Diff line change @@ -2,14 +2,15 @@ package db
2
2
3
3
import (
4
4
"context"
5
+ "github.com/google/uuid"
5
6
)
6
7
7
8
type Team struct {
8
- ID string `gorm:"primaryKey"`
9
+ ID uuid. UUID `gorm:"primaryKey"`
9
10
Name string
10
11
Slug string
11
12
CreationTime StringlyTime `gorm:"column:creationTime"`
12
- MarkedDeleted bool
13
+ MarkedDeleted bool `gorm:"column:markedDeleted"`
13
14
}
14
15
15
16
// TableName overrides default GORM handling of table name generation
Original file line number Diff line number Diff line change 1
1
package db
2
2
3
3
import (
4
- "fmt "
4
+ "github.com/google/uuid "
5
5
"github.com/stretchr/testify/require"
6
6
"os"
7
7
"testing"
8
+ "time"
8
9
)
9
10
10
11
func TestTeam (t * testing.T ) {
@@ -20,9 +21,39 @@ func TestTeam(t *testing.T) {
20
21
require .NoError (t , err )
21
22
22
23
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 ,
25
33
}
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 )
26
49
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 )
28
59
}
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
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
+ //}
You can’t perform that action at this time.
0 commit comments