Skip to content

Commit 129fe01

Browse files
committed
Remove sqlc queries
1 parent 472c8ca commit 129fe01

File tree

10 files changed

+377
-327
lines changed

10 files changed

+377
-327
lines changed

coderd/database/modelqueries.go

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package database
22

33
import (
44
"context"
5-
"embed"
5+
"database/sql"
6+
7+
"github.com/coder/coder/coderd/database/sqlxqueries"
8+
69
"fmt"
710
"strings"
811

9-
_ "embed"
10-
1112
"github.com/google/uuid"
1213
"github.com/lib/pq"
1314
"golang.org/x/xerrors"
@@ -16,9 +17,6 @@ import (
1617
"github.com/coder/coder/coderd/rbac/regosql"
1718
)
1819

19-
//go:embed sqlxqueries/*.sql
20-
var sqlxQueries embed.FS
21-
2220
const (
2321
authorizedQueryPlaceholder = "-- @authorize_filter"
2422
)
@@ -184,6 +182,8 @@ func (q *sqlQuerier) GetTemplateGroupRoles(ctx context.Context, id uuid.UUID) ([
184182
type workspaceQuerier interface {
185183
GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspacesParams, prepared rbac.PreparedAuthorized) ([]WorkspaceWithData, error)
186184
GetWorkspaces(ctx context.Context, arg GetWorkspacesParams) ([]WorkspaceWithData, error)
185+
GetWorkspaceByID(ctx context.Context, id uuid.UUID) (WorkspaceWithData, error)
186+
GetWorkspaceByOwnerIDAndName(ctx context.Context, arg GetWorkspaceByOwnerIDAndNameParams) (WorkspaceWithData, error)
187187
}
188188

189189
// WorkspaceWithData includes related information to the workspace.
@@ -222,20 +222,52 @@ type GetWorkspacesParams struct {
222222
TemplateIds []uuid.UUID `db:"template_ids" json:"template_ids"`
223223
WorkspaceIds []uuid.UUID `db:"workspace_ids" json:"workspace_ids"`
224224
Name string `db:"name" json:"name"`
225+
ExactName string `db:"exact_name" json:"exact_name"`
225226
HasAgent string `db:"has_agent" json:"has_agent"`
226227
AgentInactiveDisconnectTimeoutSeconds int64 `db:"agent_inactive_disconnect_timeout_seconds" json:"agent_inactive_disconnect_timeout_seconds"`
227228
Offset int32 `db:"offset_" json:"offset_"`
228229
Limit int32 `db:"limit_" json:"limit_"`
229230
}
230231

231232
func (q *sqlQuerier) GetWorkspaces(ctx context.Context, arg GetWorkspacesParams) ([]WorkspaceWithData, error) {
232-
// Only GetAuthorizedWorkspaces should be called. This is a placeholder for the interface,
233-
// as the GetWorkspaces method is used in dbauthz package.
234-
panic("not implemented")
233+
return q.GetAuthorizedWorkspaces(ctx, arg, NoopAuthorizer{})
234+
}
235+
236+
type GetWorkspaceByOwnerIDAndNameParams struct {
237+
OwnerID uuid.UUID `db:"owner_id" json:"owner_id"`
238+
Deleted bool `db:"deleted" json:"deleted"`
239+
Name string `db:"name" json:"name"`
240+
}
241+
242+
func (q *sqlQuerier) GetWorkspaceByOwnerIDAndName(ctx context.Context, arg GetWorkspaceByOwnerIDAndNameParams) (WorkspaceWithData, error) {
243+
workspaces, err := q.GetAuthorizedWorkspaces(ctx, GetWorkspacesParams{
244+
Deleted: arg.Deleted,
245+
ExactName: arg.Name,
246+
OwnerID: arg.OwnerID,
247+
Limit: 1,
248+
}, NoopAuthorizer{})
249+
if err != nil {
250+
return WorkspaceWithData{}, err
251+
}
252+
if len(workspaces) == 0 {
253+
return WorkspaceWithData{}, sql.ErrNoRows
254+
}
255+
return workspaces[0], nil
235256
}
236257

237-
//go:embed sqlxqueries/getworkspaces.sql
238-
var getAuthorizedWorkspacesQuery string
258+
func (q *sqlQuerier) GetWorkspaceByID(ctx context.Context, id uuid.UUID) (WorkspaceWithData, error) {
259+
workspaces, err := q.GetAuthorizedWorkspaces(ctx, GetWorkspacesParams{
260+
WorkspaceIds: []uuid.UUID{id},
261+
Limit: 1,
262+
}, NoopAuthorizer{})
263+
if err != nil {
264+
return WorkspaceWithData{}, err
265+
}
266+
if len(workspaces) == 0 {
267+
return WorkspaceWithData{}, sql.ErrNoRows
268+
}
269+
return workspaces[0], nil
270+
}
239271

240272
// GetAuthorizedWorkspaces returns all workspaces that the user is authorized to access.
241273
// This code is copied from `GetWorkspaces` and adds the authorized filter WHERE
@@ -246,6 +278,11 @@ func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspa
246278
return nil, xerrors.Errorf("compile authorized filter: %w", err)
247279
}
248280

281+
getAuthorizedWorkspacesQuery, err := sqlxqueries.GetAuthorizedWorkspaces()
282+
if err != nil {
283+
return nil, xerrors.Errorf("get query: %w", err)
284+
}
285+
249286
// In order to properly use ORDER BY, OFFSET, and LIMIT, we need to inject the
250287
// authorizedFilter between the end of the where clause and those statements.
251288
filtered, err := insertAuthorizedFilter(getAuthorizedWorkspacesQuery, fmt.Sprintf(" AND %s", authorizedFilter))
@@ -302,3 +339,13 @@ func insertAuthorizedFilter(query string, replaceWith string) (string, error) {
302339
filtered := strings.Replace(query, authorizedQueryPlaceholder, replaceWith, 1)
303340
return filtered, nil
304341
}
342+
343+
// TODO: This can be removed once dbauthz becomes the default.
344+
type NoopAuthorizer struct{}
345+
346+
func (NoopAuthorizer) Authorize(ctx context.Context, object rbac.Object) error {
347+
return nil
348+
}
349+
func (NoopAuthorizer) CompileToSQL(ctx context.Context, cfg regosql.ConvertConfig) (string, error) {
350+
return "", nil
351+
}

coderd/database/querier.go

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

coderd/database/queries.sql.go

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

coderd/database/queries/workspaces.sql

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
-- name: GetWorkspaceByID :one
2-
SELECT
3-
*
4-
FROM
5-
workspaces
6-
WHERE
7-
id = $1
8-
LIMIT
9-
1;
10-
111
-- name: GetWorkspaceByWorkspaceAppID :one
122
SELECT
133
*
@@ -73,17 +63,6 @@ WHERE
7363
)
7464
);
7565

