-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[usage] Add db.Project model in golang #10368
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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 ( | ||
"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;" json:"id"` | ||
Name string `gorm:"column:name;type:varchar;size:255;" json:"name"` | ||
CloneURL string `gorm:"column:cloneUrl;type:varchar;size:255;" json:"cloneUrl"` | ||
Slug sql.NullString `gorm:"column:slug;type:varchar;size:255;" json:"slug"` | ||
Settings datatypes.JSON `gorm:"column:settings;type:text;size:65535;" json:"settings"` | ||
|
||
AppInstallationID string `gorm:"column:appInstallationId;type:varchar;size:255;" json:"appInstallationId"` | ||
|
||
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"` | ||
|
||
TeamID sql.NullString `gorm:"column:teamId;type:char;size:36;" json:"teamId"` | ||
UserID sql.NullString `gorm:"column:userId;type:char;size:36;" json:"userId"` | ||
|
||
MarkedDeleted bool `gorm:"column:markedDeleted;type:tinyint;default:0;" json:"markedDeleted"` | ||
|
||
// deleted 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 *Project) TableName() string { | ||
return "d_b_project" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// 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 projectJSON = map[string]interface{}{ | ||
"id": "49b5d309-bb95-4a79-846c-e3cf42c0d591", | ||
"cloneUrl": "https://github.com/gptest1/gptest1-repo1-private.git", | ||
"teamId": "0e433063-1358-4892-9ed2-68e273d17d07", | ||
"appInstallationId": "20446411", | ||
"creationTime": "2021-11-01T19:36:07.532Z", | ||
"deleted": 0, | ||
"_lastModified": "2021-11-02 10:49:12.473658", | ||
"name": "gptest1-repo1-private", | ||
"config": "{\".gitpod.yml\":\"tasks:\\n - init: |\\n echo 'TODO: build project'\\n command: |\\n echo 'TODO: start app'\"}", | ||
jankeromnes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"markedDeleted": 1, | ||
"userId": nil, | ||
"slug": "gptest1-repo1-private", | ||
"settings": nil, | ||
} | ||
|
||
func TestProject_ReadExistingRecords(t *testing.T) { | ||
conn := db.ConnectForTests(t) | ||
|
||
id := insertRawProject(t, conn, projectJSON) | ||
|
||
project := db.Project{ID: id} | ||
tx := conn.First(&project) | ||
require.NoError(t, tx.Error) | ||
|
||
require.Equal(t, id, project.ID) | ||
require.Equal(t, projectJSON["teamId"], project.TeamID.String) | ||
require.Equal(t, stringToVarchar(t, "2021-11-01T19:36:07.532Z"), project.CreationTime) | ||
} | ||
|
||
func insertRawProject(t *testing.T, conn *gorm.DB, obj map[string]interface{}) uuid.UUID { | ||
columns := []string{ | ||
"id", "cloneUrl", "teamId", "appInstallationId", "creationTime", "deleted", "_lastModified", "name", "config", "markedDeleted", "userId", "slug", "settings", | ||
} | ||
statement := fmt.Sprintf(`INSERT INTO d_b_project (%s) VALUES ?;`, strings.Join(columns, ", ")) | ||
id := uuid.MustParse(obj["id"].(string)) | ||
|
||
var values []interface{} | ||
for _, col := range columns { | ||
val, ok := obj[col] | ||
if !ok { | ||
values = append(values, "null") | ||
jankeromnes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
values = append(values, val) | ||
} | ||
} | ||
|
||
tx := conn.Exec(statement, values) | ||
require.NoError(t, tx.Error) | ||
|
||
t.Cleanup(func() { | ||
tx = conn.Delete(&db.Project{ID: id}) | ||
require.NoError(t, tx.Error) | ||
}) | ||
|
||
return id | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -33,11 +33,11 @@ type Workspace struct { | |||||
SoftDeletedTime VarcharTime `gorm:"column:softDeletedTime;type:varchar;size:255;" json:"softDeletedTime"` | ||||||
ContentDeletedTime VarcharTime `gorm:"column:contentDeletedTime;type:varchar;size:255;" json:"contentDeletedTime"` | ||||||
|
||||||
Archived int32 `gorm:"column:archived;type:tinyint;default:0;" json:"archived"` | ||||||
Shareable int32 `gorm:"column:shareable;type:tinyint;default:0;" json:"shareable"` | ||||||
Archived bool `gorm:"column:archived;type:tinyint;default:0;" json:"archived"` | ||||||
Shareable bool `gorm:"column:shareable;type:tinyint;default:0;" json:"shareable"` | ||||||
|
||||||
SoftDeleted sql.NullString `gorm:"column:softDeleted;type:char;size:4;" json:"softDeleted"` | ||||||
Pinned int32 `gorm:"column:pinned;type:tinyint;default:0;" json:"pinned"` | ||||||
Pinned bool `gorm:"column:pinned;type:tinyint;default:0;" json:"pinned"` | ||||||
|
||||||
// deleted is reserved for use by db-sync | ||||||
_ int32 `gorm:"column:deleted;type:tinyint;default:0;" json:"deleted"` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also,
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good spot. I'd like to leave this part for later PRs once we start using the data from the model and come across this. This will guide us exactly how we want to encapsulated the enum model for this. |
||||||
|
Uh oh!
There was an error while loading. Please reload this page.