mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: upsert coder_app resources in case they are persistent (#18509)
This commit is contained in:
@@ -3938,23 +3938,6 @@ func (q *querier) InsertWorkspaceAgentStats(ctx context.Context, arg database.In
|
||||
return q.db.InsertWorkspaceAgentStats(ctx, arg)
|
||||
}
|
||||
|
||||
func (q *querier) InsertWorkspaceApp(ctx context.Context, arg database.InsertWorkspaceAppParams) (database.WorkspaceApp, error) {
|
||||
// NOTE(DanielleMaywood):
|
||||
// It is possible for there to exist an agent without a workspace.
|
||||
// This means that we want to allow execution to continue if
|
||||
// there isn't a workspace found to allow this behavior to continue.
|
||||
workspace, err := q.db.GetWorkspaceByAgentID(ctx, arg.AgentID)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return database.WorkspaceApp{}, err
|
||||
}
|
||||
|
||||
if err := q.authorizeContext(ctx, policy.ActionUpdate, workspace); err != nil {
|
||||
return database.WorkspaceApp{}, err
|
||||
}
|
||||
|
||||
return q.db.InsertWorkspaceApp(ctx, arg)
|
||||
}
|
||||
|
||||
func (q *querier) InsertWorkspaceAppStats(ctx context.Context, arg database.InsertWorkspaceAppStatsParams) error {
|
||||
if err := q.authorizeContext(ctx, policy.ActionCreate, rbac.ResourceSystem); err != nil {
|
||||
return err
|
||||
@@ -5181,6 +5164,23 @@ func (q *querier) UpsertWorkspaceAgentPortShare(ctx context.Context, arg databas
|
||||
return q.db.UpsertWorkspaceAgentPortShare(ctx, arg)
|
||||
}
|
||||
|
||||
func (q *querier) UpsertWorkspaceApp(ctx context.Context, arg database.UpsertWorkspaceAppParams) (database.WorkspaceApp, error) {
|
||||
// NOTE(DanielleMaywood):
|
||||
// It is possible for there to exist an agent without a workspace.
|
||||
// This means that we want to allow execution to continue if
|
||||
// there isn't a workspace found to allow this behavior to continue.
|
||||
workspace, err := q.db.GetWorkspaceByAgentID(ctx, arg.AgentID)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return database.WorkspaceApp{}, err
|
||||
}
|
||||
|
||||
if err := q.authorizeContext(ctx, policy.ActionUpdate, workspace); err != nil {
|
||||
return database.WorkspaceApp{}, err
|
||||
}
|
||||
|
||||
return q.db.UpsertWorkspaceApp(ctx, arg)
|
||||
}
|
||||
|
||||
func (q *querier) UpsertWorkspaceAppAuditSession(ctx context.Context, arg database.UpsertWorkspaceAppAuditSessionParams) (bool, error) {
|
||||
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceSystem); err != nil {
|
||||
return false, err
|
||||
|
||||
@@ -4114,7 +4114,7 @@ func (s *MethodTestSuite) TestSystemFunctions() {
|
||||
APIKeyScope: database.AgentKeyScopeEnumAll,
|
||||
}).Asserts(ws, policy.ActionCreateAgent)
|
||||
}))
|
||||
s.Run("InsertWorkspaceApp", s.Subtest(func(db database.Store, check *expects) {
|
||||
s.Run("UpsertWorkspaceApp", s.Subtest(func(db database.Store, check *expects) {
|
||||
_ = dbgen.User(s.T(), db, database.User{})
|
||||
u := dbgen.User(s.T(), db, database.User{})
|
||||
o := dbgen.Organization(s.T(), db, database.Organization{})
|
||||
@@ -4130,7 +4130,7 @@ func (s *MethodTestSuite) TestSystemFunctions() {
|
||||
_ = dbgen.WorkspaceBuild(s.T(), db, database.WorkspaceBuild{WorkspaceID: ws.ID, JobID: j.ID, TemplateVersionID: tv.ID})
|
||||
res := dbgen.WorkspaceResource(s.T(), db, database.WorkspaceResource{JobID: j.ID})
|
||||
agent := dbgen.WorkspaceAgent(s.T(), db, database.WorkspaceAgent{ResourceID: res.ID})
|
||||
check.Args(database.InsertWorkspaceAppParams{
|
||||
check.Args(database.UpsertWorkspaceAppParams{
|
||||
ID: uuid.New(),
|
||||
AgentID: agent.ID,
|
||||
Health: database.WorkspaceAppHealthDisabled,
|
||||
|
||||
Reference in New Issue
Block a user