76-
-- name: GetWorkspaceByOwnerIDAndName :one
77-
SELECT
78-
*
79-
FROM
80-
workspaces
81-
WHERE
82-
owner_id = @owner_id
83-
AND deleted = @deleted
84-
AND LOWER("name") = LOWER(@name)
85-
ORDER BY created_at DESC;
86-
8766
-- name: InsertWorkspace :one
8867
INSERT INTO
8968
workspaces (

coderd/database/sqlxqueries/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Editor/IDE config
2+
3+
To edit template files, it is best to configure your IDE to work with go template files.
4+
5+
## VSCode
6+
7+
Required extension (Default Golang Extension): https://marketplace.visualstudio.com/items?itemName=golang.Go
8+
9+
The default extension [supports syntax highlighting](https://github.com/golang/vscode-go/wiki/features#go-template-syntax-highlighting), but requires a configuration change. You must add this section to your golang extension settings:
10+
11+
```json
12+
"gopls": {
13+
"ui.semanticTokens": true
14+
},
15+
```
16+
17+
Unfortunately, the VSCode extension does not support both go template and postgres highlighting. You can switch between the two with:
18+
19+
1. `ctl + shift + p`
20+
1. "Change language Mode"
21+
1. "Postgres" or "Go Template File"
22+
- Feel free to create a permanent file association with `*.gosql` files.
23+
24+
25+
## Goland
26+
27+
Goland supports [template highlighting](https://www.jetbrains.com/help/go/integration-with-go-templates.html) out of the box. To associate sql files, add a new file type in **Editor** settings. Select "Go template files". Add a new filename of `*.gosql` and select "postgres" as the "Template Data Language".
28+
29+
30+
![Goland language configuration](./imgs/goland-gosql.png)
31+
32+
It also helps to support the sqlc type variables. You can do this by adding ["User Parameters"](https://www.jetbrains.com/help/datagrip/settings-tools-database-user-parameters.html) in database queries.
33+
34+
![Goland language configuration](./imgs/goland-user-params.png)
35+
36+
You can also add `dump.sql` as a DDL data source for proper table column recognition.

0 commit comments

Comments
 (0)