You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to use pg-embed to run a test. So I did the following:
cargo.toml
...
[dev-dependencies]
pg-embed = { version = "0.7", default-features = false, features = ["rt_tokio", "rt_tokio_migrate"] }
And my rs file:
#[cfg(test)]mod tests {use std::path::PathBuf;use std::time::Duration;use pg_embed::pg_enums::PgAuthMethod;use pg_embed::pg_errors::PgEmbedError;use pg_embed::pg_fetch::{PG_V15,PgFetchSettings};use pg_embed::postgres::{PgEmbed,PgSettings};#[tokio::test]asyncfntest_pg_embed() -> Result<(),PgEmbedError>{/// Postgresql settingslet pg_settings = PgSettings{// Where to store the postgresql databasedatabase_dir:PathBuf::from("data/db"),port:5435,user:"postgres".to_string(),password:"password".to_string(),// authentication methodauth_method:PgAuthMethod::Plain,// If persistent is false clean up files and directories on drop, otherwise keep thempersistent:false,// duration to wait before terminating process execution// pg_ctl start/stop and initdb timeout// if set to None the process will not be terminatedtimeout:Some(Duration::from_secs(15)),// If migration sql scripts need to be run, the directory containing those scripts can be// specified here with `Some(PathBuf(path_to_dir)), otherwise `None` to run no migrations.// To enable migrations view the **Usage** section for detailsmigration_dir:None,};/// Postgresql binaries download settingslet fetch_settings = PgFetchSettings{version:PG_V15,
..Default::default()};// Create a new instanceletmut pg = PgEmbed::new(pg_settings, fetch_settings).await?;// Download, unpack, create password file and database cluster
pg.setup().await?;// start postgresql database
pg.start_db().await?;// create a new database// to enable migrations view the [Usage] section for details
pg.create_database("database_name").await?;// drop a database// to enable migrations view [Usage] for details
pg.drop_database("database_name").await?;// check database existence// to enable migrations view [Usage] for details
pg.database_exists("database_name").await?;// stop postgresql database
pg.stop_db().await?;Ok(())}}
Hey @manuelarte, I started using pg-embed this week in one of our projects and it did work successfully. The version is a bit different. We chose to use the latest version which does not seem to be published to crates.io, instead the dependency looks like this:
[dependencies]
pg-embed = { git = "https://github.com/faokunega/pg-embed", tag = "v0.8.0", default-features = false, features = ["rt_tokio"] }
tokio = { version = "1", features = [ "full", "fs" ] }
The only real difference in the Rust code is that for database_dir we use a fully qualified path to a specific the directory, rather than making it relative to the test executable.
In addition we chose to use sqlx to connect to the Postgres instance and do the database create and drop via normal SQL statements executed with sqlx. The connection pool setup looks like:
let pool = matchPgPoolOptions::new().connect(&pg.db_uri).await{Ok(resp) => resp,Err(err) => {// Do something with error ...}}
Here is the related issue that mentions how to use pg-embed 0.8.0 from GitHub rather than crates.io: Issue #28
Hi,
I am trying to use pg-embed to run a test. So I did the following:
cargo.toml
And my rs file:
But I get:
Error: PgEmbedError { error_type: PgInitFailure, source: None, message: None }
any idea?
The text was updated successfully, but these errors were encountered: