From 1ea18a638bda49800b64634db2cc92daeef575ea Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Tue, 12 Dec 2023 13:10:04 -0600 Subject: [PATCH 1/6] chore: add sqlc-vet to github actions for validating queries --- .github/workflows/ci.yaml | 24 ++++++++++++++++++++++++ Makefile | 5 +++++ coderd/database/sqlc.yaml | 6 ++++++ 3 files changed, 35 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index dee7fbabdba57..e4c1b2e233a98 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -908,3 +908,27 @@ jobs: echo "::endgroup::" done + + # sqlc-vet runs a postgres docker container, runs Coder migrations, and then + # runs sqlc-vet to ensure all queries are valid. This catches any mistakes + # in migrations or sqlc queries that makes a query unable to be prepared. + sqlc-vet: + runs-on: ${{ github.repository_owner == 'coder' && 'buildjet-8vcpu-ubuntu-2204' || 'ubuntu-latest' }} + needs: changes + # TODO: We should do this on any migration or query changes. +# if: needs.changes.outputs.go == 'true' || needs.changes.outputs.ci == 'true' || github.ref == 'refs/heads/main' + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 1 + # We need golang to run the migration main.go + - name: Setup Go + uses: ./.github/actions/setup-go + + - name: Setup sqlc + uses: ./.github/actions/setup-sqlc + + - name: Setup and run sqlc vet + run: | + make sqlc-vet diff --git a/Makefile b/Makefile index 1a94d3a33b8dc..6d547aa7fce67 100644 --- a/Makefile +++ b/Makefile @@ -708,6 +708,11 @@ test: gotestsum --format standard-quiet -- -v -short -count=1 ./... .PHONY: test +sqlc-vet: test-postgres-docker + echo "--- sqlc vet" + SQLC_DATABASE_URL="postgresql://postgres:postgres@localhost:5432/$(shell go run scripts/migrate-ci/main.go)" \ + sqlc vet -f coderd/database/sqlc.yaml + # When updating -timeout for this test, keep in sync with # test-go-postgres (.github/workflows/coder.yaml). # Do add coverage flags so that test caching works. diff --git a/coderd/database/sqlc.yaml b/coderd/database/sqlc.yaml index 956485311e4a1..e8a4724823d4f 100644 --- a/coderd/database/sqlc.yaml +++ b/coderd/database/sqlc.yaml @@ -82,6 +82,12 @@ sql: - schema: "./dump.sql" queries: "./queries" engine: "postgresql" + # This only works if you are running a local postgres database with the + # schema loaded and migrations run. + database: + uri: "${SQLC_DATABASE_URL}" + rules: + - sqlc/db-prepare gen: go: package: "database" From 845794b05187ef60acec104276a7a47dbb90d829 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Tue, 12 Dec 2023 13:16:06 -0600 Subject: [PATCH 2/6] fmt --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index e4c1b2e233a98..92e7b9fe75e8b 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -916,7 +916,7 @@ jobs: runs-on: ${{ github.repository_owner == 'coder' && 'buildjet-8vcpu-ubuntu-2204' || 'ubuntu-latest' }} needs: changes # TODO: We should do this on any migration or query changes. -# if: needs.changes.outputs.go == 'true' || needs.changes.outputs.ci == 'true' || github.ref == 'refs/heads/main' + # if: needs.changes.outputs.go == 'true' || needs.changes.outputs.ci == 'true' || github.ref == 'refs/heads/main' steps: - name: Checkout uses: actions/checkout@v4 From 6ac03022a25c3a95469c76efc4397a5b7e735db6 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Tue, 12 Dec 2023 13:19:04 -0600 Subject: [PATCH 3/6] Trying some different deps --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 92e7b9fe75e8b..2dcd0708686a1 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -315,7 +315,7 @@ jobs: test-go-pg: runs-on: ${{ github.repository_owner == 'coder' && 'buildjet-8vcpu-ubuntu-2204' || 'ubuntu-latest' }} - needs: changes + needs: sqlc-vet if: needs.changes.outputs.go == 'true' || needs.changes.outputs.ci == 'true' || github.ref == 'refs/heads/main' # This timeout must be greater than the timeout set by `go test` in # `make test-postgres` to ensure we receive a trace of running From 93ef5f0a6ecb97174de22b19e0f6eb8cde33423f Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Tue, 12 Dec 2023 15:05:32 -0600 Subject: [PATCH 4/6] Only run sqlc-vet when required by changes --- .github/workflows/ci.yaml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 2dcd0708686a1..17f46695017ca 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -36,6 +36,7 @@ jobs: ts: ${{ steps.filter.outputs.ts }} k8s: ${{ steps.filter.outputs.k8s }} ci: ${{ steps.filter.outputs.ci }} + db: ${{ steps.filter.outputs.db }} offlinedocs-only: ${{ steps.filter.outputs.offlinedocs_count == steps.filter.outputs.all_count }} offlinedocs: ${{ steps.filter.outputs.offlinedocs }} steps: @@ -57,6 +58,12 @@ jobs: - "examples/web-server/**" - "examples/monitoring/**" - "examples/lima/**" + db: + - "**.sql" + - "coderd/database/queries/**" + - "coderd/database/migrations" + - "coderd/database/sqlc.yaml" + - "coderd/database/dump.sql" go: - "**.sql" - "**.go" @@ -315,7 +322,7 @@ jobs: test-go-pg: runs-on: ${{ github.repository_owner == 'coder' && 'buildjet-8vcpu-ubuntu-2204' || 'ubuntu-latest' }} - needs: sqlc-vet + needs: changes if: needs.changes.outputs.go == 'true' || needs.changes.outputs.ci == 'true' || github.ref == 'refs/heads/main' # This timeout must be greater than the timeout set by `go test` in # `make test-postgres` to ensure we receive a trace of running @@ -915,8 +922,7 @@ jobs: sqlc-vet: runs-on: ${{ github.repository_owner == 'coder' && 'buildjet-8vcpu-ubuntu-2204' || 'ubuntu-latest' }} needs: changes - # TODO: We should do this on any migration or query changes. - # if: needs.changes.outputs.go == 'true' || needs.changes.outputs.ci == 'true' || github.ref == 'refs/heads/main' + if: needs.changes.outputs.db == 'true' || github.ref == 'refs/heads/main' steps: - name: Checkout uses: actions/checkout@v4 From 3c4a8183c37d43b6718cf0970c2f7f99341a9d1d Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Tue, 12 Dec 2023 15:08:08 -0600 Subject: [PATCH 5/6] require sqlc-vet --- .github/workflows/ci.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 17f46695017ca..ff2b54e45d199 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -625,6 +625,7 @@ jobs: - test-js - test-e2e - offlinedocs + - sqlc-vet # Allow this job to run even if the needed jobs fail, are skipped or # cancelled. if: always() From 2dd66a9ee416cc82553a67a1c17c418f3efe3fd2 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Tue, 12 Dec 2023 15:16:10 -0600 Subject: [PATCH 6/6] print a success message on vet --- Makefile | 2 +- coderd/database/sqlc.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 6d547aa7fce67..3ef85ea1beb0f 100644 --- a/Makefile +++ b/Makefile @@ -711,7 +711,7 @@ test: sqlc-vet: test-postgres-docker echo "--- sqlc vet" SQLC_DATABASE_URL="postgresql://postgres:postgres@localhost:5432/$(shell go run scripts/migrate-ci/main.go)" \ - sqlc vet -f coderd/database/sqlc.yaml + sqlc vet -f coderd/database/sqlc.yaml && echo "Passed sqlc vet" # When updating -timeout for this test, keep in sync with # test-go-postgres (.github/workflows/coder.yaml). diff --git a/coderd/database/sqlc.yaml b/coderd/database/sqlc.yaml index e8a4724823d4f..2ed67c3ac1434 100644 --- a/coderd/database/sqlc.yaml +++ b/coderd/database/sqlc.yaml @@ -83,7 +83,7 @@ sql: queries: "./queries" engine: "postgresql" # This only works if you are running a local postgres database with the - # schema loaded and migrations run. + # schema loaded and migrations run. Run `make sqlc-vet` to run the linter. database: uri: "${SQLC_DATABASE_URL}" rules: