Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/go/workspace.go
2498 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package db
6
7
import (
8
"context"
9
"database/sql"
10
"fmt"
11
"math"
12
"time"
13
14
"github.com/google/uuid"
15
"gorm.io/datatypes"
16
"gorm.io/gorm"
17
)
18
19
// Workspace represents the underlying DB object
20
type Workspace struct {
21
ID string `gorm:"primary_key;column:id;type:char;size:36;" json:"id"`
22
OrganizationId *uuid.UUID `gorm:"column:organizationId;type:char;size:36;" json:"organizationId"`
23
OwnerID uuid.UUID `gorm:"column:ownerId;type:char;size:36;" json:"ownerId"`
24
ProjectID sql.NullString `gorm:"column:projectId;type:char;size:36;" json:"projectId"`
25
Description string `gorm:"column:description;type:varchar;size:255;" json:"description"`
26
Type WorkspaceType `gorm:"column:type;type:char;size:16;default:regular;" json:"type"`
27
CloneURL string `gorm:"column:cloneURL;type:varchar;size:255;" json:"cloneURL"`
28
29
ContextURL string `gorm:"column:contextURL;type:text;size:65535;" json:"contextURL"`
30
Context datatypes.JSON `gorm:"column:context;type:text;size:65535;" json:"context"`
31
Config datatypes.JSON `gorm:"column:config;type:text;size:65535;" json:"config"`
32
BasedOnPrebuildID sql.NullString `gorm:"column:basedOnPrebuildId;type:char;size:36;" json:"basedOnPrebuildId"`
33
BasedOnSnapshotID sql.NullString `gorm:"column:basedOnSnapshotId;type:char;size:36;" json:"basedOnSnapshotId"`
34
ImageSource datatypes.JSON `gorm:"column:imageSource;type:text;size:65535;" json:"imageSource"`
35
ImageNameResolved string `gorm:"column:imageNameResolved;type:varchar;size:255;" json:"imageNameResolved"`
36
BaseImageNameResolved string `gorm:"column:baseImageNameResolved;type:varchar;size:255;" json:"baseImageNameResolved"`
37
38
CreationTime VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"`
39
LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"`
40
SoftDeletedTime VarcharTime `gorm:"column:softDeletedTime;type:varchar;size:255;" json:"softDeletedTime"`
41
ContentDeletedTime VarcharTime `gorm:"column:contentDeletedTime;type:varchar;size:255;" json:"contentDeletedTime"`
42
43
Archived bool `gorm:"column:archived;type:tinyint;default:0;" json:"archived"`
44
Shareable bool `gorm:"column:shareable;type:tinyint;default:0;" json:"shareable"`
45
46
SoftDeleted sql.NullString `gorm:"column:softDeleted;type:char;size:4;" json:"softDeleted"`
47
Pinned bool `gorm:"column:pinned;type:tinyint;default:0;" json:"pinned"`
48
49
// deleted is reserved for use by periodic deleter
50
_ int32 `gorm:"column:deleted;type:tinyint;default:0;" json:"deleted"`
51
}
52
53
func (d *Workspace) TableName() string {
54
return "d_b_workspace"
55
}
56
57
type WorkspaceType string
58
59
const (
60
WorkspaceType_Prebuild WorkspaceType = "prebuild"
61
WorkspaceType_Probe WorkspaceType = "probe"
62
WorkspaceType_Regular WorkspaceType = "regular"
63
)
64
65
const maxListBatchSize = 65535 // 2^16 - 1
66
67
func ListWorkspacesByID(ctx context.Context, conn *gorm.DB, ids []string) ([]Workspace, error) {
68
if len(ids) == 0 {
69
return nil, nil
70
}
71
72
var workspaces []Workspace
73
74
items := len(ids)
75
batches := int(math.Ceil(float64(items) / maxListBatchSize))
76
for i := 0; i < batches; i++ {
77
lower := i * maxListBatchSize
78
upper := (i + 1) * maxListBatchSize
79
80
if upper > items {
81
upper = items
82
}
83
84
batchIDs := ids[lower:upper]
85
var results []Workspace
86
tx := conn.WithContext(ctx).Where(batchIDs).Find(&results)
87
if tx.Error != nil {
88
return nil, fmt.Errorf("failed to list workspaces by id: %w", tx.Error)
89
}
90
91
workspaces = append(workspaces, results...)
92
}
93
94
return workspaces, nil
95
}
96
97