Debian Codegraph @

guides/diesel-setup.md

raw
1 # Diesel setup
2
3
4 ### Disclaimer
5 > The barrel crate is still in an early state of development and should not be considered "stable".
6 >
7 > Old migrations might break because the API was changed.
8 > Features might be removed or replaced! **Before 1.0 this crate is not stable
9 > and should __not__ be used in production**
10 > – just wanted to make that loud and clear :)
11
12 ---
13
14 Using rust migrations (via `barrel`) with `diesel` is really simple.
15 First make sure that you installed the `diesel_cli` with the `barrel-migrations` feature flag:
16
17 ```bash
18 ~ cargo install diesel_cli --features="barrel-migrations,barrel/sqlite3,sqlite"
19 ```
20
21 **Important:** you can only select one (1) backend with diesel.
22 Whichever you select will determine the migration files that are generated later.
23
24 ```toml
25 [dependencies]
26 diesel = { version = "1.4", features = ["sqlite"] }
27 # ...
28 ```
29
30 From this point using `diesel` is very similar to how you normally use it. The only difference is that you should provide a `--format` flag when letting diesel generate a migration for you. Running migrations doesn't change.
31
32 ```bash
33 ~ diesel migration generate <name> --format="barrel"
34 ~ diesel migration run
35 ```
36
37 A migration file generated by diesel will look as follows
38
39 ```rust
40 /// Handle up migrations
41 fn up(migr: &mut Migration) {}
42
43 /// Handle down migrations
44 fn down(migr: &mut Migration) {}
45 ```
46
47 The object provided as a function parameter is a mutable `Migration` object which you can operate on.
48 Please refer to [the docs](https://docs.rs/barrel/0.2.0/barrel/migration/struct.Migration.html) for API specifics.