Skip to content

Commit 97a7447

Browse files
Merge pull request #95 from theseus-rs/utilize-sqlx-for-database-management
feat: utilize sqlx for database management to support PostgreSQL installations that do not bundle psql
2 parents 794751d + 1d6d2fd commit 97a7447

File tree

9 files changed

+193
-87
lines changed

9 files changed

+193
-87
lines changed

Cargo.lock

Lines changed: 68 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ serde_json = "1.0.118"
4646
sha1 = "0.10.6"
4747
sha2 = "0.10.8"
4848
sha3 = "0.10.8"
49+
sqlx = { version = "0.7.4", default-features = false, features = ["postgres"] }
4950
tar = "0.4.41"
5051
target-triple = "0.1.3"
5152
test-log = "0.2.16"

examples/zonky/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
edition.workspace = true
3+
name = "zonky"
4+
publish = false
5+
license.workspace = true
6+
version.workspace = true
7+
8+
[dependencies]
9+
postgresql_archive = { path = "../../postgresql_archive" }
10+
postgresql_embedded = { path = "../../postgresql_embedded" }
11+
tokio = { workspace = true, features = ["full"] }

examples/zonky/src/main.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#![forbid(unsafe_code)]
2+
#![deny(clippy::pedantic)]
3+
4+
use postgresql_archive::configuration::zonky;
5+
use postgresql_archive::VersionReq;
6+
use postgresql_embedded::{PostgreSQL, Result, Settings};
7+
8+
#[tokio::main]
9+
async fn main() -> Result<()> {
10+
let settings = Settings {
11+
releases_url: zonky::URL.to_string(),
12+
version: VersionReq::parse("=16.2.0")?,
13+
..Default::default()
14+
};
15+
let mut postgresql = PostgreSQL::new(settings);
16+
postgresql.setup().await?;
17+
postgresql.start().await?;
18+
19+
let database_name = "test";
20+
postgresql.create_database(database_name).await?;
21+
postgresql.database_exists(database_name).await?;
22+
postgresql.drop_database(database_name).await?;
23+
24+
postgresql.stop().await
25+
}
26+
27+
#[cfg(test)]
28+
mod test {
29+
use super::*;
30+
31+
#[test]
32+
fn test_main() -> Result<()> {
33+
main()
34+
}
35+
}

postgresql_embedded/Cargo.toml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ postgresql_archive = { path = "../postgresql_archive", version = "0.12.0", defau
2424
postgresql_commands = { path = "../postgresql_commands", version = "0.12.0" }
2525
rand = { workspace = true }
2626
semver = { workspace = true }
27+
sqlx = { workspace = true, features = ["runtime-tokio"] }
2728
tempfile = { workspace = true }
2829
thiserror = { workspace = true }
2930
tokio = { workspace = true, features = ["full"], optional = true }
@@ -39,11 +40,18 @@ tokio = { workspace = true, features = ["full"] }
3940
default = ["rustls-tls"]
4041
blocking = ["tokio"]
4142
bundled = []
42-
native-tls = ["postgresql_archive/native-tls"]
43-
rustls-tls = ["postgresql_archive/rustls-tls"]
43+
native-tls = [
44+
"postgresql_archive/native-tls",
45+
"sqlx/tls-native-tls",
46+
]
47+
rustls-tls = [
48+
"postgresql_archive/rustls-tls",
49+
"sqlx/tls-rustls",
50+
]
4451
tokio = [
4552
"dep:tokio",
46-
"postgresql_commands/tokio"
53+
"postgresql_commands/tokio",
54+
"sqlx/runtime-tokio",
4755
]
4856

4957
[package.metadata.release]

postgresql_embedded/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ pub enum Error {
1515
/// Error when the database could not be created
1616
#[error(transparent)]
1717
CreateDatabaseError(anyhow::Error),
18+
/// Error when accessing the database
19+
#[error(transparent)]
20+
DatabaseError(#[from] sqlx::Error),
1821
/// Error when determining if the database exists
1922
#[error(transparent)]
2023
DatabaseExistsError(anyhow::Error),

0 commit comments

Comments
 (0)