|
7 | 7 | Rules are defined in the `sqlc` [configuration](../reference/config) file. They consist |
8 | 8 | of a name, message, and a [Common Expression Language (CEL)](https://github.com/google/cel-spec) |
9 | 9 | expression. Expressions are evaluated using [cel-go](https://github.com/google/cel-go). |
10 | | -If an expression evaluates to `true`, an error is reported using the given message. |
| 10 | +If an expression evaluates to `true`, `sqlc vet` will report an error using the given message. |
11 | 11 |
|
12 | | -Each expression has access to variables from your sqlc configuration and queries, |
13 | | -defined in the following struct: |
| 12 | +## Defining lint rules |
| 13 | + |
| 14 | +Each lint rule's CEL expression has access to variables from your sqlc configuration and queries, |
| 15 | +defined in the following struct. |
14 | 16 |
|
15 | 17 | ```proto |
16 | 18 | message Config |
@@ -109,4 +111,47 @@ example](https://github.com/kyleconroy/sqlc/blob/main/examples/authors/sqlc.yaml |
109 | 111 |
|
110 | 112 | Please note that `sqlc` does not manage or migrate your database. Use your |
111 | 113 | migration tool of choice to create the necessary database tables and objects |
112 | | -before running `sqlc vet`. |
| 114 | +before running `sqlc vet` with the `sqlc/db-prepare` rule. |
| 115 | + |
| 116 | +## Running lint rules |
| 117 | + |
| 118 | +When you add the name of a defined rule to the rules list |
| 119 | +for a [sql package](https://docs.sqlc.dev/en/stable/reference/config.html#sql), |
| 120 | +`sqlc vet` will evaluate that rule against every query in the package. |
| 121 | + |
| 122 | +In the example below, two rules are defined but only one is enabled. |
| 123 | + |
| 124 | +```yaml |
| 125 | +version: 2 |
| 126 | +sql: |
| 127 | + - schema: "query.sql" |
| 128 | + queries: "query.sql" |
| 129 | + engine: "postgresql" |
| 130 | + gen: |
| 131 | + go: |
| 132 | + package: "authors" |
| 133 | + out: "db" |
| 134 | + rules: |
| 135 | + - no-delete |
| 136 | +rules: |
| 137 | + - name: no-pg |
| 138 | + message: "invalid engine: postgresql" |
| 139 | + rule: | |
| 140 | + config.engine == "postgresql" |
| 141 | + - name: no-delete |
| 142 | + message: "don't use delete statements" |
| 143 | + rule: | |
| 144 | + query.sql.contains("DELETE") |
| 145 | +``` |
| 146 | + |
| 147 | +### Opting-out of lint rules |
| 148 | + |
| 149 | +For any query, you can tell `sqlc vet` not to evaluate lint rules using the |
| 150 | +`@sqlc-vet-disable` query annotation. |
| 151 | + |
| 152 | +```sql |
| 153 | +/* name: GetAuthor :one */ |
| 154 | +/* @sqlc-vet-disable */ |
| 155 | +SELECT * FROM authors |
| 156 | +WHERE id = ? LIMIT 1; |
| 157 | +``` |
0 commit comments