-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example in readme doesn't compile #8
Comments
Hi, |
.... and I will update the example so that this won't happen anymore. |
Thanks! |
This compiles use config::Config;
use apps_data::DBManager;
use pg_embed::postgres::{PgEmbed, PgSettings};
use pg_embed::pg_enums::PgAuthMethod;
use pg_embed::pg_fetch;
use pg_embed::pg_fetch::{PgFetchSettings, PG_V13};
use std::time::Duration;
use std::path::PathBuf;
#[test]
fn test_connect() {
let mut settings = Config::new();
settings.set_default("database.user", "postgres").unwrap();
settings.set_default("database.password", "password").unwrap();
settings.set_default("database.host", "172.17.0.3").unwrap();
settings.set_default("database.dbname", "hmi").unwrap();
settings.set_default("database.port", "5432").unwrap();
println!("{:?}", settings);
db_init();
let mut dbman = DBManager::new(&settings);
let conn = dbman.connected();
println!("connected: {}", conn);
assert!(conn, "database not connected");
}
// async fn main() -> Result<(), Box<dyn std::error::Error>>
async fn db_init() -> Result<(), Box<dyn std::error::Error>> {
// Postgresql settings
let pg_settings = PgSettings {
// Where to store the postgresql database
database_dir: PathBuf::from("data/db"),
port: 5432,
user: "postgres".to_string(),
password: "password".to_string(),
// authentication method
auth_method: PgAuthMethod::Plain,
// If persistent is false clean up files and directories on drop, otherwise keep them
persistent: false,
// duration to wait before terminating process execution
// pg_ctl start/stop and initdb timeout
// if set to None the process will not be terminated
timeout: 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 details
migration_dir: None,
};
// Postgresql binaries download settings
let fetch_settings = PgFetchSettings {
version: PG_V13,
..Default::default()
};
// async block only to show that these methods need to be executed in an async context
// Create a new instance
let mut 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;
// run migration sql scripts
// to enable migrations view [Usage] for details
pg.migrate("database_name").await;
// stop postgresql database
pg.stop_db().await;
// get the base postgresql uri
// `postgres://{username}:{password}@localhost:{port}`
let pg_uri: &str = &pg.db_uri;
// get a postgresql database uri
// `postgres://{username}:{password}@localhost:{port}/{specified_database_name}`
let pg_db_uri: String = pg.full_db_uri("database_name");
Ok(())
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After wrapping the code on
fn main() { }
, here it fails compilation with:Compiler error
The
?
error isn't too important (I guess I could just dofn main() -> Result<..> { }
, but the unresolved import errors seem to indicate the code example is for another version of pg-embed.Are there other examples? Or some repository on Github that use pg-embed to do something.
The text was updated successfully, but these errors were encountered: