Skip to content

WIP: Implement db model in go #8967

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

Closed
wants to merge 10 commits into from
Closed
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
37 changes: 37 additions & 0 deletions components/gitpod-db-go/conn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package db

import (
"fmt"
driver_mysql "github.com/go-sql-driver/mysql"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"time"
)

type ConnectionParams struct {
User string
Password string
Host string
Database string
}

func Connect(p ConnectionParams) (*gorm.DB, error) {
loc, _ := time.LoadLocation("UTC")
cfg := driver_mysql.Config{
User: p.User,
Passwd: p.Password,
Net: "tcp",
Addr: p.Host,
DBName: p.Database,
Loc: loc,
AllowNativePasswords: true,
ParseTime: true,
}

dsn := cfg.FormatDSN()
fmt.Println(dsn)

// refer https://github.com/go-sql-driver/mysql#dsn-data-source-name for details
//dsn := fmt.Sprintf("%s:%s@%s/%s?charset=utf8mb4&parseTime=True&loc=Local", p.User, p.Password, p.Host, p.Database)
return gorm.Open(mysql.Open(dsn), &gorm.Config{})
}
21 changes: 21 additions & 0 deletions components/gitpod-db-go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module github.com/gitpod-io/gitpod/gitpod-db

go 1.17

require (
github.com/go-sql-driver/mysql v1.6.0
github.com/google/uuid v1.3.0
github.com/relvacode/iso8601 v1.1.0
github.com/stretchr/testify v1.7.1
gorm.io/datatypes v1.0.6
gorm.io/driver/mysql v1.3.2
gorm.io/gorm v1.23.3
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
229 changes: 229 additions & 0 deletions components/gitpod-db-go/go.sum

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions components/gitpod-db-go/prebuild.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package db

import (
"database/sql"
"github.com/google/uuid"
"time"
)

type Prebuild struct {
ID uuid.UUID `gorm:"primary_key;column:id;type:char;size:36;" json:"id"`
CloneURL string `gorm:"column:cloneURL;type:varchar;size:255;" json:"clone_url"`
Commit string `gorm:"column:commit;type:varchar;size:255;" json:"commit"`
State string `gorm:"column:state;type:varchar;size:255;" json:"state"`
BuildWorkspaceID string `gorm:"column:buildWorkspaceId;type:char;size:36;" json:"build_workspace_id"`
Snapshot string `gorm:"column:snapshot;type:varchar;size:255;" json:"snapshot"`
Error string `gorm:"column:error;type:varchar;size:255;" json:"error"`
ProjectID sql.NullString `gorm:"column:projectId;type:char;size:36;" json:"project_id"`
Branch sql.NullString `gorm:"column:branch;type:varchar;size:255;" json:"branch"`
StatusVersion int64 `gorm:"column:statusVersion;type:bigint;default:0;" json:"status_version"`

CreationTime time.Time `gorm:"column:creationTime;type:timestamp;default:CURRENT_TIMESTAMP(6);->;" json:"creation_time"`
LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);->;" json:"_last_modified"`
}

// TableName sets the insert table name for this struct type
func (d *Prebuild) TableName() string {
return "d_b_prebuilt_workspace"
}
24 changes: 24 additions & 0 deletions components/gitpod-db-go/prebuild_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package db

import (
"fmt"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"testing"
)

func TestPrebuildRead(t *testing.T) {
db := getPreviewDB(t)

prebuild := Prebuild{}
tx := db.First(&prebuild, uuid.MustParse("27836aaa-acb7-45a8-82d0-924c18d8aa26"))
require.NoError(t, tx.Error)

// +--------------------------------------+--------------------------------------------------------+------------------------------------------+-----------+----------------------------+--------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+-------+----------------------------+--------------------------------------+--------+-----------------+
//| id | cloneURL | commit | state | creationTime | buildWorkspaceId | snapshot | error | _lastModified | projectId | branch | statusVersion |
//+--------------------------------------+--------------------------------------------------------+------------------------------------------+-----------+----------------------------+--------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+-------+----------------------------+--------------------------------------+--------+-----------------+
//| 27836aaa-acb7-45a8-82d0-924c18d8aa26 | https://gitlab.com/gitpod-milan/gitpod-large-image.git | d741ef690a211f5a4157f3edc685c984b3b4b1d1 | available | 2022-04-10 14:30:55.170277 | gitpodmilan-gitpodlargei-zmghwy9q41t | workspaces/gitpodmilan-gitpodlargei-zmghwy9q41t/snapshot-1649601374866319750.tar@gitpod-user-1a8ce801-b849-43e2-8c1c-d19473976e55 | | 2022-04-10 14:36:14.926379 | 5b9599ea-235c-4659-80ff-44cc944d7fad | main | 108108275646469 |
//+--------------------------------------+--------------------------------------------------------+------------------------------------------+-----------+----------------------------+--------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+-------+----------------------------+--------------------------------------+--------+-----------------+

fmt.Print(prebuild)
}
30 changes: 30 additions & 0 deletions components/gitpod-db-go/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package db

import (
"database/sql"
"github.com/google/uuid"
"gorm.io/datatypes"
"time"
)

type Project struct {
ID uuid.UUID `gorm:"primary_key;column:id;type:char;size:36;"`
Name string `gorm:"column:name;type:varchar;size:255;"`
CloneURL string `gorm:"column:cloneUrl;type:varchar;size:255;"`
TeamID sql.NullString `gorm:"column:teamId;type:char;size:36;"`
AppInstallationID string `gorm:"column:appInstallationId;type:varchar;size:255;"`
CreationTime VarcharTime `gorm:"column:creationTime;type:varchar;size:255;"`
Deleted int32 `gorm:"column:deleted;type:tinyint;default:0;"`
LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);"`
Config *datatypes.JSON `gorm:"column:config;type:text;size:65535;"`
UserID sql.NullString `gorm:"column:userId;type:char;size:36;"`
Slug sql.NullString `gorm:"column:slug;type:varchar;size:255;"`
Settings *datatypes.JSON `gorm:"column:settings;type:text;size:65535;"`

_ int32 `gorm:"column:markedDeleted;type:tinyint;default:0;"`
}

// TableName sets the insert table name for this struct type
func (d *Project) TableName() string {
return "d_b_project"
}
21 changes: 21 additions & 0 deletions components/gitpod-db-go/project_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package db

import (
"fmt"
"github.com/stretchr/testify/require"
"testing"
)

func TestListAllProject(t *testing.T) {
db := getPreviewDB(t)

rows, err := db.Model(&Project{}).Rows()
require.NoError(t, err)

for rows.Next() {
var project Project
require.NoError(t, db.ScanRows(rows, &project))

fmt.Println(project)
}
}
38 changes: 38 additions & 0 deletions components/gitpod-db-go/team.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package db

import (
"context"
"github.com/google/uuid"
"time"
)

type Team struct {
ID uuid.UUID `gorm:"primary_key;column:id;type:char;size:36;"`
Name string `gorm:"column:name;type:varchar;size:255;"`
Slug string `gorm:"column:slug;type:varchar;size:255;"`
CreationTime VarcharTime `gorm:"column:creationTime;type:varchar;size:255;"`
Deleted int32 `gorm:"column:deleted;type:tinyint;default:0;"`
LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);"`
_ int32 `gorm:"column:markedDeleted;type:tinyint;default:0;"`
}

// TableName overrides default GORM handling of table name generation
func (t *Team) TableName() string {
return "d_b_team"
}

type ListOpts struct {
OrderBy string
SearchTerm string
}

type TeamRepository interface {
List(ctx context.Context, opts ListOpts) ([]*Team, error)

Get(ctx context.Context, id string) (*Team, error)
GetByUser(ctx context.Context, userID string) (*Team, error)

Create(ctx context.Context, team Team) (*Team, error)

Delete(ctx context.Context, id string) (*Team, error)
}
55 changes: 55 additions & 0 deletions components/gitpod-db-go/team_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package db

import (
"encoding/base64"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
"os/exec"
"testing"
"time"
)

func TestCreateAndRead(t *testing.T) {
db := getPreviewDB(t)

teamToCreate := Team{
ID: uuid.New(),
Name: "foo-bar",
Slug: "foobar",
CreationTime: NewVarcharTime(time.Now()),
}
tx := db.Debug().Create(&teamToCreate)
require.NoError(t, tx.Error)

retrieved := Team{}
tx = db.Debug().First(&retrieved, teamToCreate.ID)
require.NoError(t, tx.Error)

require.Equal(t, teamToCreate.CreationTime, retrieved.CreationTime)
}

func getPreviewEnvDBPass(t *testing.T) string {
cmd := `kubectl get secret mysql -o json | jq -r '.data["password"]'`
out, err := exec.Command("/bin/bash", "-c", cmd).Output()
require.NoError(t, err, "must retrieve db password")

decoded, err := base64.StdEncoding.DecodeString(string(out))
require.NoError(t, err)

return string(decoded)
}

func getPreviewDB(t *testing.T) *gorm.DB {
pass := getPreviewEnvDBPass(t)

db, err := Connect(ConnectionParams{
User: "gitpod",
Password: pass,
Host: "127.0.0.1:3306",
Database: "gitpod",
})
require.NoError(t, err)

return db
}
41 changes: 41 additions & 0 deletions components/gitpod-db-go/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package db

import (
"database/sql/driver"
"fmt"
"github.com/relvacode/iso8601"
"time"
)

func NewVarcharTime(t time.Time) VarcharTime {
return VarcharTime(t.UTC())
}

// VarcharTime exists for cases where records are inserted into the DB as VARCHAR but actually contain a timestamp which is time.RFC3339
type VarcharTime time.Time

// Scan implements the Scanner interface.
func (n *VarcharTime) Scan(value interface{}) error {
if value == nil {
return fmt.Errorf("nil value")
}

switch s := value.(type) {
case []uint8:
parsed, err := iso8601.ParseString(string(s))
if err != nil {
return fmt.Errorf("failed to parse %v into ISO8601: %w", string(s), err)
}
*n = VarcharTime(parsed.UTC())
}
return fmt.Errorf("unknown scan value for VarcharTime with value: %v", value)
}

// Value implements the driver Valuer interface.
func (n VarcharTime) Value() (driver.Value, error) {
return time.Time(n).UTC().Format(time.RFC3339Nano), nil
}

func (n VarcharTime) String() string {
return time.Time(n).Format(time.RFC3339Nano)
}