-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[usage] Add db.WorkspaceInstance model in golang #10367
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// 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 WorkspaceInstance struct { | ||
ID uuid.UUID `gorm:"primary_key;column:id;type:char;size:36;" json:"id"` | ||
WorkspaceID string `gorm:"column:workspaceId;type:char;size:36;" json:"workspaceId"` | ||
Configuration datatypes.JSON `gorm:"column:configuration;type:text;size:65535;" json:"configuration"` | ||
Region string `gorm:"column:region;type:varchar;size:255;" json:"region"` | ||
ImageBuildInfo sql.NullString `gorm:"column:imageBuildInfo;type:text;size:65535;" json:"imageBuildInfo"` | ||
IdeURL string `gorm:"column:ideUrl;type:varchar;size:255;" json:"ideUrl"` | ||
WorkspaceBaseImage string `gorm:"column:workspaceBaseImage;type:varchar;size:255;" json:"workspaceBaseImage"` | ||
WorkspaceImage string `gorm:"column:workspaceImage;type:varchar;size:255;" json:"workspaceImage"` | ||
|
||
CreationTime VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"` | ||
StartedTime VarcharTime `gorm:"column:startedTime;type:varchar;size:255;" json:"startedTime"` | ||
DeployedTime VarcharTime `gorm:"column:deployedTime;type:varchar;size:255;" json:"deployedTime"` | ||
StoppedTime VarcharTime `gorm:"column:stoppedTime;type:varchar;size:255;" json:"stoppedTime"` | ||
LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"` | ||
StoppingTime VarcharTime `gorm:"column:stoppingTime;type:varchar;size:255;" json:"stoppingTime"` | ||
|
||
LastHeartbeat string `gorm:"column:lastHeartbeat;type:varchar;size:255;" json:"lastHeartbeat"` | ||
StatusOld sql.NullString `gorm:"column:status_old;type:varchar;size:255;" json:"status_old"` | ||
Status datatypes.JSON `gorm:"column:status;type:json;" json:"status"` | ||
Phase sql.NullString `gorm:"column:phase;type:char;size:32;" json:"phase"` | ||
PhasePersisted string `gorm:"column:phasePersisted;type:char;size:32;" json:"phasePersisted"` | ||
|
||
// deleted is restricted 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 *WorkspaceInstance) TableName() string { | ||
return "d_b_workspace_instance" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// 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 workspaceInstanceJSON = map[string]interface{}{ | ||
"id": "2f9a29bb-406e-4494-b592-c99f7fa0e793", | ||
"workspaceId": "gitpodio-ops-b8fqh3eylmr", | ||
"region": "eu03", | ||
"creationTime": "2022-05-31T15:22:57.192Z", | ||
"startedTime": "2022-05-31T15:24:04.336Z", | ||
"deployedTime": "", | ||
"stoppedTime": "2022-05-31T15:24:50.361Z", | ||
"lastHeartbeat": "", | ||
"ideUrl": "https://gitpodio-ops-b8fqh3eylmr.ws-eu03.gitpod-staging.com", | ||
"workspaceBaseImage": "", | ||
"workspaceImage": "eu.gcr.io/gitpod-dev/workspace-images:59e2c9722f56918c0e84f2640880fee0c19ac40471e968faaddcdcaebaf0df58", | ||
"status_old": nil, | ||
"_lastModified": "2022-05-31 15:24:50.373117", | ||
"status": "{\"repo\": {\"branch\": \"main\", \"latestCommit\": \"25d888e90dd21eb98be699c0b43556c153d667c9\", \"totalUntrackedFiles\": 0, \"totalUncommitedFiles\": 0, \"totalUnpushedCommits\": 0}, \"phase\": \"stopped\", \"nodeIp\": \"10.132.0.14\", \"message\": \"\", \"podName\": \"prebuild-2f9a29bb-406e-4494-b592-c99f7fa0e791\", \"timeout\": \"30m0s\", \"nodeName\": \"ws-eu03.gitpod-staging.com\", \"conditions\": {\"failed\": \"\", \"timeout\": \"\", \"deployed\": false, \"pullingImages\": false, \"stoppedByRequest\": false, \"headlessTaskFailed\": \"\"}, \"ownerToken\": \"4.MIXs2XurX1vMP7bTEryt9R9IbzGT_D\", \"exposedPorts\": []}", | ||
"phase": "stopped", | ||
"deleted": 0, | ||
"phasePersisted": "stopped", | ||
"configuration": "{\"ideImage\":\"eu.gcr.io/gitpod-core-dev/build/ide/code:commit-80d9b1ebfd826fd0db25320ba94d762b51887ada\",\"supervisorImage\":\"eu.gcr.io/gitpod-core-dev/build/supervisor:commit-1d4be665efdd1507f92b185afd112a7568d21a42\",\"ideConfig\":{\"useLatest\":false},\"featureFlags\":[\"registry_facade\",\"fixed_resources\"]}", | ||
"stoppingTime": "2022-05-31T15:24:39.657Z", | ||
"imageBuildInfo": nil, | ||
} | ||
|
||
func TestWorkspaceInstance_ReadExistingRecords(t *testing.T) { | ||
conn := db.ConnectForTests(t) | ||
id := insertRawWorkspaceInstance(t, conn, workspaceInstanceJSON) | ||
|
||
wsi := db.WorkspaceInstance{ID: id} | ||
tx := conn.First(&wsi) | ||
require.NoError(t, tx.Error) | ||
|
||
require.Equal(t, id, wsi.ID) | ||
|
||
require.True(t, wsi.StoppingTime.IsSet()) | ||
require.Equal(t, stringToVarchar(t, workspaceInstanceJSON["stoppingTime"].(string)), wsi.StoppingTime) | ||
|
||
require.True(t, wsi.StartedTime.IsSet()) | ||
require.Equal(t, stringToVarchar(t, workspaceInstanceJSON["startedTime"].(string)), wsi.StartedTime) | ||
|
||
require.False(t, wsi.DeployedTime.IsSet()) | ||
|
||
require.True(t, wsi.StoppedTime.IsSet()) | ||
require.Equal(t, stringToVarchar(t, workspaceInstanceJSON["stoppedTime"].(string)), wsi.StoppedTime) | ||
|
||
require.True(t, wsi.CreationTime.IsSet()) | ||
require.Equal(t, stringToVarchar(t, workspaceInstanceJSON["creationTime"].(string)), wsi.CreationTime) | ||
|
||
require.Equal(t, workspaceInstanceJSON["status"], wsi.Status.String()) | ||
require.Equal(t, workspaceInstanceJSON["configuration"], wsi.Configuration.String()) | ||
} | ||
|
||
func insertRawWorkspaceInstance(t *testing.T, conn *gorm.DB, object map[string]interface{}) uuid.UUID { | ||
columns := []string{"id", "workspaceId", "region", "creationTime", "startedTime", "deployedTime", "stoppedTime", "lastHeartbeat", "ideUrl", "workspaceBaseImage", "workspaceImage", "status_old", "_lastModified", "status", "deleted", "phasePersisted", "configuration", "stoppingTime", "imageBuildInfo"} | ||
statement := fmt.Sprintf(`INSERT INTO d_b_workspace_instance (%s) VALUES ?;`, strings.Join(columns, ", ")) | ||
id := uuid.MustParse(insertRawObject(t, conn, columns, statement, object)) | ||
|
||
t.Cleanup(func() { | ||
tx := conn.Delete(&db.WorkspaceInstance{ID: id}) | ||
require.NoError(t, tx.Error) | ||
}) | ||
|
||
return id | ||
} | ||
|
||
func insertRawObject(t *testing.T, conn *gorm.DB, columns []string, statement string, obj map[string]interface{}) string { | ||
t.Helper() | ||
|
||
id := obj["id"].(string) | ||
|
||
var values []interface{} | ||
for _, col := range columns { | ||
val, ok := obj[col] | ||
if !ok { | ||
values = append(values, "null") | ||
} else { | ||
values = append(values, val) | ||
} | ||
} | ||
|
||
tx := conn.Exec(statement, values) | ||
require.NoError(t, tx.Error) | ||
|
||
return id | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: one ID column is UUID, the other string: Why not make both strings? (so far we only have one column type for IDs: string) What's the advantange of UUID?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Workspace Instance ID is an actual UUID, but Workspace ID is something like
org-repo-random-string
which isn't a valid UUIDThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UUID allows you to parse UUIDs and validate before you even hit the DB. UUID is the more appropriate type as it's more specific. For example, it prevents you from inserting something like
Workspace{ID: "my-random-string"}
because it won't even compileThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jup. My point was that it's the only point in the code that imposes that constraint, then. The only constraint we have so far is "equality".
Mentioning this to a) raise awareness and b) to think about how we see this DB layer. So far I thought about it as 100% follower of the primary implementation we have.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And it is.
uuid.UUID
serializes as a string. It's the same as using the UUID_COLUMN type in our TypeORM implementation.https://github.com/gitpod-io/gitpod/blob/main/components/gitpod-db/src/typeorm/entity/db-workspace-instance.ts#L24
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah! The UUID_COLUMN_TYPE is only there to ensure the width/type of the field is the same everywhere.
The key question is: does
uuid.UUID
influence the runtime behavior (e.g., introduces new error modes during parsing? If no I'm happy! 👍There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn't, as long as what is stored in the DB is a valid UUID. I'm gonna run it against the RO replica of staging & prod to batch list all items to validate we don't have underlying violations to verify.