-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathconfiguring-diesel-cli.md
228 lines (164 loc) · 7.48 KB
/
configuring-diesel-cli.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
---
title: "Configuring Diesel CLI"
lang: en-US
css: ../assets/stylesheets/application.css
include-after: |
<script src="../assets/javascripts/application.js"></script>
---
::: demo
::: content-wrapper
::: guide-wrapper
Diesel CLI is an optional tool Diesel provides to manage your
database schema. Its main two roles are to run database
migrations, and to create a Rust file which represents your
database schema.
The behavior of Diesel CLI can be configured through a toml file. By
default Diesel will look for `diesel.toml` in the same directory as
your `Cargo.toml` file. You can provide a different config file by
setting the `DIESEL_CONFIG_FILE` environment variable, or passing
`--config-file` on the command line. You can get a basic config file
with some defaults provided by running `diesel setup`.
As of Diesel 2.0, the file contains a single section,
`[print_schema]`. All fields in this file are optional.
## The `file` field
This field specifies the file where you want the Rust representation
of your schema to live. When this field is present, commands which
modify database schema (such as `diesel migration run`) will
automatically run `diesel print-schema`, and output its results to
this file.
This means that you can modify your database schema without having to
worry about running a separate command to update your Rust code. It is
highly recommended that you use this field, to ensure that the Rust
representation of your database schema is always in sync with what is
actually in your database. Typically this is set to `src/schema.rs`.
Unlike other fields, this doesn't actually modify the behavior of
`diesel print-schema`. `diesel print-schema` will always output your
schema to stdout, regardless of whether this field is present or not.
## The `with_docs` field
When this field is set to `true`, `diesel print-schema` will act as
though the `--with-docs` flag was passed by default. This places a doc
comment on all tables and columns. This comment contains either the
information provided by the database via `COMMENT ON` annotations
or is generated by diesel.
## The `filter` field
This field specifies which tables should be output by `diesel
print-schema`. It corresponds to the `--only-tables` and
`--except-tables` on the command line. Its value should be a map with
one of those two keys. For example:
::: code-block
[diesel.toml]()
```toml
[print_schema]
# This will cause only the users and posts tables to be output
filter = { only_tables = ["users", "posts"] }
# This will cause all tables *except* the comments table to be
# output
filter = { except_tables = ["comments"] }
```
:::
## The `schema` field
Specifies which schema to use when searching for tables. When set,
`diesel print-schema` will always behave as though `--schema` were
passed. This field only affects PostgreSQL. If no value is provided,
the `public` schema will be searched.
## The `generate_missing_sql_type_definitions` field
This field accepts a boolean value, that configures whether `diesel print-schema`
should generate definitions for missing sql types as part of the `schema.rs` file.
The generated sql type definitions are placed in a separate `sql_types` module inside
of the generated `schema.rs` file. This will generate only definitions for sql types actually
used by any of the `table!` definitions generated by `diesel print-schema`. Those definitions
automatically use the auto-generated sql type definition.
If this field is not present, or set to `true`
`diesel print-schema` will generate the corresponding definitions. This configuration
only generates custom sql type definition for PostgreSQL based database systems, as
SQLite and MySQL are using a fixed set of sql types.
See the `exclude_custom_type_definitions` field for skipping the generation of certain custom types.
::: code-block
[diesel.toml]()
```toml
[print_schema]
# skip generating missing sql type definitions
generate_missing_sql_type_definitions = false
```
:::
## The `custom_type_derives` field
This field adds `#[derive(...)]` items to the sql type definitions
automatically generated as a result of the
`generate_missing_sql_type_definitions` field.
When set, `diesel print-schema` will behave as if `--custom-type-derives`
were passed. The `diesel::sql_types::SqlType` trait will be automatically
derived if this flag is either empty, or doesn't already contain the trait.
::: code-block
[diesel.toml]()
```toml
[print_schema]
generate_missing_sql_type_definitions = true
# Derive `SqlType` and `Debug` for the automatically generated sql type definitions
custom_type_derives = ["diesel::sql_types::SqlType", "std::fmt::Debug"]
```
:::
## The `except_custom_type_definitions` field
This field allows to define a list of regexes matched against custom type names for which diesel-cli should not generate sql type definitions. You should use this option in combination with `import_types` if you use crates like `postgis-diesel` or `pgvector` that provide their own sql type definition for specific custom types. If not set this defaults to an empty list.
::: code-block
[diesel.toml]()
```toml
[print_schema]
generate_missing_sql_type_definitions = true
# excludes any type named `Vector` from the generated `sql_types` module
except_custom_type_definitions = ["Vector"]
```
:::
## The `import_types` field
This field adds `use` statements to the top of every `table!`
declaration. When set, `diesel print-schema` will behave as if
`--import-types` were passed. When no value is given, only types from
`diesel::sql_types` will be imported.
::: code-block
[diesel.toml]()
```toml
[print_schema]
# Add types from `diesel_full_text_search` like `tsvector`
import_types = ["diesel::sql_types::*", "diesel_full_text_search::types::*"]
```
:::
## The `patch_file` field
Specifies a `.patch` file to be applied to your schema after it is
generated. Corresponds to the `--patch-file` option on the command
line.
We can't provide an option for every possible customization to this
file that you might want to make. This serves as a general purpose
catch-all for schema customizations.
The file should be a unified diff, which you can generate with `diff`
or `git diff`. It's highly recommended that you provide more than 3
context lines, especially if you have set `import_types`.
You can easily generate this file by making the changes you want to
`schema.rs`, and then running `git diff -U6 > src/schema.patch`.
## The `sqlite_integer_primary_key_is_bigint` field
This field allows you to configure diesel-cli to thread `INTEGER PRIMARY KEY` fields as `BIGINT` for sqlite databases. If not set this defaults to false.
::: code-block
[diesel.toml]()
```toml
[print_schema]
sqlite_integer_primary_key_is_bigint = true
```
:::
## Nested sub schemas
Diesel-CLI allows to configure nested subschemas via `[print_schema.your_sub_schema]` entries. This allows to split up large `schema.rs` files into smaller ones or generate schema.rs files for multiple database schemas at once. Each subschema can configure all keys described above. Diesel-cli allows to configure the subschema that should be used via the `--schema-key` command line argument. If no such argument is given it default to the top level `[print_schema]` entry.
::: code-block
[diesel.toml]()
```toml
# defines a subschema `user1` including the `user1` table
[print_schema.user1]
file = "src/schema1.rs"
with_docs = true
filter = { only_tables = ["users1"] }
# defines a subschema `user2` including the `user2` table
[print_schema.user2]
file = "src/schema2.rs"
with_docs = true
filter = { only_tables = ["users2"] }
```
:::
:::
:::
:::