|
| 1 | +# Suggested CI/CD setup |
| 2 | + |
| 3 | +If your project has more than a single developer, we suggest running `sqlc` as |
| 4 | +part of your CI/CD pipeline. The two commands you'll want to run are `diff` and `vet` |
| 5 | + |
| 6 | +`sqlc diff` ensures that code is up to date. New developers to a project may |
| 7 | +forget to run `sqlc generate`. They also might edit generated code. `diff` will |
| 8 | +catch both scenarios. |
| 9 | + |
| 10 | +```diff |
| 11 | +% sqlc-dev diff |
| 12 | +--- a/postgresql/query.sql.go |
| 13 | ++++ b/postgresql/query.sql.go |
| 14 | +@@ -55,7 +55,7 @@ |
| 15 | + |
| 16 | + const listAuthors = `-- name: ListAuthors :many |
| 17 | + SELECT id, name, bio FROM authors |
| 18 | +-ORDER BY name |
| 19 | ++ORDER BY bio |
| 20 | + ` |
| 21 | +``` |
| 22 | + |
| 23 | +`sqlc vet` runs a set of lint checks against your SQL queries. These checks are |
| 24 | +helpful in catching anti-patterns before they make it into production. Please |
| 25 | +see the [vet](vet.md) documentation for a complete guide on adding checks to your |
| 26 | +project. |
| 27 | + |
| 28 | +## General setup |
| 29 | + |
| 30 | +Install `sqlc` using the [suggested instructions](../overview/install). |
| 31 | + |
| 32 | +Create two steps in your pipelines, one for `sqlc diff`and one for `sqlc vet`. |
| 33 | + |
| 34 | +## GitHub Actions |
| 35 | + |
| 36 | +We provide the [setup-sqlc](https://github.com/marketplace/actions/setup-sqlc) |
| 37 | +GitHub Action to install `sqlc`. The action uses the built-in |
| 38 | +[tool-cache](https://github.com/actions/toolkit/blob/main/packages/tool-cache/README.md) |
| 39 | +to speed up the installation process. |
| 40 | + |
| 41 | +The following workflow runs `sqlc diff` on every push. |
| 42 | + |
| 43 | +```yaml |
| 44 | +name: sqlc |
| 45 | +on: [push] |
| 46 | +jobs: |
| 47 | + diff: |
| 48 | + runs-on: ubuntu-latest |
| 49 | + steps: |
| 50 | + - uses: actions/checkout@v3 |
| 51 | + - uses: sqlc-dev/setup-sqlc@v3 |
| 52 | + with: |
| 53 | + sqlc-version: '1.19.0' |
| 54 | + - run: sqlc diff |
| 55 | +``` |
| 56 | +
|
| 57 | +We also encourage running [`sqlc vet`](vet.md). To get the most value out of |
| 58 | +`vet`, you'll want to set up a running database. |
| 59 | + |
| 60 | +```yaml |
| 61 | +name: sqlc |
| 62 | +on: [push] |
| 63 | +jobs: |
| 64 | + vet: |
| 65 | + runs-on: ubuntu-latest |
| 66 | + services: |
| 67 | + postgres: |
| 68 | + image: "postgres:15" |
| 69 | + env: |
| 70 | + POSTGRES_DB: postgres |
| 71 | + POSTGRES_PASSWORD: postgres |
| 72 | + POSTGRES_USER: postgres |
| 73 | + ports: |
| 74 | + - 5432:5432 |
| 75 | + # needed because the postgres container does not provide a healthcheck |
| 76 | + options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 |
| 77 | + env: |
| 78 | + PG_PORT: ${{ job.services.postgres.ports['5432'] }} |
| 79 | +
|
| 80 | + steps: |
| 81 | + - uses: actions/checkout@v3 |
| 82 | + - uses: sqlc-dev/setup-sqlc@v3 |
| 83 | + with: |
| 84 | + sqlc-version: '1.19.0' |
| 85 | + # Connect and migrate your database here. This is an example which runs |
| 86 | + # commands from a `schema.sql` file. |
| 87 | + - run: psql -h localhost -U postgres -p $PG_PORT -d postgres -f schema.sql |
| 88 | + env: |
| 89 | + PGPASSWORD: postgres |
| 90 | + - run: sqlc vet |
| 91 | +``` |
0 commit comments