Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0cfd0a9

Browse files
committed
chore: InsertWorkspaceApp -> UpsertWorkspaceApp
Signed-off-by: Danny Kopping <[email protected]>
1 parent ba08d38 commit 0cfd0a9

File tree

12 files changed

+297
-161
lines changed

12 files changed

+297
-161
lines changed

coderd/agentapi/subagent.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import (
1212
"golang.org/x/xerrors"
1313

1414
"cdr.dev/slog"
15+
"github.com/coder/quartz"
16+
1517
agentproto "github.com/coder/coder/v2/agent/proto"
1618
"github.com/coder/coder/v2/coderd/database"
1719
"github.com/coder/coder/v2/coderd/database/dbauthz"
1820
"github.com/coder/coder/v2/codersdk"
1921
"github.com/coder/coder/v2/provisioner"
20-
"github.com/coder/quartz"
2122
)
2223

2324
type SubAgentAPI struct {
@@ -164,8 +165,8 @@ func (a *SubAgentAPI) CreateSubAgent(ctx context.Context, req *agentproto.Create
164165
}
165166
}
166167

167-
_, err := a.Database.InsertWorkspaceApp(ctx, database.InsertWorkspaceAppParams{
168-
ID: uuid.New(),
168+
_, err := a.Database.UpsertWorkspaceApp(ctx, database.UpsertWorkspaceAppParams{
169+
ID: uuid.New(), // TODO: we may need to maintain the app's ID here for stability, but for now we'll leave this as-is.
169170
CreatedAt: createdAt,
170171
AgentID: subAgent.ID,
171172
Slug: app.Slug,

coderd/database/dbauthz/dbauthz.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3938,23 +3938,6 @@ func (q *querier) InsertWorkspaceAgentStats(ctx context.Context, arg database.In
39383938
return q.db.InsertWorkspaceAgentStats(ctx, arg)
39393939
}
39403940

3941-
func (q *querier) InsertWorkspaceApp(ctx context.Context, arg database.InsertWorkspaceAppParams) (database.WorkspaceApp, error) {
3942-
// NOTE(DanielleMaywood):
3943-
// It is possible for there to exist an agent without a workspace.
3944-
// This means that we want to allow execution to continue if
3945-
// there isn't a workspace found to allow this behavior to continue.
3946-
workspace, err := q.db.GetWorkspaceByAgentID(ctx, arg.AgentID)
3947-
if err != nil && !errors.Is(err, sql.ErrNoRows) {
3948-
return database.WorkspaceApp{}, err
3949-
}
3950-
3951-
if err := q.authorizeContext(ctx, policy.ActionUpdate, workspace); err != nil {
3952-
return database.WorkspaceApp{}, err
3953-
}
3954-
3955-
return q.db.InsertWorkspaceApp(ctx, arg)
3956-
}
3957-
39583941
func (q *querier) InsertWorkspaceAppStats(ctx context.Context, arg database.InsertWorkspaceAppStatsParams) error {
39593942
if err := q.authorizeContext(ctx, policy.ActionCreate, rbac.ResourceSystem); err != nil {
39603943
return err
@@ -5181,6 +5164,23 @@ func (q *querier) UpsertWorkspaceAgentPortShare(ctx context.Context, arg databas
51815164
return q.db.UpsertWorkspaceAgentPortShare(ctx, arg)
51825165
}
51835166

5167+
func (q *querier) UpsertWorkspaceApp(ctx context.Context, arg database.UpsertWorkspaceAppParams) (database.WorkspaceApp, error) {
5168+
// NOTE(DanielleMaywood):
5169+
// It is possible for there to exist an agent without a workspace.
5170+
// This means that we want to allow execution to continue if
5171+
// there isn't a workspace found to allow this behavior to continue.
5172+
workspace, err := q.db.GetWorkspaceByAgentID(ctx, arg.AgentID)
5173+
if err != nil && !errors.Is(err, sql.ErrNoRows) {
5174+
return database.WorkspaceApp{}, err
5175+
}
5176+
5177+
if err := q.authorizeContext(ctx, policy.ActionUpdate, workspace); err != nil {
5178+
return database.WorkspaceApp{}, err
5179+
}
5180+
5181+
return q.db.UpsertWorkspaceApp(ctx, arg)
5182+
}
5183+
51845184
func (q *querier) UpsertWorkspaceAppAuditSession(ctx context.Context, arg database.UpsertWorkspaceAppAuditSessionParams) (bool, error) {
51855185
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceSystem); err != nil {
51865186
return false, err

coderd/database/dbauthz/dbauthz_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4114,7 +4114,7 @@ func (s *MethodTestSuite) TestSystemFunctions() {
41144114
APIKeyScope: database.AgentKeyScopeEnumAll,
41154115
}).Asserts(ws, policy.ActionCreateAgent)
41164116
}))
4117-
s.Run("InsertWorkspaceApp", s.Subtest(func(db database.Store, check *expects) {
4117+
s.Run("UpsertWorkspaceApp", s.Subtest(func(db database.Store, check *expects) {
41184118
_ = dbgen.User(s.T(), db, database.User{})
41194119
u := dbgen.User(s.T(), db, database.User{})
41204120
o := dbgen.Organization(s.T(), db, database.Organization{})
@@ -4130,7 +4130,7 @@ func (s *MethodTestSuite) TestSystemFunctions() {
41304130
_ = dbgen.WorkspaceBuild(s.T(), db, database.WorkspaceBuild{WorkspaceID: ws.ID, JobID: j.ID, TemplateVersionID: tv.ID})
41314131
res := dbgen.WorkspaceResource(s.T(), db, database.WorkspaceResource{JobID: j.ID})
41324132
agent := dbgen.WorkspaceAgent(s.T(), db, database.WorkspaceAgent{ResourceID: res.ID})
4133-
check.Args(database.InsertWorkspaceAppParams{
4133+
check.Args(database.UpsertWorkspaceAppParams{
41344134
ID: uuid.New(),
41354135
AgentID: agent.ID,
41364136
Health: database.WorkspaceAppHealthDisabled,

coderd/database/dbgen/dbgen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ func ProvisionerKey(t testing.TB, db database.Store, orig database.ProvisionerKe
778778
}
779779

780780
func WorkspaceApp(t testing.TB, db database.Store, orig database.WorkspaceApp) database.WorkspaceApp {
781-
resource, err := db.InsertWorkspaceApp(genCtx, database.InsertWorkspaceAppParams{
781+
resource, err := db.UpsertWorkspaceApp(genCtx, database.UpsertWorkspaceAppParams{
782782
ID: takeFirst(orig.ID, uuid.New()),
783783
CreatedAt: takeFirst(orig.CreatedAt, dbtime.Now()),
784784
AgentID: takeFirst(orig.AgentID, uuid.New()),

coderd/database/dbmem/dbmem.go

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10018,48 +10018,6 @@ func (q *FakeQuerier) InsertWorkspaceAgentStats(_ context.Context, arg database.
1001810018
return nil
1001910019
}
1002010020

10021-
func (q *FakeQuerier) InsertWorkspaceApp(_ context.Context, arg database.InsertWorkspaceAppParams) (database.WorkspaceApp, error) {
10022-
if err := validateDatabaseType(arg); err != nil {
10023-
return database.WorkspaceApp{}, err
10024-
}
10025-
10026-
q.mutex.Lock()
10027-
defer q.mutex.Unlock()
10028-
10029-
if arg.SharingLevel == "" {
10030-
arg.SharingLevel = database.AppSharingLevelOwner
10031-
}
10032-
10033-
if arg.OpenIn == "" {
10034-
arg.OpenIn = database.WorkspaceAppOpenInSlimWindow
10035-
}
10036-
10037-
// nolint:gosimple
10038-
workspaceApp := database.WorkspaceApp{
10039-
ID: arg.ID,
10040-
AgentID: arg.AgentID,
10041-
CreatedAt: arg.CreatedAt,
10042-
Slug: arg.Slug,
10043-
DisplayName: arg.DisplayName,
10044-
Icon: arg.Icon,
10045-
Command: arg.Command,
10046-
Url: arg.Url,
10047-
External: arg.External,
10048-
Subdomain: arg.Subdomain,
10049-
SharingLevel: arg.SharingLevel,
10050-
HealthcheckUrl: arg.HealthcheckUrl,
10051-
HealthcheckInterval: arg.HealthcheckInterval,
10052-
HealthcheckThreshold: arg.HealthcheckThreshold,
10053-
Health: arg.Health,
10054-
Hidden: arg.Hidden,
10055-
DisplayOrder: arg.DisplayOrder,
10056-
OpenIn: arg.OpenIn,
10057-
DisplayGroup: arg.DisplayGroup,
10058-
}
10059-
q.workspaceApps = append(q.workspaceApps, workspaceApp)
10060-
return workspaceApp, nil
10061-
}
10062-
1006310021
func (q *FakeQuerier) InsertWorkspaceAppStats(_ context.Context, arg database.InsertWorkspaceAppStatsParams) error {
1006410022
err := validateDatabaseType(arg)
1006510023
if err != nil {
@@ -13192,6 +13150,58 @@ func (q *FakeQuerier) UpsertWorkspaceAgentPortShare(_ context.Context, arg datab
1319213150
return psl, nil
1319313151
}
1319413152

13153+
func (q *FakeQuerier) UpsertWorkspaceApp(ctx context.Context, arg database.UpsertWorkspaceAppParams) (database.WorkspaceApp, error) {
13154+
err := validateDatabaseType(arg)
13155+
if err != nil {
13156+
return database.WorkspaceApp{}, err
13157+
}
13158+
13159+
q.mutex.Lock()
13160+
defer q.mutex.Unlock()
13161+
13162+
if arg.SharingLevel == "" {
13163+
arg.SharingLevel = database.AppSharingLevelOwner
13164+
}
13165+
if arg.OpenIn == "" {
13166+
arg.OpenIn = database.WorkspaceAppOpenInSlimWindow
13167+
}
13168+
13169+
buildApp := func(id uuid.UUID, createdAt time.Time) database.WorkspaceApp {
13170+
return database.WorkspaceApp{
13171+
ID: id,
13172+
CreatedAt: createdAt,
13173+
AgentID: arg.AgentID,
13174+
Slug: arg.Slug,
13175+
DisplayName: arg.DisplayName,
13176+
Icon: arg.Icon,
13177+
Command: arg.Command,
13178+
Url: arg.Url,
13179+
External: arg.External,
13180+
Subdomain: arg.Subdomain,
13181+
SharingLevel: arg.SharingLevel,
13182+
HealthcheckUrl: arg.HealthcheckUrl,
13183+
HealthcheckInterval: arg.HealthcheckInterval,
13184+
HealthcheckThreshold: arg.HealthcheckThreshold,
13185+
Health: arg.Health,
13186+
Hidden: arg.Hidden,
13187+
DisplayOrder: arg.DisplayOrder,
13188+
OpenIn: arg.OpenIn,
13189+
DisplayGroup: arg.DisplayGroup,
13190+
}
13191+
}
13192+
13193+
for i, app := range q.workspaceApps {
13194+
if app.ID == arg.ID {
13195+
q.workspaceApps[i] = buildApp(app.ID, app.CreatedAt)
13196+
return q.workspaceApps[i], nil
13197+
}
13198+
}
13199+
13200+
workspaceApp := buildApp(arg.ID, arg.CreatedAt)
13201+
q.workspaceApps = append(q.workspaceApps, workspaceApp)
13202+
return workspaceApp, nil
13203+
}
13204+
1319513205
func (q *FakeQuerier) UpsertWorkspaceAppAuditSession(_ context.Context, arg database.UpsertWorkspaceAppAuditSessionParams) (bool, error) {
1319613206
err := validateDatabaseType(arg)
1319713207
if err != nil {

coderd/database/dbmetrics/querymetrics.go

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbmock/dbmock.go

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)