chore: split queries.sql into files by table (#762)

This commit is contained in:
Colin Adler 2022-04-01 15:45:23 -05:00 committed by GitHub
parent 2b1a0ee126
commit fd523100bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 2816 additions and 2770 deletions

View File

@ -89,6 +89,7 @@ jobs:
- run: go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.26
- run: go install storj.io/drpc/cmd/protoc-gen-go-drpc@v0.0.26
- run: go install golang.org/x/tools/cmd/goimports@latest
- run: "make --output-sync -j gen"
- run: ./scripts/check_unstaged.sh

View File

@ -15,10 +15,8 @@ coderd/database/dump.sql: $(wildcard coderd/database/migrations/*.sql)
.PHONY: coderd/database/dump.sql
# Generates Go code for querying the database.
coderd/database/generate: fmt/sql coderd/database/dump.sql coderd/database/query.sql
cd coderd/database && sqlc generate && rm db_tmp.go
cd coderd/database && gofmt -w -r 'Querier -> querier' *.go
cd coderd/database && gofmt -w -r 'Queries -> sqlQuerier' *.go
coderd/database/generate: fmt/sql coderd/database/dump.sql $(wildcard coderd/database/queries/*.sql)
coderd/database/generate.sh
.PHONY: coderd/database/generate
fmt/prettier:
@ -31,13 +29,18 @@ else
endif
.PHONY: fmt/prettier
fmt/sql: ./coderd/database/query.sql
npx sql-formatter \
--language postgresql \
--lines-between-queries 2 \
./coderd/database/query.sql \
--output ./coderd/database/query.sql
sed -i 's/@ /@/g' ./coderd/database/query.sql
fmt/sql: $(wildcard coderd/database/queries/*.sql)
# TODO: this is slightly slow
for fi in coderd/database/queries/*.sql; do \
npx sql-formatter \
--language postgresql \
--lines-between-queries 2 \
--tab-indent \
$$fi \
--output $$fi; \
done
sed -i 's/@ /@/g' ./coderd/database/queries/*.sql
fmt: fmt/prettier fmt/sql
.PHONY: fmt

View File

@ -362,10 +362,14 @@ CREATE UNIQUE INDEX idx_organization_name ON organizations USING btree (name);
CREATE UNIQUE INDEX idx_organization_name_lower ON organizations USING btree (lower(name));
CREATE UNIQUE INDEX idx_projects_name_lower ON projects USING btree (lower((name)::text));
CREATE UNIQUE INDEX idx_users_email ON users USING btree (email);
CREATE UNIQUE INDEX idx_users_username ON users USING btree (username);
CREATE UNIQUE INDEX idx_workspaces_name_lower ON workspaces USING btree (lower((name)::text));
CREATE UNIQUE INDEX projects_organization_id_name_idx ON projects USING btree (organization_id, name) WHERE (deleted = false);
CREATE UNIQUE INDEX users_username_lower_idx ON users USING btree (lower(username));

41
coderd/database/generate.sh Executable file
View File

@ -0,0 +1,41 @@
#!/usr/bin/env bash
# This script turns many *.sql.go files into a single queries.sql.go file. This
# is due to sqlc's behavior when using multiple sql files to output them to
# multiple Go files. We decided it would be cleaner to move these to a single
# file for readability. We should probably contribute the option to do this
# upstream instead, because this is quite janky.
set -euo pipefail
cd "$(dirname "$0")"
sqlc generate
first=true
for fi in queries/*.sql.go; do
# Find the last line from the imports section and add 1.
cut=$(grep -n ')' "$fi" | head -n 1 | cut -d: -f1)
cut=$((cut + 1))
# Copy the header from the first file only, ignoring the source comment.
if $first; then
head -n 4 < "$fi" | grep -v "source" > queries.sql.go
first=false
fi
# Append the file past the imports section into queries.sql.go.
tail -n "+$cut" < "$fi" >> queries.sql.go
done
# Remove temporary go files.
rm -f queries/*.go
# Fix struct/interface names.
gofmt -w -r 'Querier -> querier' -- *.go
gofmt -w -r 'Queries -> sqlQuerier' -- *.go
# Ensure correct imports exist. Modules must all be downloaded so we get correct
# suggestions.
go mod download
goimports -w queries.sql.go

View File

@ -33,6 +33,7 @@ CREATE TABLE projects (
-- Enforces no active projects have the same name.
CREATE UNIQUE INDEX ON projects (organization_id, name) WHERE deleted = FALSE;
CREATE UNIQUE INDEX idx_projects_name_lower ON projects USING btree (lower(name));
-- Project Versions store historical project data. When a Project Version is imported,
-- an "import" job is queued to parse parameters. A Project Version

View File

@ -12,7 +12,8 @@ CREATE TABLE workspaces (
);
-- Enforces no active workspaces have the same name.
CREATE UNIQUE INDEX ON workspaces (owner_id, name) WHERE deleted = FALSE;
CREATE UNIQUE INDEX ON workspaces USING btree (owner_id, name) WHERE deleted = FALSE;
CREATE UNIQUE INDEX idx_workspaces_name_lower ON workspaces USING btree (lower(name));
CREATE TYPE workspace_transition AS ENUM (
'start',

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,6 @@
# queries
Database queries are generated using sqlc. See: [sqlc
docs](https://docs.sqlc.dev/en/latest/tutorials/getting-started-postgresql.html)
Run `make gen` to generate models and query functions.

View File

@ -0,0 +1,59 @@
-- name: GetAPIKeyByID :one
SELECT
*
FROM
api_keys
WHERE
id = $1
LIMIT
1;
-- name: InsertAPIKey :one
INSERT INTO
api_keys (
id,
hashed_secret,
user_id,
application,
"name",
last_used,
expires_at,
created_at,
updated_at,
login_type,
oidc_access_token,
oidc_refresh_token,
oidc_id_token,
oidc_expiry,
devurl_token
)
VALUES
(
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8,
$9,
$10,
$11,
$12,
$13,
$14,
$15
) RETURNING *;
-- name: UpdateAPIKeyByID :exec
UPDATE
api_keys
SET
last_used = $2,
expires_at = $3,
oidc_access_token = $4,
oidc_refresh_token = $5,
oidc_expiry = $6
WHERE
id = $1;

View File

@ -0,0 +1,15 @@
-- name: GetFileByHash :one
SELECT
*
FROM
files
WHERE
hash = $1
LIMIT
1;
-- name: InsertFile :one
INSERT INTO
files (hash, created_at, created_by, mimetype, "data")
VALUES
($1, $2, $3, $4, $5) RETURNING *;

View File

@ -0,0 +1,22 @@
-- name: GetOrganizationMemberByUserID :one
SELECT
*
FROM
organization_members
WHERE
organization_id = $1
AND user_id = $2
LIMIT
1;
-- name: InsertOrganizationMember :one
INSERT INTO
organization_members (
organization_id,
user_id,
created_at,
updated_at,
roles
)
VALUES
($1, $2, $3, $4, $5) RETURNING *;

View File

@ -0,0 +1,38 @@
-- name: GetOrganizationByID :one
SELECT
*
FROM
organizations
WHERE
id = $1;
-- name: GetOrganizationByName :one
SELECT
*
FROM
organizations
WHERE
LOWER("name") = LOWER(@name)
LIMIT
1;
-- name: GetOrganizationsByUserID :many
SELECT
*
FROM
organizations
WHERE
id = (
SELECT
organization_id
FROM
organization_members
WHERE
user_id = $1
);
-- name: InsertOrganization :one
INSERT INTO
organizations (id, "name", description, created_at, updated_at)
VALUES
($1, $2, $3, $4, $5) RETURNING *;

View File

@ -0,0 +1,47 @@
-- name: GetParameterSchemasByJobID :many
SELECT
*
FROM
parameter_schemas
WHERE
job_id = $1;
-- name: InsertParameterSchema :one
INSERT INTO
parameter_schemas (
id,
created_at,
job_id,
"name",
description,
default_source_scheme,
default_source_value,
allow_override_source,
default_destination_scheme,
allow_override_destination,
default_refresh,
redisplay_value,
validation_error,
validation_condition,
validation_type_system,
validation_value_type
)
VALUES
(
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8,
$9,
$10,
$11,
$12,
$13,
$14,
$15,
$16
) RETURNING *;

View File

@ -0,0 +1,42 @@
-- name: DeleteParameterValueByID :exec
DELETE FROM
parameter_values
WHERE
id = $1;
-- name: GetParameterValuesByScope :many
SELECT
*
FROM
parameter_values
WHERE
scope = $1
AND scope_id = $2;
-- name: GetParameterValueByScopeAndName :one
SELECT
*
FROM
parameter_values
WHERE
scope = $1
AND scope_id = $2
AND NAME = $3
LIMIT
1;
-- name: InsertParameterValue :one
INSERT INTO
parameter_values (
id,
"name",
created_at,
updated_at,
scope,
scope_id,
source_scheme,
source_value,
destination_scheme
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING *;

View File

@ -0,0 +1,68 @@
-- name: GetProjectByID :one
SELECT
*
FROM
projects
WHERE
id = $1
LIMIT
1;
-- name: GetProjectsByIDs :many
SELECT
*
FROM
projects
WHERE
id = ANY(@ids :: uuid [ ]);
-- name: GetProjectByOrganizationAndName :one
SELECT
*
FROM
projects
WHERE
organization_id = @organization_id
AND deleted = @deleted
AND LOWER("name") = LOWER(@name)
LIMIT
1;
-- name: GetProjectsByOrganization :many
SELECT
*
FROM
projects
WHERE
organization_id = $1
AND deleted = $2;
-- name: InsertProject :one
INSERT INTO
projects (
id,
created_at,
updated_at,
organization_id,
"name",
provisioner,
active_version_id
)
VALUES
($1, $2, $3, $4, $5, $6, $7) RETURNING *;
-- name: UpdateProjectActiveVersionByID :exec
UPDATE
projects
SET
active_version_id = $2
WHERE
id = $1;
-- name: UpdateProjectDeletedByID :exec
UPDATE
projects
SET
deleted = $2
WHERE
id = $1;

View File

@ -0,0 +1,56 @@
-- name: GetProjectVersionsByProjectID :many
SELECT
*
FROM
project_versions
WHERE
project_id = $1 :: uuid;
-- name: GetProjectVersionByJobID :one
SELECT
*
FROM
project_versions
WHERE
job_id = $1;
-- name: GetProjectVersionByProjectIDAndName :one
SELECT
*
FROM
project_versions
WHERE
project_id = $1
AND "name" = $2;
-- name: GetProjectVersionByID :one
SELECT
*
FROM
project_versions
WHERE
id = $1;
-- name: InsertProjectVersion :one
INSERT INTO
project_versions (
id,
project_id,
organization_id,
created_at,
updated_at,
"name",
description,
job_id
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *;
-- name: UpdateProjectVersionByID :exec
UPDATE
project_versions
SET
project_id = $2,
updated_at = $3
WHERE
id = $1;

View File

@ -0,0 +1,34 @@
-- name: GetProvisionerDaemonByID :one
SELECT
*
FROM
provisioner_daemons
WHERE
id = $1;
-- name: GetProvisionerDaemons :many
SELECT
*
FROM
provisioner_daemons;
-- name: InsertProvisionerDaemon :one
INSERT INTO
provisioner_daemons (
id,
created_at,
organization_id,
"name",
provisioners
)
VALUES
($1, $2, $3, $4, $5) RETURNING *;
-- name: UpdateProvisionerDaemonByID :exec
UPDATE
provisioner_daemons
SET
updated_at = $2,
provisioners = $3
WHERE
id = $1;

View File

@ -0,0 +1,25 @@
-- name: GetProvisionerLogsByIDBetween :many
SELECT
*
FROM
provisioner_job_logs
WHERE
job_id = @job_id
AND (
created_at >= @created_after
OR created_at <= @created_before
)
ORDER BY
created_at DESC;
-- name: InsertProvisionerJobLogs :many
INSERT INTO
provisioner_job_logs
SELECT
unnest(@id :: uuid [ ]) AS id,
@job_id :: uuid AS job_id,
unnest(@created_at :: timestamptz [ ]) AS created_at,
unnest(@source :: log_source [ ]) AS source,
unnest(@level :: log_level [ ]) AS LEVEL,
unnest(@stage :: VARCHAR(128) [ ]) AS stage,
unnest(@output :: VARCHAR(1024) [ ]) AS output RETURNING *;

View File

@ -0,0 +1,90 @@
-- Acquires the lock for a single job that isn't started, completed,
-- canceled, and that matches an array of provisioner types.
--
-- SKIP LOCKED is used to jump over locked rows. This prevents
-- multiple provisioners from acquiring the same jobs. See:
-- https://www.postgresql.org/docs/9.5/sql-select.html#SQL-FOR-UPDATE-SHARE
-- name: AcquireProvisionerJob :one
UPDATE
provisioner_jobs
SET
started_at = @started_at,
updated_at = @started_at,
worker_id = @worker_id
WHERE
id = (
SELECT
id
FROM
provisioner_jobs AS nested
WHERE
nested.started_at IS NULL
AND nested.canceled_at IS NULL
AND nested.completed_at IS NULL
AND nested.provisioner = ANY(@types :: provisioner_type [ ])
ORDER BY
nested.created_at FOR
UPDATE
SKIP LOCKED
LIMIT
1
) RETURNING *;
-- name: GetProvisionerJobByID :one
SELECT
*
FROM
provisioner_jobs
WHERE
id = $1;
-- name: GetProvisionerJobsByIDs :many
SELECT
*
FROM
provisioner_jobs
WHERE
id = ANY(@ids :: uuid [ ]);
-- name: InsertProvisionerJob :one
INSERT INTO
provisioner_jobs (
id,
created_at,
updated_at,
organization_id,
initiator_id,
provisioner,
storage_method,
storage_source,
"type",
"input"
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING *;
-- name: UpdateProvisionerJobByID :exec
UPDATE
provisioner_jobs
SET
updated_at = $2
WHERE
id = $1;
-- name: UpdateProvisionerJobWithCancelByID :exec
UPDATE
provisioner_jobs
SET
canceled_at = $2
WHERE
id = $1;
-- name: UpdateProvisionerJobWithCompleteByID :exec
UPDATE
provisioner_jobs
SET
updated_at = $2,
completed_at = $3,
error = $4
WHERE
id = $1;

View File

@ -0,0 +1,42 @@
-- name: GetUserByID :one
SELECT
*
FROM
users
WHERE
id = $1
LIMIT
1;
-- name: GetUserByEmailOrUsername :one
SELECT
*
FROM
users
WHERE
LOWER(username) = LOWER(@username)
OR email = @email
LIMIT
1;
-- name: GetUserCount :one
SELECT
COUNT(*)
FROM
users;
-- name: InsertUser :one
INSERT INTO
users (
id,
email,
"name",
login_type,
revoked,
hashed_password,
created_at,
updated_at,
username
)
VALUES
($1, $2, $3, $4, FALSE, $5, $6, $7, $8) RETURNING *;

View File

@ -0,0 +1,54 @@
-- name: GetWorkspaceAgentByAuthToken :one
SELECT
*
FROM
workspace_agents
WHERE
auth_token = $1
ORDER BY
created_at DESC;
-- name: GetWorkspaceAgentByInstanceID :one
SELECT
*
FROM
workspace_agents
WHERE
auth_instance_id = @auth_instance_id :: TEXT
ORDER BY
created_at DESC;
-- name: GetWorkspaceAgentByResourceID :one
SELECT
*
FROM
workspace_agents
WHERE
resource_id = $1;
-- name: InsertWorkspaceAgent :one
INSERT INTO
workspace_agents (
id,
created_at,
updated_at,
resource_id,
auth_token,
auth_instance_id,
environment_variables,
startup_script,
instance_metadata,
resource_metadata
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING *;
-- name: UpdateWorkspaceAgentConnectionByID :exec
UPDATE
workspace_agents
SET
first_connected_at = $2,
last_connected_at = $3,
disconnected_at = $4
WHERE
id = $1;

View File

@ -0,0 +1,84 @@
-- name: GetWorkspaceBuildByID :one
SELECT
*
FROM
workspace_builds
WHERE
id = $1
LIMIT
1;
-- name: GetWorkspaceBuildByJobID :one
SELECT
*
FROM
workspace_builds
WHERE
job_id = $1
LIMIT
1;
-- name: GetWorkspaceBuildByWorkspaceIDAndName :one
SELECT
*
FROM
workspace_builds
WHERE
workspace_id = $1
AND "name" = $2;
-- name: GetWorkspaceBuildByWorkspaceID :many
SELECT
*
FROM
workspace_builds
WHERE
workspace_id = $1;
-- name: GetWorkspaceBuildByWorkspaceIDWithoutAfter :one
SELECT
*
FROM
workspace_builds
WHERE
workspace_id = $1
AND after_id IS NULL
LIMIT
1;
-- name: GetWorkspaceBuildsByWorkspaceIDsWithoutAfter :many
SELECT
*
FROM
workspace_builds
WHERE
workspace_id = ANY(@ids :: uuid [ ])
AND after_id IS NULL;
-- name: InsertWorkspaceBuild :one
INSERT INTO
workspace_builds (
id,
created_at,
updated_at,
workspace_id,
project_version_id,
before_id,
"name",
transition,
initiator_id,
job_id,
provisioner_state
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING *;
-- name: UpdateWorkspaceBuildByID :exec
UPDATE
workspace_builds
SET
updated_at = $2,
after_id = $3,
provisioner_state = $4
WHERE
id = $1;

View File

@ -0,0 +1,30 @@
-- name: GetWorkspaceResourceByID :one
SELECT
*
FROM
workspace_resources
WHERE
id = $1;
-- name: GetWorkspaceResourcesByJobID :many
SELECT
*
FROM
workspace_resources
WHERE
job_id = $1;
-- name: InsertWorkspaceResource :one
INSERT INTO
workspace_resources (
id,
created_at,
job_id,
transition,
address,
type,
name,
agent_id
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *;

View File

@ -0,0 +1,70 @@
-- name: GetWorkspaceByID :one
SELECT
*
FROM
workspaces
WHERE
id = $1
LIMIT
1;
-- name: GetWorkspacesByProjectID :many
SELECT
*
FROM
workspaces
WHERE
project_id = $1
AND deleted = $2;
-- name: GetWorkspacesByUserID :many
SELECT
*
FROM
workspaces
WHERE
owner_id = $1
AND deleted = $2;
-- name: GetWorkspaceByUserIDAndName :one
SELECT
*
FROM
workspaces
WHERE
owner_id = @owner_id
AND deleted = @deleted
AND LOWER("name") = LOWER(@name);
-- name: GetWorkspaceOwnerCountsByProjectIDs :many
SELECT
project_id,
COUNT(DISTINCT owner_id)
FROM
workspaces
WHERE
project_id = ANY(@ids :: uuid [ ])
GROUP BY
project_id,
owner_id;
-- name: InsertWorkspace :one
INSERT INTO
workspaces (
id,
created_at,
updated_at,
owner_id,
project_id,
name
)
VALUES
($1, $2, $3, $4, $5, $6) RETURNING *;
-- name: UpdateWorkspaceDeletedByID :exec
UPDATE
workspaces
SET
deleted = $2
WHERE
id = $1;

View File

@ -1,791 +0,0 @@
-- Database queries are generated using sqlc. See:
-- https://docs.sqlc.dev/en/latest/tutorials/getting-started-postgresql.html
--
-- Run "make gen" to generate models and query functions.
;
-- Acquires the lock for a single job that isn't started, completed,
-- canceled, and that matches an array of provisioner types.
--
-- SKIP LOCKED is used to jump over locked rows. This prevents
-- multiple provisioners from acquiring the same jobs. See:
-- https://www.postgresql.org/docs/9.5/sql-select.html#SQL-FOR-UPDATE-SHARE
-- name: AcquireProvisionerJob :one
UPDATE
provisioner_jobs
SET
started_at = @started_at,
updated_at = @started_at,
worker_id = @worker_id
WHERE
id = (
SELECT
id
FROM
provisioner_jobs AS nested
WHERE
nested.started_at IS NULL
AND nested.canceled_at IS NULL
AND nested.completed_at IS NULL
AND nested.provisioner = ANY(@types :: provisioner_type [ ])
ORDER BY
nested.created_at FOR
UPDATE
SKIP LOCKED
LIMIT
1
) RETURNING *;
-- name: DeleteParameterValueByID :exec
DELETE FROM
parameter_values
WHERE
id = $1;
-- name: GetAPIKeyByID :one
SELECT
*
FROM
api_keys
WHERE
id = $1
LIMIT
1;
-- name: GetFileByHash :one
SELECT
*
FROM
files
WHERE
hash = $1
LIMIT
1;
-- name: GetUserByID :one
SELECT
*
FROM
users
WHERE
id = $1
LIMIT
1;
-- name: GetUserByEmailOrUsername :one
SELECT
*
FROM
users
WHERE
LOWER(username) = LOWER(@username)
OR email = @email
LIMIT
1;
-- name: GetUserCount :one
SELECT
COUNT(*)
FROM
users;
-- name: GetOrganizationByID :one
SELECT
*
FROM
organizations
WHERE
id = $1;
-- name: GetOrganizationByName :one
SELECT
*
FROM
organizations
WHERE
LOWER(name) = LOWER(@name)
LIMIT
1;
-- name: GetOrganizationsByUserID :many
SELECT
*
FROM
organizations
WHERE
id = (
SELECT
organization_id
FROM
organization_members
WHERE
user_id = $1
);
-- name: GetOrganizationMemberByUserID :one
SELECT
*
FROM
organization_members
WHERE
organization_id = $1
AND user_id = $2
LIMIT
1;
-- name: GetParameterValuesByScope :many
SELECT
*
FROM
parameter_values
WHERE
scope = $1
AND scope_id = $2;
-- name: GetParameterValueByScopeAndName :one
SELECT
*
FROM
parameter_values
WHERE
scope = $1
AND scope_id = $2
AND name = $3
LIMIT
1;
-- name: GetProjectByID :one
SELECT
*
FROM
projects
WHERE
id = $1
LIMIT
1;
-- name: GetProjectsByIDs :many
SELECT
*
FROM
projects
WHERE
id = ANY(@ids :: uuid [ ]);
-- name: GetProjectByOrganizationAndName :one
SELECT
*
FROM
projects
WHERE
organization_id = @organization_id
AND deleted = @deleted
AND LOWER(name) = LOWER(@name)
LIMIT
1;
-- name: GetProjectsByOrganization :many
SELECT
*
FROM
projects
WHERE
organization_id = $1
AND deleted = $2;
-- name: GetParameterSchemasByJobID :many
SELECT
*
FROM
parameter_schemas
WHERE
job_id = $1;
-- name: GetProjectVersionsByProjectID :many
SELECT
*
FROM
project_versions
WHERE
project_id = $1 :: uuid;
-- name: GetProjectVersionByJobID :one
SELECT
*
FROM
project_versions
WHERE
job_id = $1;
-- name: GetProjectVersionByProjectIDAndName :one
SELECT
*
FROM
project_versions
WHERE
project_id = $1
AND name = $2;
-- name: GetProjectVersionByID :one
SELECT
*
FROM
project_versions
WHERE
id = $1;
-- name: GetProvisionerLogsByIDBetween :many
SELECT
*
FROM
provisioner_job_logs
WHERE
job_id = @job_id
AND (
created_at >= @created_after
OR created_at <= @created_before
)
ORDER BY
created_at;
-- name: GetProvisionerDaemonByID :one
SELECT
*
FROM
provisioner_daemons
WHERE
id = $1;
-- name: GetProvisionerDaemons :many
SELECT
*
FROM
provisioner_daemons;
-- name: GetWorkspaceAgentByAuthToken :one
SELECT
*
FROM
workspace_agents
WHERE
auth_token = $1
ORDER BY
created_at DESC;
-- name: GetWorkspaceAgentByInstanceID :one
SELECT
*
FROM
workspace_agents
WHERE
auth_instance_id = @auth_instance_id :: text
ORDER BY
created_at DESC;
-- name: GetProvisionerJobByID :one
SELECT
*
FROM
provisioner_jobs
WHERE
id = $1;
-- name: GetProvisionerJobsByIDs :many
SELECT
*
FROM
provisioner_jobs
WHERE
id = ANY(@ids :: uuid [ ]);
-- name: GetWorkspaceByID :one
SELECT
*
FROM
workspaces
WHERE
id = $1
LIMIT
1;
-- name: GetWorkspacesByProjectID :many
SELECT
*
FROM
workspaces
WHERE
project_id = $1
AND deleted = $2;
-- name: GetWorkspacesByUserID :many
SELECT
*
FROM
workspaces
WHERE
owner_id = $1
AND deleted = $2;
-- name: GetWorkspaceByUserIDAndName :one
SELECT
*
FROM
workspaces
WHERE
owner_id = @owner_id
AND deleted = @deleted
AND LOWER(name) = LOWER(@name);
-- name: GetWorkspaceOwnerCountsByProjectIDs :many
SELECT
project_id,
COUNT(DISTINCT owner_id)
FROM
workspaces
WHERE
project_id = ANY(@ids :: uuid [ ])
GROUP BY
project_id,
owner_id;
-- name: GetWorkspaceBuildByID :one
SELECT
*
FROM
workspace_builds
WHERE
id = $1
LIMIT
1;
-- name: GetWorkspaceBuildByJobID :one
SELECT
*
FROM
workspace_builds
WHERE
job_id = $1
LIMIT
1;
-- name: GetWorkspaceBuildByWorkspaceIDAndName :one
SELECT
*
FROM
workspace_builds
WHERE
workspace_id = $1
AND name = $2;
-- name: GetWorkspaceBuildByWorkspaceID :many
SELECT
*
FROM
workspace_builds
WHERE
workspace_id = $1;
-- name: GetWorkspaceBuildByWorkspaceIDWithoutAfter :one
SELECT
*
FROM
workspace_builds
WHERE
workspace_id = $1
AND after_id IS NULL
LIMIT
1;
-- name: GetWorkspaceBuildsByWorkspaceIDsWithoutAfter :many
SELECT
*
FROM
workspace_builds
WHERE
workspace_id = ANY(@ids :: uuid [ ])
AND after_id IS NULL;
-- name: GetWorkspaceResourceByID :one
SELECT
*
FROM
workspace_resources
WHERE
id = $1;
-- name: GetWorkspaceResourcesByJobID :many
SELECT
*
FROM
workspace_resources
WHERE
job_id = $1;
-- name: GetWorkspaceAgentByResourceID :one
SELECT
*
FROM
workspace_agents
WHERE
resource_id = $1;
-- name: InsertAPIKey :one
INSERT INTO
api_keys (
id,
hashed_secret,
user_id,
application,
name,
last_used,
expires_at,
created_at,
updated_at,
login_type,
oidc_access_token,
oidc_refresh_token,
oidc_id_token,
oidc_expiry,
devurl_token
)
VALUES
(
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8,
$9,
$10,
$11,
$12,
$13,
$14,
$15
) RETURNING *;
-- name: InsertFile :one
INSERT INTO
files (hash, created_at, created_by, mimetype, data)
VALUES
($1, $2, $3, $4, $5) RETURNING *;
-- name: InsertProvisionerJobLogs :many
INSERT INTO
provisioner_job_logs
SELECT
unnest(@id :: uuid [ ]) AS id,
@job_id :: uuid AS job_id,
unnest(@created_at :: timestamptz [ ]) AS created_at,
unnest(@source :: log_source [ ]) as source,
unnest(@level :: log_level [ ]) as level,
unnest(@stage :: varchar(128) [ ]) as stage,
unnest(@output :: varchar(1024) [ ]) as output RETURNING *;
-- name: InsertOrganization :one
INSERT INTO
organizations (id, name, description, created_at, updated_at)
VALUES
($1, $2, $3, $4, $5) RETURNING *;
-- name: InsertOrganizationMember :one
INSERT INTO
organization_members (
organization_id,
user_id,
created_at,
updated_at,
roles
)
VALUES
($1, $2, $3, $4, $5) RETURNING *;
-- name: InsertParameterValue :one
INSERT INTO
parameter_values (
id,
name,
created_at,
updated_at,
scope,
scope_id,
source_scheme,
source_value,
destination_scheme
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING *;
-- name: InsertProject :one
INSERT INTO
projects (
id,
created_at,
updated_at,
organization_id,
name,
provisioner,
active_version_id
)
VALUES
($1, $2, $3, $4, $5, $6, $7) RETURNING *;
-- name: InsertWorkspaceResource :one
INSERT INTO
workspace_resources (
id,
created_at,
job_id,
transition,
address,
type,
name,
agent_id
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *;
-- name: InsertProjectVersion :one
INSERT INTO
project_versions (
id,
project_id,
organization_id,
created_at,
updated_at,
name,
description,
job_id
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *;
-- name: InsertParameterSchema :one
INSERT INTO
parameter_schemas (
id,
created_at,
job_id,
name,
description,
default_source_scheme,
default_source_value,
allow_override_source,
default_destination_scheme,
allow_override_destination,
default_refresh,
redisplay_value,
validation_error,
validation_condition,
validation_type_system,
validation_value_type
)
VALUES
(
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8,
$9,
$10,
$11,
$12,
$13,
$14,
$15,
$16
) RETURNING *;
-- name: InsertProvisionerDaemon :one
INSERT INTO
provisioner_daemons (id, created_at, organization_id, name, provisioners)
VALUES
($1, $2, $3, $4, $5) RETURNING *;
-- name: InsertProvisionerJob :one
INSERT INTO
provisioner_jobs (
id,
created_at,
updated_at,
organization_id,
initiator_id,
provisioner,
storage_method,
storage_source,
type,
input
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING *;
-- name: InsertUser :one
INSERT INTO
users (
id,
email,
name,
login_type,
revoked,
hashed_password,
created_at,
updated_at,
username
)
VALUES
($1, $2, $3, $4, false, $5, $6, $7, $8) RETURNING *;
-- name: InsertWorkspace :one
INSERT INTO
workspaces (
id,
created_at,
updated_at,
owner_id,
project_id,
name
)
VALUES
($1, $2, $3, $4, $5, $6) RETURNING *;
-- name: InsertWorkspaceAgent :one
INSERT INTO
workspace_agents (
id,
created_at,
updated_at,
resource_id,
auth_token,
auth_instance_id,
environment_variables,
startup_script,
instance_metadata,
resource_metadata
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING *;
-- name: InsertWorkspaceBuild :one
INSERT INTO
workspace_builds (
id,
created_at,
updated_at,
workspace_id,
project_version_id,
before_id,
name,
transition,
initiator_id,
job_id,
provisioner_state
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING *;
-- name: UpdateAPIKeyByID :exec
UPDATE
api_keys
SET
last_used = $2,
expires_at = $3,
oidc_access_token = $4,
oidc_refresh_token = $5,
oidc_expiry = $6
WHERE
id = $1;
-- name: UpdateProjectActiveVersionByID :exec
UPDATE
projects
SET
active_version_id = $2
WHERE
id = $1;
-- name: UpdateProjectDeletedByID :exec
UPDATE
projects
SET
deleted = $2
WHERE
id = $1;
-- name: UpdateProjectVersionByID :exec
UPDATE
project_versions
SET
project_id = $2,
updated_at = $3
WHERE
id = $1;
-- name: UpdateProvisionerDaemonByID :exec
UPDATE
provisioner_daemons
SET
updated_at = $2,
provisioners = $3
WHERE
id = $1;
-- name: UpdateProvisionerJobByID :exec
UPDATE
provisioner_jobs
SET
updated_at = $2
WHERE
id = $1;
-- name: UpdateProvisionerJobWithCancelByID :exec
UPDATE
provisioner_jobs
SET
canceled_at = $2
WHERE
id = $1;
-- name: UpdateProvisionerJobWithCompleteByID :exec
UPDATE
provisioner_jobs
SET
updated_at = $2,
completed_at = $3,
error = $4
WHERE
id = $1;
-- name: UpdateWorkspaceDeletedByID :exec
UPDATE
workspaces
SET
deleted = $2
WHERE
id = $1;
-- name: UpdateWorkspaceAgentConnectionByID :exec
UPDATE
workspace_agents
SET
first_connected_at = $2,
last_connected_at = $3,
disconnected_at = $4
WHERE
id = $1;
-- name: UpdateWorkspaceBuildByID :exec
UPDATE
workspace_builds
SET
updated_at = $2,
after_id = $3,
provisioner_state = $4
WHERE
id = $1;

View File

@ -4,8 +4,8 @@
version: "1"
packages:
- name: "database"
path: "."
queries: "./query.sql"
path: "./queries"
queries: "./queries"
schema: "./dump.sql"
engine: "postgresql"
emit_interface: true

3
go.mod
View File

@ -40,6 +40,7 @@ require (
github.com/briandowns/spinner v1.18.1
github.com/charmbracelet/charm v0.11.0
github.com/charmbracelet/lipgloss v0.5.0
github.com/cli/safeexec v1.0.0
github.com/cloudflare/cloudflared v0.0.0-20220308214351-5352b3cf0489
github.com/coder/retry v1.3.0
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
@ -93,8 +94,6 @@ require (
storj.io/drpc v0.0.30
)
require github.com/cli/safeexec v1.0.0
require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/BurntSushi/toml v1.0.0 // indirect