Skip to content

Commit 478187f

Browse files
committed
chore: InsertWorkspaceApp -> UpsertWorkspaceApp
Signed-off-by: Danny Kopping <dannykopping@gmail.com>
1 parent ba08d38 commit 478187f

File tree

12 files changed

+314
-160
lines changed

12 files changed

+314
-160
lines changed

coderd/agentapi/subagent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func (a *SubAgentAPI) CreateSubAgent(ctx context.Context, req *agentproto.Create
164164
}
165165
}
166166

167-
_, err := a.Database.InsertWorkspaceApp(ctx, database.InsertWorkspaceAppParams{
167+
_, err := a.Database.UpsertWorkspaceApp(ctx, database.UpsertWorkspaceAppParams{
168168
ID: uuid.New(),
169169
CreatedAt: createdAt,
170170
AgentID: subAgent.ID,

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: 72 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,78 @@ 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+
13166+
if arg.OpenIn == "" {
13167+
arg.OpenIn = database.WorkspaceAppOpenInSlimWindow
13168+
}
13169+
13170+
// Check if app exists (by agent_id and slug for ON CONFLICT clause)
13171+
for i, app := range q.workspaceApps {
13172+
if app.AgentID == arg.AgentID && app.Slug == arg.Slug {
13173+
// Update existing app
13174+
q.workspaceApps[i] = database.WorkspaceApp{
13175+
ID: app.ID, // Keep original ID
13176+
AgentID: arg.AgentID,
13177+
CreatedAt: app.CreatedAt, // Keep original created time
13178+
Slug: arg.Slug,
13179+
DisplayName: arg.DisplayName,
13180+
Icon: arg.Icon,
13181+
Command: arg.Command,
13182+
Url: arg.Url,
13183+
External: arg.External,
13184+
Subdomain: arg.Subdomain,
13185+
SharingLevel: arg.SharingLevel,
13186+
HealthcheckUrl: arg.HealthcheckUrl,
13187+
HealthcheckInterval: arg.HealthcheckInterval,
13188+
HealthcheckThreshold: arg.HealthcheckThreshold,
13189+
Health: arg.Health,
13190+
Hidden: arg.Hidden,
13191+
DisplayOrder: arg.DisplayOrder,
13192+
OpenIn: arg.OpenIn,
13193+
DisplayGroup: arg.DisplayGroup,
13194+
}
13195+
return q.workspaceApps[i], nil
13196+
}
13197+
}
13198+
13199+
// Insert new app
13200+
workspaceApp := database.WorkspaceApp{
13201+
ID: arg.ID,
13202+
AgentID: arg.AgentID,
13203+
CreatedAt: arg.CreatedAt,
13204+
Slug: arg.Slug,
13205+
DisplayName: arg.DisplayName,
13206+
Icon: arg.Icon,
13207+
Command: arg.Command,
13208+
Url: arg.Url,
13209+
External: arg.External,
13210+
Subdomain: arg.Subdomain,
13211+
SharingLevel: arg.SharingLevel,
13212+
HealthcheckUrl: arg.HealthcheckUrl,
13213+
HealthcheckInterval: arg.HealthcheckInterval,
13214+
HealthcheckThreshold: arg.HealthcheckThreshold,
13215+
Health: arg.Health,
13216+
Hidden: arg.Hidden,
13217+
DisplayOrder: arg.DisplayOrder,
13218+
OpenIn: arg.OpenIn,
13219+
DisplayGroup: arg.DisplayGroup,
13220+
}
13221+
q.workspaceApps = append(q.workspaceApps, workspaceApp)
13222+
return workspaceApp, nil
13223+
}
13224+
1319513225
func (q *FakeQuerier) UpsertWorkspaceAppAuditSession(_ context.Context, arg database.UpsertWorkspaceAppAuditSessionParams) (bool, error) {
1319613226
err := validateDatabaseType(arg)
1319713227
if err != nil {

coderd/database/dbmetrics/querymetrics.go

Lines changed: 7 additions & 8 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)