From 80b04d85c006f105a0e638fee26739f86096d057 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 10 Feb 2026 18:09:23 +0000 Subject: [PATCH 1/3] chore(workspace-engine): add sqlc config and starter templates Co-authored-by: Justin Brooks --- apps/workspace-engine/Makefile | 42 ++++++- apps/workspace-engine/README.md | 13 +++ .../workspace-engine/pkg/db/sqlcgen/README.md | 9 ++ apps/workspace-engine/sqlc.yaml | 14 +++ apps/workspace-engine/sqlc/README.md | 53 +++++++++ .../sqlc/queries/workspace_engine.sql | 110 ++++++++++++++++++ .../sqlc/templates/query.template.sql | 47 ++++++++ .../sqlc/templates/schema.template.sql | 31 +++++ 8 files changed, 318 insertions(+), 1 deletion(-) create mode 100644 apps/workspace-engine/pkg/db/sqlcgen/README.md create mode 100644 apps/workspace-engine/sqlc.yaml create mode 100644 apps/workspace-engine/sqlc/README.md create mode 100644 apps/workspace-engine/sqlc/queries/workspace_engine.sql create mode 100644 apps/workspace-engine/sqlc/templates/query.template.sql create mode 100644 apps/workspace-engine/sqlc/templates/schema.template.sql diff --git a/apps/workspace-engine/Makefile b/apps/workspace-engine/Makefile index e1dcb77cb..aca158430 100644 --- a/apps/workspace-engine/Makefile +++ b/apps/workspace-engine/Makefile @@ -1,4 +1,4 @@ -.PHONY: all build clean proto deps run-server run-client test lint help proto-install +.PHONY: all build clean proto deps run-server run-client test lint help proto-install sqlc-install sqlc-generate sqlc-compile sqlc-verify # Variables BINARY := bin/workspace-engine @@ -62,6 +62,42 @@ lint: echo "golangci-lint not installed. Install with: brew install golangci-lint"; \ fi +# sqlc +sqlc-install: + @echo "Installing sqlc..." + $(GO) install github.com/sqlc-dev/sqlc/cmd/sqlc@latest + +sqlc-generate: + @echo "Generating typed query code with sqlc..." + @SQLC_BIN=$$(go env GOPATH)/bin/sqlc; \ + if [ ! -x "$$SQLC_BIN" ]; then \ + echo "sqlc not found. Installing..."; \ + $(GO) install github.com/sqlc-dev/sqlc/cmd/sqlc@latest; \ + fi; \ + $$SQLC_BIN generate -f sqlc.yaml + +sqlc-compile: + @echo "Validating sqlc configuration and queries..." + @SQLC_BIN=$$(go env GOPATH)/bin/sqlc; \ + if [ ! -x "$$SQLC_BIN" ]; then \ + echo "sqlc not found. Installing..."; \ + $(GO) install github.com/sqlc-dev/sqlc/cmd/sqlc@latest; \ + fi; \ + $$SQLC_BIN compile -f sqlc.yaml + +sqlc-verify: + @echo "Verifying sqlc queries against live database..." + @if [ -z "$$POSTGRES_URL" ]; then \ + echo "POSTGRES_URL is required for sqlc verify"; \ + exit 1; \ + fi + @SQLC_BIN=$$(go env GOPATH)/bin/sqlc; \ + if [ ! -x "$$SQLC_BIN" ]; then \ + echo "sqlc not found. Installing..."; \ + $(GO) install github.com/sqlc-dev/sqlc/cmd/sqlc@latest; \ + fi; \ + $$SQLC_BIN verify -f sqlc.yaml + # Format code fmt: @echo "Formatting code..." @@ -100,6 +136,10 @@ help: @echo " make test - Run tests" @echo " make test-coverage - Run tests with coverage report" @echo " make lint - Run linter" + @echo " make sqlc-install - Install sqlc CLI" + @echo " make sqlc-generate - Generate typed Go query code from sqlc files" + @echo " make sqlc-compile - Validate sqlc config, schema, and queries" + @echo " make sqlc-verify - Verify queries against a live DB (POSTGRES_URL required)" @echo " make fmt - Format code" @echo " make clean - Clean build artifacts" @echo " make install-tools - Install development tools" diff --git a/apps/workspace-engine/README.md b/apps/workspace-engine/README.md index dc9149bf4..ce9cd6b38 100644 --- a/apps/workspace-engine/README.md +++ b/apps/workspace-engine/README.md @@ -85,12 +85,25 @@ make dev # Run without building make test # Run tests make test-coverage # Run tests with coverage report make lint # Run linter +make sqlc-generate # Generate typed Go query code from sqlc files +make sqlc-compile # Validate sqlc config, schema, and queries +make sqlc-verify # Verify queries against live DB (POSTGRES_URL required) make fmt # Format code make clean # Clean build artifacts make install-tools # Install development tools make help # Show help message ``` +### SQLC typed queries + +`workspace-engine` now includes a starter `sqlc` setup under `./sqlc`. + +```bash +make sqlc-generate +``` + +Generated code is written to `pkg/db/sqlcgen`. + ## API Overview The Workspace Engine provides six main gRPC endpoints: diff --git a/apps/workspace-engine/pkg/db/sqlcgen/README.md b/apps/workspace-engine/pkg/db/sqlcgen/README.md new file mode 100644 index 000000000..37de41f3e --- /dev/null +++ b/apps/workspace-engine/pkg/db/sqlcgen/README.md @@ -0,0 +1,9 @@ +# Generated sqlc package + +This directory contains generated code from `apps/workspace-engine/sqlc.yaml`. + +Regenerate with: + +```bash +make sqlc-generate +``` diff --git a/apps/workspace-engine/sqlc.yaml b/apps/workspace-engine/sqlc.yaml new file mode 100644 index 000000000..16b3dc957 --- /dev/null +++ b/apps/workspace-engine/sqlc.yaml @@ -0,0 +1,14 @@ +version: "2" +sql: + - engine: "postgresql" + queries: "sqlc/queries" + schema: "../../packages/db/drizzle" + database: + uri: "${POSTGRES_URL}" + gen: + go: + package: "sqlcgen" + out: "pkg/db/sqlcgen" + sql_package: "pgx/v5" + emit_interface: true + emit_json_tags: true diff --git a/apps/workspace-engine/sqlc/README.md b/apps/workspace-engine/sqlc/README.md new file mode 100644 index 000000000..57be77f70 --- /dev/null +++ b/apps/workspace-engine/sqlc/README.md @@ -0,0 +1,53 @@ +# sqlc setup for workspace-engine + +This directory contains a ready-to-use `sqlc` setup for generating typed Go query code. + +## Layout + +- `../sqlc.yaml`: sqlc project config +- `queries/workspace_engine.sql`: starter queries for workspace-engine tables +- `templates/query.template.sql`: copy/paste template for new query files +- `templates/schema.template.sql`: minimal schema template for isolated local experiments + +Generated code is written to: + +- `../pkg/db/sqlcgen` + +## Generate code + +From `apps/workspace-engine`: + +```bash +make sqlc-generate +``` + +Or directly: + +```bash +$(go env GOPATH)/bin/sqlc generate -f sqlc.yaml +``` + +## Validate parsing/codegen + +```bash +make sqlc-compile +``` + +## Verify against a live database + +`sqlc generate` uses the `schema` path and does **not** need a database connection. + +`database.uri` is used for database-backed checks (for example `sqlc verify`). + +```bash +POSTGRES_URL="postgres://postgres:password@localhost:5432/postgres?sslmode=disable" \ + make sqlc-verify +``` + +## Adding new queries + +1. Copy `templates/query.template.sql` into `queries/.sql`. +2. Rename query annotations (`-- name: ...`) to match your use case. +3. Keep query files focused by feature/domain. +4. Run `make sqlc-generate`. +5. Use generated methods from `pkg/db/sqlcgen`. diff --git a/apps/workspace-engine/sqlc/queries/workspace_engine.sql b/apps/workspace-engine/sqlc/queries/workspace_engine.sql new file mode 100644 index 000000000..a5be13baa --- /dev/null +++ b/apps/workspace-engine/sqlc/queries/workspace_engine.sql @@ -0,0 +1,110 @@ +-- name: GetDeploymentByID :one +SELECT + d.id, + d.name, + d.slug, + d.description, + d.system_id, + d.job_agent_id, + d.job_agent_config, + d.retry_count, + d.timeout, + d.resource_selector +FROM deployment AS d +WHERE d.id = $1 +LIMIT 1; + +-- name: ListDeploymentsByWorkspace :many +SELECT + d.id, + d.name, + d.slug, + d.description, + d.system_id, + d.job_agent_id, + d.job_agent_config, + d.retry_count, + d.timeout, + d.resource_selector +FROM deployment AS d +INNER JOIN system AS s + ON s.id = d.system_id +WHERE s.workspace_id = $1 +ORDER BY d.name ASC; + +-- name: UpsertDeployment :one +INSERT INTO deployment ( + id, + name, + slug, + description, + system_id, + job_agent_id, + job_agent_config, + retry_count, + timeout, + resource_selector +) +VALUES ( + $1, + $2, + $3, + $4, + $5, + $6, + $7, + $8, + $9, + $10 +) +ON CONFLICT (id) +DO UPDATE SET + name = EXCLUDED.name, + slug = EXCLUDED.slug, + description = EXCLUDED.description, + system_id = EXCLUDED.system_id, + job_agent_id = EXCLUDED.job_agent_id, + job_agent_config = EXCLUDED.job_agent_config, + retry_count = EXCLUDED.retry_count, + timeout = EXCLUDED.timeout, + resource_selector = EXCLUDED.resource_selector +RETURNING + id, + name, + slug, + description, + system_id, + job_agent_id, + job_agent_config, + retry_count, + timeout, + resource_selector; + +-- name: ListEnvironmentsByWorkspace :many +SELECT + e.id, + e.name, + e.system_id, + e.created_at, + e.description, + e.resource_selector +FROM environment AS e +INNER JOIN system AS s + ON s.id = e.system_id +WHERE s.workspace_id = $1 +ORDER BY e.name ASC; + +-- name: GetReleaseTargetID :one +SELECT + rt.id +FROM release_target AS rt +WHERE rt.resource_id = $1 + AND rt.environment_id = $2 + AND rt.deployment_id = $3 +LIMIT 1; + +-- name: InsertReleaseTargetIfMissing :exec +INSERT INTO release_target (resource_id, environment_id, deployment_id) +VALUES ($1, $2, $3) +ON CONFLICT (resource_id, environment_id, deployment_id) +DO NOTHING; diff --git a/apps/workspace-engine/sqlc/templates/query.template.sql b/apps/workspace-engine/sqlc/templates/query.template.sql new file mode 100644 index 000000000..33bd03f3b --- /dev/null +++ b/apps/workspace-engine/sqlc/templates/query.template.sql @@ -0,0 +1,47 @@ +-- Copy this file into sqlc/queries/.sql and update names/columns. +-- sqlc annotations: +-- :one -> single row result +-- :many -> slice result +-- :exec -> no returned rows +-- :execrows -> rows affected (int64) + +-- name: GetThingByID :one +SELECT + t.id, + t.name, + t.created_at +FROM your_table AS t +WHERE t.id = $1 +LIMIT 1; + +-- name: ListThingsByWorkspace :many +SELECT + t.id, + t.name, + t.created_at +FROM your_table AS t +WHERE t.workspace_id = $1 +ORDER BY t.created_at DESC +LIMIT $2 +OFFSET $3; + +-- name: UpsertThing :one +INSERT INTO your_table ( + id, + workspace_id, + name +) +VALUES ($1, $2, $3) +ON CONFLICT (id) +DO UPDATE SET + workspace_id = EXCLUDED.workspace_id, + name = EXCLUDED.name +RETURNING + id, + workspace_id, + name, + created_at; + +-- name: DeleteThingByID :execrows +DELETE FROM your_table +WHERE id = $1; diff --git a/apps/workspace-engine/sqlc/templates/schema.template.sql b/apps/workspace-engine/sqlc/templates/schema.template.sql new file mode 100644 index 000000000..d4d427766 --- /dev/null +++ b/apps/workspace-engine/sqlc/templates/schema.template.sql @@ -0,0 +1,31 @@ +-- Minimal schema template for local sqlc experiments. +-- For production code generation in this repo we use ../../packages/db/drizzle. + +CREATE TABLE workspace ( + id uuid PRIMARY KEY, + name text NOT NULL, + slug text NOT NULL UNIQUE, + created_at timestamptz NOT NULL DEFAULT now() +); + +CREATE TABLE system ( + id uuid PRIMARY KEY, + workspace_id uuid NOT NULL REFERENCES workspace(id) ON DELETE CASCADE, + name text NOT NULL, + slug text NOT NULL, + description text NOT NULL DEFAULT '', + UNIQUE (workspace_id, slug) +); + +CREATE TABLE deployment ( + id uuid PRIMARY KEY, + system_id uuid NOT NULL REFERENCES system(id) ON DELETE CASCADE, + name text NOT NULL, + slug text NOT NULL, + description text NOT NULL, + job_agent_id uuid, + job_agent_config jsonb NOT NULL DEFAULT '{}', + retry_count integer NOT NULL DEFAULT 0, + timeout integer, + resource_selector jsonb +); From 68a3423ca2d1fe2014ad72bb7118769cf4d24219 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 10 Feb 2026 18:28:11 +0000 Subject: [PATCH 2/3] fix(workspace-engine): use stable sqlc schema snapshot Co-authored-by: Justin Brooks --- apps/workspace-engine/sqlc.yaml | 2 +- apps/workspace-engine/sqlc/README.md | 6 ++ .../sqlc/schema/workspace_engine.sql | 62 +++++++++++++++++++ .../sqlc/templates/schema.template.sql | 2 +- 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 apps/workspace-engine/sqlc/schema/workspace_engine.sql diff --git a/apps/workspace-engine/sqlc.yaml b/apps/workspace-engine/sqlc.yaml index 16b3dc957..b5fae04c2 100644 --- a/apps/workspace-engine/sqlc.yaml +++ b/apps/workspace-engine/sqlc.yaml @@ -2,7 +2,7 @@ version: "2" sql: - engine: "postgresql" queries: "sqlc/queries" - schema: "../../packages/db/drizzle" + schema: "sqlc/schema/workspace_engine.sql" database: uri: "${POSTGRES_URL}" gen: diff --git a/apps/workspace-engine/sqlc/README.md b/apps/workspace-engine/sqlc/README.md index 57be77f70..230acc3ef 100644 --- a/apps/workspace-engine/sqlc/README.md +++ b/apps/workspace-engine/sqlc/README.md @@ -5,6 +5,7 @@ This directory contains a ready-to-use `sqlc` setup for generating typed Go quer ## Layout - `../sqlc.yaml`: sqlc project config +- `schema/workspace_engine.sql`: schema snapshot used by sqlc code generation - `queries/workspace_engine.sql`: starter queries for workspace-engine tables - `templates/query.template.sql`: copy/paste template for new query files - `templates/schema.template.sql`: minimal schema template for isolated local experiments @@ -33,6 +34,11 @@ $(go env GOPATH)/bin/sqlc generate -f sqlc.yaml make sqlc-compile ``` +## Keeping schema current + +The `schema/workspace_engine.sql` file is a stable schema snapshot for sqlc. +When Drizzle migrations change tables used by sqlc queries, update this snapshot. + ## Verify against a live database `sqlc generate` uses the `schema` path and does **not** need a database connection. diff --git a/apps/workspace-engine/sqlc/schema/workspace_engine.sql b/apps/workspace-engine/sqlc/schema/workspace_engine.sql new file mode 100644 index 000000000..44d239872 --- /dev/null +++ b/apps/workspace-engine/sqlc/schema/workspace_engine.sql @@ -0,0 +1,62 @@ +CREATE EXTENSION IF NOT EXISTS "pgcrypto"; + +CREATE TABLE workspace ( + id uuid PRIMARY KEY DEFAULT gen_random_uuid(), + name text NOT NULL, + slug text NOT NULL UNIQUE, + created_at timestamptz NOT NULL DEFAULT now() +); + +CREATE TABLE system ( + id uuid PRIMARY KEY DEFAULT gen_random_uuid(), + workspace_id uuid NOT NULL REFERENCES workspace(id) ON DELETE CASCADE, + name text NOT NULL, + slug text NOT NULL, + description text NOT NULL DEFAULT '', + UNIQUE (workspace_id, slug) +); + +CREATE TABLE job_agent ( + id uuid PRIMARY KEY DEFAULT gen_random_uuid(), + workspace_id uuid NOT NULL REFERENCES workspace(id) ON DELETE CASCADE, + name text NOT NULL, + type text NOT NULL, + config jsonb NOT NULL DEFAULT '{}' +); + +CREATE TABLE deployment ( + id uuid PRIMARY KEY DEFAULT gen_random_uuid(), + name text NOT NULL, + slug text NOT NULL, + description text NOT NULL, + system_id uuid NOT NULL REFERENCES system(id) ON DELETE CASCADE, + job_agent_id uuid REFERENCES job_agent(id) ON DELETE SET NULL, + job_agent_config jsonb NOT NULL DEFAULT '{}', + retry_count integer NOT NULL DEFAULT 0, + timeout integer, + resource_selector jsonb, + UNIQUE (system_id, slug) +); + +CREATE TABLE environment ( + id uuid PRIMARY KEY DEFAULT gen_random_uuid(), + system_id uuid NOT NULL REFERENCES system(id) ON DELETE CASCADE, + name text NOT NULL, + created_at timestamptz NOT NULL DEFAULT now(), + description text, + resource_selector jsonb, + UNIQUE (system_id, name) +); + +CREATE TABLE resource ( + id uuid PRIMARY KEY DEFAULT gen_random_uuid(), + workspace_id uuid NOT NULL REFERENCES workspace(id) ON DELETE CASCADE +); + +CREATE TABLE release_target ( + id uuid PRIMARY KEY DEFAULT gen_random_uuid(), + resource_id uuid NOT NULL REFERENCES resource(id) ON DELETE CASCADE, + environment_id uuid NOT NULL REFERENCES environment(id) ON DELETE CASCADE, + deployment_id uuid NOT NULL REFERENCES deployment(id) ON DELETE CASCADE, + UNIQUE (resource_id, environment_id, deployment_id) +); diff --git a/apps/workspace-engine/sqlc/templates/schema.template.sql b/apps/workspace-engine/sqlc/templates/schema.template.sql index d4d427766..aecd7bf6f 100644 --- a/apps/workspace-engine/sqlc/templates/schema.template.sql +++ b/apps/workspace-engine/sqlc/templates/schema.template.sql @@ -1,5 +1,5 @@ -- Minimal schema template for local sqlc experiments. --- For production code generation in this repo we use ../../packages/db/drizzle. +-- For workspace-engine sqlc generation in this repo see sqlc/schema/workspace_engine.sql. CREATE TABLE workspace ( id uuid PRIMARY KEY, From f0a902bf74eba6650b592700d8b774ad24b3c668 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 10 Feb 2026 18:33:34 +0000 Subject: [PATCH 3/3] feat(workspace-engine): generate sqlc starter package Co-authored-by: Justin Brooks --- apps/workspace-engine/Makefile | 16 +- apps/workspace-engine/README.md | 2 +- apps/workspace-engine/pkg/db/sqlcgen/db.go | 32 ++ .../workspace-engine/pkg/db/sqlcgen/models.go | 66 +++++ .../pkg/db/sqlcgen/querier.go | 22 ++ .../pkg/db/sqlcgen/workspace_engine.sql.go | 280 ++++++++++++++++++ apps/workspace-engine/sqlc.yaml | 2 - apps/workspace-engine/sqlc/README.md | 13 +- 8 files changed, 410 insertions(+), 23 deletions(-) create mode 100644 apps/workspace-engine/pkg/db/sqlcgen/db.go create mode 100644 apps/workspace-engine/pkg/db/sqlcgen/models.go create mode 100644 apps/workspace-engine/pkg/db/sqlcgen/querier.go create mode 100644 apps/workspace-engine/pkg/db/sqlcgen/workspace_engine.sql.go diff --git a/apps/workspace-engine/Makefile b/apps/workspace-engine/Makefile index aca158430..c4fe8f2df 100644 --- a/apps/workspace-engine/Makefile +++ b/apps/workspace-engine/Makefile @@ -86,17 +86,9 @@ sqlc-compile: $$SQLC_BIN compile -f sqlc.yaml sqlc-verify: - @echo "Verifying sqlc queries against live database..." - @if [ -z "$$POSTGRES_URL" ]; then \ - echo "POSTGRES_URL is required for sqlc verify"; \ - exit 1; \ - fi - @SQLC_BIN=$$(go env GOPATH)/bin/sqlc; \ - if [ ! -x "$$SQLC_BIN" ]; then \ - echo "sqlc not found. Installing..."; \ - $(GO) install github.com/sqlc-dev/sqlc/cmd/sqlc@latest; \ - fi; \ - $$SQLC_BIN verify -f sqlc.yaml + @$(MAKE) sqlc-compile + @echo "sqlc verify uses sqlc Cloud project settings." + @echo "Run '$$(go env GOPATH)/bin/sqlc verify -f sqlc.yaml' after configuring sqlc Cloud." # Format code fmt: @@ -139,7 +131,7 @@ help: @echo " make sqlc-install - Install sqlc CLI" @echo " make sqlc-generate - Generate typed Go query code from sqlc files" @echo " make sqlc-compile - Validate sqlc config, schema, and queries" - @echo " make sqlc-verify - Verify queries against a live DB (POSTGRES_URL required)" + @echo " make sqlc-verify - Run local compile checks and show sqlc Cloud verify hint" @echo " make fmt - Format code" @echo " make clean - Clean build artifacts" @echo " make install-tools - Install development tools" diff --git a/apps/workspace-engine/README.md b/apps/workspace-engine/README.md index ce9cd6b38..28577bf79 100644 --- a/apps/workspace-engine/README.md +++ b/apps/workspace-engine/README.md @@ -87,7 +87,7 @@ make test-coverage # Run tests with coverage report make lint # Run linter make sqlc-generate # Generate typed Go query code from sqlc files make sqlc-compile # Validate sqlc config, schema, and queries -make sqlc-verify # Verify queries against live DB (POSTGRES_URL required) +make sqlc-verify # Run local compile checks and show sqlc Cloud verify hint make fmt # Format code make clean # Clean build artifacts make install-tools # Install development tools diff --git a/apps/workspace-engine/pkg/db/sqlcgen/db.go b/apps/workspace-engine/pkg/db/sqlcgen/db.go new file mode 100644 index 000000000..10dff7bbb --- /dev/null +++ b/apps/workspace-engine/pkg/db/sqlcgen/db.go @@ -0,0 +1,32 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.30.0 + +package sqlcgen + +import ( + "context" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" +) + +type DBTX interface { + Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error) + Query(context.Context, string, ...interface{}) (pgx.Rows, error) + QueryRow(context.Context, string, ...interface{}) pgx.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx pgx.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/apps/workspace-engine/pkg/db/sqlcgen/models.go b/apps/workspace-engine/pkg/db/sqlcgen/models.go new file mode 100644 index 000000000..b2ad3bc08 --- /dev/null +++ b/apps/workspace-engine/pkg/db/sqlcgen/models.go @@ -0,0 +1,66 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.30.0 + +package sqlcgen + +import ( + "github.com/jackc/pgx/v5/pgtype" +) + +type Deployment struct { + ID pgtype.UUID `json:"id"` + Name string `json:"name"` + Slug string `json:"slug"` + Description string `json:"description"` + SystemID pgtype.UUID `json:"system_id"` + JobAgentID pgtype.UUID `json:"job_agent_id"` + JobAgentConfig []byte `json:"job_agent_config"` + RetryCount int32 `json:"retry_count"` + Timeout pgtype.Int4 `json:"timeout"` + ResourceSelector []byte `json:"resource_selector"` +} + +type Environment struct { + ID pgtype.UUID `json:"id"` + SystemID pgtype.UUID `json:"system_id"` + Name string `json:"name"` + CreatedAt pgtype.Timestamptz `json:"created_at"` + Description pgtype.Text `json:"description"` + ResourceSelector []byte `json:"resource_selector"` +} + +type JobAgent struct { + ID pgtype.UUID `json:"id"` + WorkspaceID pgtype.UUID `json:"workspace_id"` + Name string `json:"name"` + Type string `json:"type"` + Config []byte `json:"config"` +} + +type ReleaseTarget struct { + ID pgtype.UUID `json:"id"` + ResourceID pgtype.UUID `json:"resource_id"` + EnvironmentID pgtype.UUID `json:"environment_id"` + DeploymentID pgtype.UUID `json:"deployment_id"` +} + +type Resource struct { + ID pgtype.UUID `json:"id"` + WorkspaceID pgtype.UUID `json:"workspace_id"` +} + +type System struct { + ID pgtype.UUID `json:"id"` + WorkspaceID pgtype.UUID `json:"workspace_id"` + Name string `json:"name"` + Slug string `json:"slug"` + Description string `json:"description"` +} + +type Workspace struct { + ID pgtype.UUID `json:"id"` + Name string `json:"name"` + Slug string `json:"slug"` + CreatedAt pgtype.Timestamptz `json:"created_at"` +} diff --git a/apps/workspace-engine/pkg/db/sqlcgen/querier.go b/apps/workspace-engine/pkg/db/sqlcgen/querier.go new file mode 100644 index 000000000..a6c05af24 --- /dev/null +++ b/apps/workspace-engine/pkg/db/sqlcgen/querier.go @@ -0,0 +1,22 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.30.0 + +package sqlcgen + +import ( + "context" + + "github.com/jackc/pgx/v5/pgtype" +) + +type Querier interface { + GetDeploymentByID(ctx context.Context, id pgtype.UUID) (Deployment, error) + GetReleaseTargetID(ctx context.Context, arg GetReleaseTargetIDParams) (pgtype.UUID, error) + InsertReleaseTargetIfMissing(ctx context.Context, arg InsertReleaseTargetIfMissingParams) error + ListDeploymentsByWorkspace(ctx context.Context, workspaceID pgtype.UUID) ([]Deployment, error) + ListEnvironmentsByWorkspace(ctx context.Context, workspaceID pgtype.UUID) ([]ListEnvironmentsByWorkspaceRow, error) + UpsertDeployment(ctx context.Context, arg UpsertDeploymentParams) (Deployment, error) +} + +var _ Querier = (*Queries)(nil) diff --git a/apps/workspace-engine/pkg/db/sqlcgen/workspace_engine.sql.go b/apps/workspace-engine/pkg/db/sqlcgen/workspace_engine.sql.go new file mode 100644 index 000000000..47c482d50 --- /dev/null +++ b/apps/workspace-engine/pkg/db/sqlcgen/workspace_engine.sql.go @@ -0,0 +1,280 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.30.0 +// source: workspace_engine.sql + +package sqlcgen + +import ( + "context" + + "github.com/jackc/pgx/v5/pgtype" +) + +const getDeploymentByID = `-- name: GetDeploymentByID :one +SELECT + d.id, + d.name, + d.slug, + d.description, + d.system_id, + d.job_agent_id, + d.job_agent_config, + d.retry_count, + d.timeout, + d.resource_selector +FROM deployment AS d +WHERE d.id = $1 +LIMIT 1 +` + +func (q *Queries) GetDeploymentByID(ctx context.Context, id pgtype.UUID) (Deployment, error) { + row := q.db.QueryRow(ctx, getDeploymentByID, id) + var i Deployment + err := row.Scan( + &i.ID, + &i.Name, + &i.Slug, + &i.Description, + &i.SystemID, + &i.JobAgentID, + &i.JobAgentConfig, + &i.RetryCount, + &i.Timeout, + &i.ResourceSelector, + ) + return i, err +} + +const getReleaseTargetID = `-- name: GetReleaseTargetID :one +SELECT + rt.id +FROM release_target AS rt +WHERE rt.resource_id = $1 + AND rt.environment_id = $2 + AND rt.deployment_id = $3 +LIMIT 1 +` + +type GetReleaseTargetIDParams struct { + ResourceID pgtype.UUID `json:"resource_id"` + EnvironmentID pgtype.UUID `json:"environment_id"` + DeploymentID pgtype.UUID `json:"deployment_id"` +} + +func (q *Queries) GetReleaseTargetID(ctx context.Context, arg GetReleaseTargetIDParams) (pgtype.UUID, error) { + row := q.db.QueryRow(ctx, getReleaseTargetID, arg.ResourceID, arg.EnvironmentID, arg.DeploymentID) + var id pgtype.UUID + err := row.Scan(&id) + return id, err +} + +const insertReleaseTargetIfMissing = `-- name: InsertReleaseTargetIfMissing :exec +INSERT INTO release_target (resource_id, environment_id, deployment_id) +VALUES ($1, $2, $3) +ON CONFLICT (resource_id, environment_id, deployment_id) +DO NOTHING +` + +type InsertReleaseTargetIfMissingParams struct { + ResourceID pgtype.UUID `json:"resource_id"` + EnvironmentID pgtype.UUID `json:"environment_id"` + DeploymentID pgtype.UUID `json:"deployment_id"` +} + +func (q *Queries) InsertReleaseTargetIfMissing(ctx context.Context, arg InsertReleaseTargetIfMissingParams) error { + _, err := q.db.Exec(ctx, insertReleaseTargetIfMissing, arg.ResourceID, arg.EnvironmentID, arg.DeploymentID) + return err +} + +const listDeploymentsByWorkspace = `-- name: ListDeploymentsByWorkspace :many +SELECT + d.id, + d.name, + d.slug, + d.description, + d.system_id, + d.job_agent_id, + d.job_agent_config, + d.retry_count, + d.timeout, + d.resource_selector +FROM deployment AS d +INNER JOIN system AS s + ON s.id = d.system_id +WHERE s.workspace_id = $1 +ORDER BY d.name ASC +` + +func (q *Queries) ListDeploymentsByWorkspace(ctx context.Context, workspaceID pgtype.UUID) ([]Deployment, error) { + rows, err := q.db.Query(ctx, listDeploymentsByWorkspace, workspaceID) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Deployment + for rows.Next() { + var i Deployment + if err := rows.Scan( + &i.ID, + &i.Name, + &i.Slug, + &i.Description, + &i.SystemID, + &i.JobAgentID, + &i.JobAgentConfig, + &i.RetryCount, + &i.Timeout, + &i.ResourceSelector, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const listEnvironmentsByWorkspace = `-- name: ListEnvironmentsByWorkspace :many +SELECT + e.id, + e.name, + e.system_id, + e.created_at, + e.description, + e.resource_selector +FROM environment AS e +INNER JOIN system AS s + ON s.id = e.system_id +WHERE s.workspace_id = $1 +ORDER BY e.name ASC +` + +type ListEnvironmentsByWorkspaceRow struct { + ID pgtype.UUID `json:"id"` + Name string `json:"name"` + SystemID pgtype.UUID `json:"system_id"` + CreatedAt pgtype.Timestamptz `json:"created_at"` + Description pgtype.Text `json:"description"` + ResourceSelector []byte `json:"resource_selector"` +} + +func (q *Queries) ListEnvironmentsByWorkspace(ctx context.Context, workspaceID pgtype.UUID) ([]ListEnvironmentsByWorkspaceRow, error) { + rows, err := q.db.Query(ctx, listEnvironmentsByWorkspace, workspaceID) + if err != nil { + return nil, err + } + defer rows.Close() + var items []ListEnvironmentsByWorkspaceRow + for rows.Next() { + var i ListEnvironmentsByWorkspaceRow + if err := rows.Scan( + &i.ID, + &i.Name, + &i.SystemID, + &i.CreatedAt, + &i.Description, + &i.ResourceSelector, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const upsertDeployment = `-- name: UpsertDeployment :one +INSERT INTO deployment ( + id, + name, + slug, + description, + system_id, + job_agent_id, + job_agent_config, + retry_count, + timeout, + resource_selector +) +VALUES ( + $1, + $2, + $3, + $4, + $5, + $6, + $7, + $8, + $9, + $10 +) +ON CONFLICT (id) +DO UPDATE SET + name = EXCLUDED.name, + slug = EXCLUDED.slug, + description = EXCLUDED.description, + system_id = EXCLUDED.system_id, + job_agent_id = EXCLUDED.job_agent_id, + job_agent_config = EXCLUDED.job_agent_config, + retry_count = EXCLUDED.retry_count, + timeout = EXCLUDED.timeout, + resource_selector = EXCLUDED.resource_selector +RETURNING + id, + name, + slug, + description, + system_id, + job_agent_id, + job_agent_config, + retry_count, + timeout, + resource_selector +` + +type UpsertDeploymentParams struct { + ID pgtype.UUID `json:"id"` + Name string `json:"name"` + Slug string `json:"slug"` + Description string `json:"description"` + SystemID pgtype.UUID `json:"system_id"` + JobAgentID pgtype.UUID `json:"job_agent_id"` + JobAgentConfig []byte `json:"job_agent_config"` + RetryCount int32 `json:"retry_count"` + Timeout pgtype.Int4 `json:"timeout"` + ResourceSelector []byte `json:"resource_selector"` +} + +func (q *Queries) UpsertDeployment(ctx context.Context, arg UpsertDeploymentParams) (Deployment, error) { + row := q.db.QueryRow(ctx, upsertDeployment, + arg.ID, + arg.Name, + arg.Slug, + arg.Description, + arg.SystemID, + arg.JobAgentID, + arg.JobAgentConfig, + arg.RetryCount, + arg.Timeout, + arg.ResourceSelector, + ) + var i Deployment + err := row.Scan( + &i.ID, + &i.Name, + &i.Slug, + &i.Description, + &i.SystemID, + &i.JobAgentID, + &i.JobAgentConfig, + &i.RetryCount, + &i.Timeout, + &i.ResourceSelector, + ) + return i, err +} diff --git a/apps/workspace-engine/sqlc.yaml b/apps/workspace-engine/sqlc.yaml index b5fae04c2..d9cef3179 100644 --- a/apps/workspace-engine/sqlc.yaml +++ b/apps/workspace-engine/sqlc.yaml @@ -3,8 +3,6 @@ sql: - engine: "postgresql" queries: "sqlc/queries" schema: "sqlc/schema/workspace_engine.sql" - database: - uri: "${POSTGRES_URL}" gen: go: package: "sqlcgen" diff --git a/apps/workspace-engine/sqlc/README.md b/apps/workspace-engine/sqlc/README.md index 230acc3ef..810b42f8d 100644 --- a/apps/workspace-engine/sqlc/README.md +++ b/apps/workspace-engine/sqlc/README.md @@ -39,16 +39,13 @@ make sqlc-compile The `schema/workspace_engine.sql` file is a stable schema snapshot for sqlc. When Drizzle migrations change tables used by sqlc queries, update this snapshot. -## Verify against a live database +## Verification modes -`sqlc generate` uses the `schema` path and does **not** need a database connection. +`sqlc generate` and `sqlc compile` use the local `schema` file and do **not** need +a database connection. -`database.uri` is used for database-backed checks (for example `sqlc verify`). - -```bash -POSTGRES_URL="postgres://postgres:password@localhost:5432/postgres?sslmode=disable" \ - make sqlc-verify -``` +`sqlc verify` is a sqlc Cloud workflow and requires cloud project configuration. +`make sqlc-verify` runs local compile checks and prints the cloud verify command. ## Adding new queries