Skip to content

Commit

Permalink
docs: Update managed databases doc to discuss codegen (#2897)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmbenton authored Oct 20, 2023
1 parent 01947a6 commit 0d32dbe
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 19 deletions.
90 changes: 76 additions & 14 deletions docs/howto/managed-databases.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@ Managed databases are powered by [sqlc Cloud](https://dashboard.sqlc.dev). Sign

*Added in v1.22.0*

`sqlc` can create and maintain hosted databases for your project. These
databases are immediately useful for linting queries with [`sqlc vet`](vet.md)
if your lint rules require a connection to a running database. PostgreSQL
support is available today, with MySQL on the way.

This feature is under active development, and we're interested in supporting
other use-cases. Beyond linting queries, you can use sqlc managed databases
in your tests to quickly stand up a database per test suite or even per test,
`sqlc` can create and maintain short-lived hosted databases for your project.
These ephemeral databases are immediately useful for powering sqlc's
database-connected query analyzer, an opt-in feature that improves upon sqlc's
built-in query analysis engine. PostgreSQL support is available today, with
MySQL on the way.

Once configured, `sqlc` will also use managed databases when linting queries
with [`sqlc vet`](vet.html) in cases where your lint rules require a connection
to a running database.

Managed databases are under active development, and we're interested in
supporting other use-cases. Outside of sqlc itself, you can use our managed
databases in your tests to quickly stand up a database per test suite or even per test,
providing a real, isolated database for a test run. No cleanup required.

## Configuring managed databases

To configure `sqlc` to use a managed database, remove the `uri` key from your
To configure `sqlc` to use managed databases, remove the `uri` key from your
`database` configuration and replace it with the `managed` key set to `true`.
Set the `project` key in your `cloud` configuration to the value of your
project ID, obtained via the [dashboard](https://dashboard.sqlc.dev).
Expand All @@ -35,7 +40,7 @@ sql:
managed: true
```
## Authentication
### Authentication
`sqlc` expects to find a valid auth token in the value of the `SQLC_AUTH_TOKEN`
environment variable. You can create an auth token via the [dashboard](https://dashboard.sqlc.dev).
Expand All @@ -44,13 +49,38 @@ environment variable. You can create an auth token via the [dashboard](https://d
export SQLC_AUTH_TOKEN=sqlc_xxxxxxxx
```

## Improving codegen

Without a database connection, sqlc does its best to parse, analyze and compile your queries just using
the schema you pass it and what it knows about the various database engines it supports. In many cases
this works just fine, but for more advanced queries sqlc might not have enough information to produce good code.

With managed databases configured, `sqlc generate` will automatically create a hosted ephemeral database with your
schema and use that database to improve its query analysis. And sqlc will cache its analysis locally
on a per-query basis to speed up future codegen runs. Here's a minimal working configuration:

```yaml
version: '2'
cloud:
project: '<PROJECT_ID>'
sql:
- schema: schema.sql
queries: query.sql
engine: postgresql
database:
managed: true
gen:
go:
out: "db"
```

## Linting queries

With managed databases configured, `sqlc vet` will create a database with your
package's schema and use that database when running lint rules that require a
database connection, e.g. any [rule relying on `EXPLAIN ...` output](vet.md#rules-using-explain-output).
With managed databases configured, `sqlc vet` will automatically create a hosted ephemeral database with your
schema and use that database when running lint rules that require a
database connection, e.g. any [rule relying on `EXPLAIN ...` output](vet.html#rules-using-explain-output).

If you don't yet have any vet rules, the [built-in sqlc/db-prepare rule](vet.md#sqlc-db-prepare)
If you don't yet have any vet rules, the [built-in sqlc/db-prepare rule](vet.html#sqlc-db-prepare)
is a good place to start. It prepares each of your queries against the database
to ensure the query is valid. Here's a minimal working configuration:

Expand All @@ -67,3 +97,35 @@ sql:
rules:
- sqlc/db-prepare
```

## With other tools

With managed databases configured, `sqlc createdb` will create a hosted ephemeral database with your
schema and write the database's connection URI as a string to standard output (stdout). This allows you to use
ephemeral databases with other tools that understand database connection strings.

In the simplest case, you can use psql to poke around:

```shell
psql $(sqlc createdb)
```

Or if you're tired of waiting for us to resolve https://github.com/sqlc-dev/sqlc/issues/296,
you can create databases ad hoc to use with pgtyped:

```shell
DATABASE_URL=$(sqlc createdb) npx pgtyped -c config.json
```

Here's a minimal working configuration if all you need to use is `sqlc createdb`:

```yaml
version: '2'
cloud:
project: '<PROJECT_ID>'
sql:
- schema: schema.sql
engine: postgresql
database:
managed: true
```
2 changes: 1 addition & 1 deletion docs/howto/rename.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ type Writer struct {
## Limitations

Rename mappings apply to an entire package. Therefore, a column named `foo` and
a table name `foo` can't be renamed different values.
a table name `foo` can't map to different rename values.
2 changes: 1 addition & 1 deletion docs/howto/structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ type Author struct {

## More control

See the Type Overrides section of the Configuration File docs for fine-grained control over struct field types and tags.
See the guide to [Overriding types](./overrides.md) for fine-grained control over struct field types and tags.
6 changes: 3 additions & 3 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ The `gen` mapping supports the following keys:
- `overrides`:
- It is a collection of definitions that dictates which types are used to map a database types.

##### `overrides`
##### overrides

See [Overriding types](../howto/overrides.md) for in-depth guide to using type overrides. Each mapping of the `overrides` collection has the following keys:
See [Overriding types](../howto/overrides.md) for an in-depth guide to using type overrides. Each mapping of the `overrides` collection has the following keys:

- `db_type`:
- The PostgreSQL or MySQL type to override. Find the full list of supported types in [postgresql_type.go](https://github.com/sqlc-dev/sqlc/blob/main/internal/codegen/golang/postgresql_type.go#L12) or [mysql_type.go](https://github.com/sqlc-dev/sqlc/blob/main/internal/codegen/golang/mysql_type.go#L12). Note that for Postgres you must use the pg_catalog prefixed names where available. Can't be used if the `column` key is defined.
Expand Down Expand Up @@ -318,7 +318,7 @@ rules:
query.cmd == "exec"
```

### global overrides
### Global overrides

Sometimes, the same configuration must be done across various specifications of
code generation. Then a global definition for type overriding and field
Expand Down

0 comments on commit 0d32dbe

Please sign in to comment.