sqlx_core/config/common.rs
1/// Configuration shared by multiple components.
2#[derive(Debug, Default)]
3#[cfg_attr(
4 feature = "sqlx-toml",
5 derive(serde::Deserialize),
6 serde(default, rename_all = "kebab-case", deny_unknown_fields)
7)]
8pub struct Config {
9 /// Override the database URL environment variable.
10 ///
11 /// This is used by both the macros and `sqlx-cli`.
12 ///
13 /// Case-sensitive. Defaults to `DATABASE_URL`.
14 ///
15 /// Example: Multi-Database Project
16 /// -------
17 /// You can use multiple databases in the same project by breaking it up into multiple crates,
18 /// then using a different environment variable for each.
19 ///
20 /// For example, with two crates in the workspace named `foo` and `bar`:
21 ///
22 /// #### `foo/sqlx.toml`
23 /// ```toml
24 /// [common]
25 /// database-url-var = "FOO_DATABASE_URL"
26 /// ```
27 ///
28 /// #### `bar/sqlx.toml`
29 /// ```toml
30 /// [common]
31 /// database-url-var = "BAR_DATABASE_URL"
32 /// ```
33 ///
34 /// #### `.env`
35 /// ```text
36 /// FOO_DATABASE_URL=postgres://postgres@localhost:5432/foo
37 /// BAR_DATABASE_URL=postgres://postgres@localhost:5432/bar
38 /// ```
39 ///
40 /// The query macros used in `foo` will use `FOO_DATABASE_URL`,
41 /// and the ones used in `bar` will use `BAR_DATABASE_URL`.
42 pub database_url_var: Option<String>,
43}
44
45impl Config {
46 pub fn database_url_var(&self) -> &str {
47 self.database_url_var.as_deref().unwrap_or("DATABASE_URL")
48 }
49}