Skip to content

Commit 3934629

Browse files
committed
feat: create sqlx.toml format
1 parent c597a22 commit 3934629

File tree

13 files changed

+1044
-7
lines changed

13 files changed

+1044
-7
lines changed

Cargo.lock

+51-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+9-2
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,21 @@ authors.workspace = true
5050
repository.workspace = true
5151

5252
[package.metadata.docs.rs]
53-
features = ["all-databases", "_unstable-all-types"]
53+
features = ["all-databases", "_unstable-all-types", "_unstable-doc"]
5454
rustdoc-args = ["--cfg", "docsrs"]
5555

5656
[features]
57-
default = ["any", "macros", "migrate", "json"]
57+
default = ["any", "macros", "migrate", "json", "config-all"]
5858

5959
derive = ["sqlx-macros/derive"]
6060
macros = ["derive", "sqlx-macros/macros"]
6161
migrate = ["sqlx-core/migrate", "sqlx-macros?/migrate", "sqlx-mysql?/migrate", "sqlx-postgres?/migrate", "sqlx-sqlite?/migrate"]
6262

63+
# Enable parsing of `sqlx.toml` for configuring macros, migrations, or both.
64+
config-macros = ["sqlx-macros?/config-macros"]
65+
config-migrate = ["sqlx-macros?/config-migrate"]
66+
config-all = ["config-macros", "config-migrate"]
67+
6368
# intended mainly for CI and docs
6469
all-databases = ["mysql", "sqlite", "postgres", "any"]
6570
_unstable-all-types = [
@@ -73,6 +78,8 @@ _unstable-all-types = [
7378
"uuid",
7479
"bit-vec",
7580
]
81+
# Render documentation that wouldn't otherwise be shown (e.g. `sqlx_core::config`).
82+
_unstable-doc = ["config-all", "sqlx-core/_unstable-doc"]
7683

7784
# Base runtime features without TLS
7885
runtime-async-std = ["_rt-async-std", "sqlx-core/_rt-async-std", "sqlx-macros?/_rt-async-std"]

sqlx-core/Cargo.toml

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ features = ["offline"]
1212

1313
[features]
1414
default = []
15-
migrate = ["sha2", "crc"]
15+
migrate = ["sha2", "crc", "config-migrate"]
1616

1717
any = []
1818

@@ -30,6 +30,12 @@ _tls-none = []
3030
# support offline/decoupled building (enables serialization of `Describe`)
3131
offline = ["serde", "either/serde"]
3232

33+
config = ["serde", "toml/parse"]
34+
config-macros = ["config"]
35+
config-migrate = ["config"]
36+
37+
_unstable-doc = ["config-macros", "config-migrate"]
38+
3339
[dependencies]
3440
# Runtimes
3541
async-std = { workspace = true, optional = true }
@@ -77,6 +83,7 @@ percent-encoding = "2.1.0"
7783
regex = { version = "1.5.5", optional = true }
7884
serde = { version = "1.0.132", features = ["derive", "rc"], optional = true }
7985
serde_json = { version = "1.0.73", features = ["raw_value"], optional = true }
86+
toml = { version = "0.8.16", optional = true }
8087
sha1 = { version = "0.10.1", default-features = false, optional = true }
8188
sha2 = { version = "0.10.0", default-features = false, optional = true }
8289
sqlformat = "0.2.0"

sqlx-core/src/config/common.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/// Configuration shared by multiple components.
2+
#[derive(Debug, Default, serde::Deserialize)]
3+
pub struct Config {
4+
/// Override the database URL environment variable.
5+
///
6+
/// This is used by both the macros and `sqlx-cli`.
7+
///
8+
/// Case-sensitive. Defaults to `DATABASE_URL`.
9+
///
10+
/// Example: Multi-Database Project
11+
/// -------
12+
/// You can use multiple databases in the same project by breaking it up into multiple crates,
13+
/// then using a different environment variable for each.
14+
///
15+
/// For example, with two crates in the workspace named `foo` and `bar`:
16+
///
17+
/// #### `foo/sqlx.toml`
18+
/// ```toml
19+
/// [macros]
20+
/// database_url_var = "FOO_DATABASE_URL"
21+
/// ```
22+
///
23+
/// #### `bar/sqlx.toml`
24+
/// ```toml
25+
/// [macros]
26+
/// database_url_var = "BAR_DATABASE_URL"
27+
/// ```
28+
///
29+
/// #### `.env`
30+
/// ```text
31+
/// FOO_DATABASE_URL=postgres://postgres@localhost:5432/foo
32+
/// BAR_DATABASE_URL=postgres://postgres@localhost:5432/bar
33+
/// ```
34+
///
35+
/// The query macros used in `foo` will use `FOO_DATABASE_URL`,
36+
/// and the ones used in `bar` will use `BAR_DATABASE_URL`.
37+
pub database_url_var: Option<String>,
38+
}

0 commit comments

Comments
 (0)