MCPcopy Index your code
hub / github.com/coder/coder / getDatabase

Method getDatabase

coderd/workspaceapps/request.go:218–434  ·  view source on GitHub ↗

getDatabase does queries to get the owner user, workspace and agent associated with the app in the request. This will correctly perform the queries in the correct order based on the access method and what fields are available. If any of the queries don't return any rows, the error will wrap sql.Err

(ctx context.Context, db database.Store)

Source from the content-addressed store, hash-verified

216// If any of the queries don't return any rows, the error will wrap
217// sql.ErrNoRows. All other errors should be considered internal server errors.
218func (r Request) getDatabase(ctx context.Context, db database.Store) (*databaseRequest, error) {
219 // If the AccessMethod is AccessMethodTerminal, then we need to get the
220 // agent first since that's the only info we have.
221 if r.AccessMethod == AccessMethodTerminal {
222 return r.getDatabaseTerminal(ctx, db)
223 }
224
225 // For non-terminal requests, get the objects in order since we have all
226 // fields available.
227
228 // Get user.
229 var (
230 user database.User
231 userErr error
232 )
233 if userID, uuidErr := uuid.Parse(r.UsernameOrID); uuidErr == nil {
234 user, userErr = db.GetUserByID(ctx, userID)
235 } else {
236 user, userErr = db.GetUserByEmailOrUsername(ctx, database.GetUserByEmailOrUsernameParams{
237 Username: r.UsernameOrID,
238 })
239 }
240 if userErr != nil {
241 return nil, xerrors.Errorf("get user %q: %w", r.UsernameOrID, userErr)
242 }
243
244 // Get workspace.
245 var (
246 workspace database.Workspace
247 workspaceErr error
248 )
249 if workspaceID, uuidErr := uuid.Parse(r.WorkspaceNameOrID); uuidErr == nil {
250 workspace, workspaceErr = db.GetWorkspaceByID(ctx, workspaceID)
251 if workspaceErr == nil && workspace.OwnerID != user.ID {
252 workspaceErr = sql.ErrNoRows
253 }
254 } else {
255 workspace, workspaceErr = db.GetWorkspaceByOwnerIDAndName(ctx, database.GetWorkspaceByOwnerIDAndNameParams{
256 OwnerID: user.ID,
257 Name: r.WorkspaceNameOrID,
258 Deleted: false,
259 })
260 }
261 if workspaceErr != nil {
262 return nil, xerrors.Errorf("get workspace %q: %w", r.WorkspaceNameOrID, workspaceErr)
263 }
264
265 // Get workspace agents.
266 agents, err := db.GetWorkspaceAgentsInLatestBuildByWorkspaceID(ctx, workspace.ID)
267 if err != nil {
268 return nil, xerrors.Errorf("get workspace agents: %w", err)
269 }
270 build, err := db.GetLatestWorkspaceBuildByWorkspaceID(ctx, workspace.ID)
271 if err != nil {
272 return nil, xerrors.Errorf("get latest workspace build: %w", err)
273 }
274 if build.Transition == database.WorkspaceTransitionStop {
275 return nil, errWorkspaceStopped

Callers 1

IssueMethod · 0.80

Calls 15

getDatabaseTerminalMethod · 0.95
WorkspaceTableMethod · 0.80
ParseMethod · 0.65
GetUserByIDMethod · 0.65
GetWorkspaceByIDMethod · 0.65
GetTemplateByIDMethod · 0.65
NewMethod · 0.65

Tested by

no test coverage detected