Skip to content

Commit efa6acf

Browse files
authored
docs: various readability improvements (#1854)
1 parent 1a28c29 commit efa6acf

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

docs/howto/ddl.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Modifying the database schema
22

3-
sqlc understands `ALTER TABLE` statements when parsing SQL.
3+
sqlc parses `CREATE TABLE` and `ALTER TABLE` statements in order to generate
4+
the necessary code.
45

56
```sql
67
CREATE TABLE authors (
@@ -24,8 +25,11 @@ type Writer struct {
2425

2526
## Handling SQL migrations
2627

27-
sqlc will ignore rollback statements when parsing migration SQL files. The
28-
following tools are current supported:
28+
sqlc does not perform database migrations for you. However, sqlc is able to
29+
differentiate between up and down migrations. sqlc ignores down migrations when
30+
parsing SQL files.
31+
32+
sqlc supports parsing migrations from the following tools:
2933

3034
- [dbmate](https://github.com/amacneil/dbmate)
3135
- [golang-migrate](https://github.com/golang-migrate/migrate)
@@ -98,17 +102,26 @@ type Comment struct {
98102

99103
### golang-migrate
100104

101-
Warning: [golang-migrate specifies](https://github.com/golang-migrate/migrate/blob/master/MIGRATIONS.md#migration-filename-format) that the version number in the migration file name is to be interpreted numerically. However, sqlc executes the migration files in **lexicographic** order. If you choose to simply enumerate your migration versions, make sure to prepend enough zeros to the version number to avoid any unexpected behavior.
105+
**Warning:**
106+
[golang-migrate interprets](https://github.com/golang-migrate/migrate/blob/master/MIGRATIONS.md#migration-filename-format)
107+
migration filenames numerically. However, sqlc parses migration files in
108+
lexicographic order. If you choose to have sqlc enumerate your migration files,
109+
make sure their numeric ordering matches their lexicographic ordering to avoid
110+
unexpected behavior. This can be done by prepending enough zeroes to the
111+
migration filenames.
112+
113+
This doesn't work as intended.
102114

103-
Probably doesn't work as intended:
104115
```
105116
1_initial.up.sql
106117
...
107118
9_foo.up.sql
108-
# this migration file will be executed BEFORE 9_foo
119+
# this migration file will be parsed BEFORE 9_foo
109120
10_bar.up.sql
110121
```
111-
Works as was probably intended:
122+
123+
This worked as intended.
124+
112125
```
113126
001_initial.up.sql
114127
...

docs/reference/config.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Each mapping in the `sql` collection has the following keys:
3535
- `queries`:
3636
- Directory of SQL queries or path to single SQL file; or a list of paths.
3737
- `gen`:
38-
- A mapping to configure built-in code generators. Supports the following keys:
38+
- A mapping to configure built-in code generators. See [gen](#gen) for the supported keys.
3939
- `strict_function_checks`
4040
- If true, return an error if a called SQL function does not exist. Defaults to `false`.
4141

docs/tutorials/getting-started-postgresql.md

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@ directory. In our new directory, create a file named `sqlc.yaml` with the
1616
following contents:
1717

1818
```yaml
19-
version: 1
20-
packages:
21-
- path: "tutorial"
22-
name: "tutorial"
23-
engine: "postgresql"
24-
schema: "schema.sql"
19+
version: "2"
20+
sql:
21+
- engine: "postgresql"
2522
queries: "query.sql"
23+
schema: "schema.sql"
24+
gen:
25+
go:
26+
package: "tutorial"
27+
out: "tutorial"
2628
```
2729
28-
sqlc needs to know your database schema and queries. In the same directory,
29-
create a file named `schema.sql` with the following contents:
30+
sqlc needs to know your database schema and queries in order to generate code.
31+
In the same directory, create a file named `schema.sql` with the following
32+
content:
3033

3134
```sql
3235
CREATE TABLE authors (
@@ -60,25 +63,29 @@ DELETE FROM authors
6063
WHERE id = $1;
6164
```
6265

63-
For SQL UPDATE if you do not want to return the updated record to the user, add this to the `query.sql` file:
66+
If you **do not** want your SQL `UPDATE` queries to return the updated record
67+
to the user, add this to `query.sql`:
68+
6469
```sql
6570
-- name: UpdateAuthor :exec
6671
UPDATE authors
67-
set name = $2,
68-
bio = $3
72+
set name = $2,
73+
bio = $3
6974
WHERE id = $1;
7075
```
71-
Otherwise, to return the updated record back to the user, add this to the `query.sql` file:
76+
77+
Otherwise, to return the updated record to the user, add this to `query.sql`:
78+
7279
```sql
7380
-- name: UpdateAuthor :one
7481
UPDATE authors
75-
set name = $2,
76-
bio = $3
82+
set name = $2,
83+
bio = $3
7784
WHERE id = $1
7885
RETURNING *;
7986
```
8087

81-
You are now ready to generate code. Run the `generate` command. You shouldn't see any errors or output.
88+
You are now ready to generate code. You shouldn't see any errors or output.
8289

8390
```shell
8491
sqlc generate
@@ -165,5 +172,6 @@ go get github.com/lib/pq
165172
go build ./...
166173
```
167174

168-
To make that possible, sqlc generates readable, **idiomatic** Go code that you
169-
otherwise would have had to write yourself. Take a look in `tutorial/query.sql.go`.
175+
sqlc generates readable, **idiomatic** Go code that you otherwise would have
176+
had to write yourself. Take a look in the `tutorial` package to see what code
177+
sqlc generated.

0 commit comments

Comments
 (0)