mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: replace original GetPrebuiltWorkspaces with optimized version (#18832)
Fixes https://github.com/coder/internal/issues/715 Follow-up from https://github.com/coder/coder/pull/18717 Now that we've determined the updated query is safe, remove the duplication.
This commit is contained in:
@@ -7361,63 +7361,6 @@ func (q *sqlQuerier) GetPresetsBackoff(ctx context.Context, lookback time.Time)
|
||||
}
|
||||
|
||||
const getRunningPrebuiltWorkspaces = `-- name: GetRunningPrebuiltWorkspaces :many
|
||||
SELECT
|
||||
p.id,
|
||||
p.name,
|
||||
p.template_id,
|
||||
b.template_version_id,
|
||||
p.current_preset_id AS current_preset_id,
|
||||
p.ready,
|
||||
p.created_at
|
||||
FROM workspace_prebuilds p
|
||||
INNER JOIN workspace_latest_builds b ON b.workspace_id = p.id
|
||||
WHERE (b.transition = 'start'::workspace_transition
|
||||
AND b.job_status = 'succeeded'::provisioner_job_status)
|
||||
ORDER BY p.id
|
||||
`
|
||||
|
||||
type GetRunningPrebuiltWorkspacesRow struct {
|
||||
ID uuid.UUID `db:"id" json:"id"`
|
||||
Name string `db:"name" json:"name"`
|
||||
TemplateID uuid.UUID `db:"template_id" json:"template_id"`
|
||||
TemplateVersionID uuid.UUID `db:"template_version_id" json:"template_version_id"`
|
||||
CurrentPresetID uuid.NullUUID `db:"current_preset_id" json:"current_preset_id"`
|
||||
Ready bool `db:"ready" json:"ready"`
|
||||
CreatedAt time.Time `db:"created_at" json:"created_at"`
|
||||
}
|
||||
|
||||
func (q *sqlQuerier) GetRunningPrebuiltWorkspaces(ctx context.Context) ([]GetRunningPrebuiltWorkspacesRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getRunningPrebuiltWorkspaces)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetRunningPrebuiltWorkspacesRow
|
||||
for rows.Next() {
|
||||
var i GetRunningPrebuiltWorkspacesRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.TemplateID,
|
||||
&i.TemplateVersionID,
|
||||
&i.CurrentPresetID,
|
||||
&i.Ready,
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getRunningPrebuiltWorkspacesOptimized = `-- name: GetRunningPrebuiltWorkspacesOptimized :many
|
||||
WITH latest_prebuilds AS (
|
||||
-- All workspaces that match the following criteria:
|
||||
-- 1. Owned by prebuilds user
|
||||
@@ -7476,7 +7419,7 @@ LEFT JOIN workspace_latest_presets ON workspace_latest_presets.workspace_id = la
|
||||
ORDER BY latest_prebuilds.id
|
||||
`
|
||||
|
||||
type GetRunningPrebuiltWorkspacesOptimizedRow struct {
|
||||
type GetRunningPrebuiltWorkspacesRow struct {
|
||||
ID uuid.UUID `db:"id" json:"id"`
|
||||
Name string `db:"name" json:"name"`
|
||||
TemplateID uuid.UUID `db:"template_id" json:"template_id"`
|
||||
@@ -7486,15 +7429,15 @@ type GetRunningPrebuiltWorkspacesOptimizedRow struct {
|
||||
CreatedAt time.Time `db:"created_at" json:"created_at"`
|
||||
}
|
||||
|
||||
func (q *sqlQuerier) GetRunningPrebuiltWorkspacesOptimized(ctx context.Context) ([]GetRunningPrebuiltWorkspacesOptimizedRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getRunningPrebuiltWorkspacesOptimized)
|
||||
func (q *sqlQuerier) GetRunningPrebuiltWorkspaces(ctx context.Context) ([]GetRunningPrebuiltWorkspacesRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getRunningPrebuiltWorkspaces)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetRunningPrebuiltWorkspacesOptimizedRow
|
||||
var items []GetRunningPrebuiltWorkspacesRow
|
||||
for rows.Next() {
|
||||
var i GetRunningPrebuiltWorkspacesOptimizedRow
|
||||
var i GetRunningPrebuiltWorkspacesRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
|
||||
Reference in New Issue
Block a user