Example in readme doesn't compile
Opened this issue · 4 comments
dlight commented
After wrapping the code on fn main() { }
, here it fails compilation with:
Compiler error
error[E0432]: unresolved import `pg_embed::fetch`
--> src/main.rs:2:5
|
2 | use pg_embed::fetch;
| ^^^^^^^^^^^^^^^ no `fetch` in the root
error[E0432]: unresolved import `pg_embed::fetch`
--> src/main.rs:3:15
|
3 | use pg_embed::fetch::{PgFetchSettings, PG_V13};
| ^^^^^ could not find `fetch` in `pg_embed`
error[E0425]: cannot find value `pg` in this scope
--> src/main.rs:69:25
|
69 | let pg_uri: &str = &pg.db_uri;
| ^^ not found in this scope
error[E0425]: cannot find value `pg` in this scope
--> src/main.rs:73:29
|
73 | let pg_db_uri: String = pg.full_db_uri("database_name");
| ^^ not found in this scope
error[E0603]: enum `PgAuthMethod` is private
--> src/main.rs:1:47
|
1 | use pg_embed::postgres::{PgEmbed, PgSettings, PgAuthMethod};
| ^^^^^^^^^^^^ private enum
|
note: the enum `PgAuthMethod` is defined here
--> /home/me/.cargo/registry/src/github.com-1ecc6299db9ec823/pg-embed-0.6.1/src/postgres.rs:27:23
|
27 | use crate::pg_enums::{PgAuthMethod, PgServerStatus};
| ^^^^^^^^^^^^
warning: unused doc comment
--> src/main.rs:9:1
|
9 | /// Postgresql settings
| ^^^^^^^^^^^^^^^^^^^^^^^
10 | / let pg_settings = PgSettings{
11 | | // Where to store the postgresql database
12 | | database_dir: PathBuf::from("data/db"),
13 | | port: 5432,
... |
27 | | migration_dir: None,
28 | | };
| |__- rustdoc does not generate documentation for statements
|
= note: `#[warn(unused_doc_comments)]` on by default
warning: unused doc comment
--> src/main.rs:30:1
|
30 | /// Postgresql binaries download settings
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31 | / let fetch_settings = PgFetchSettings{
32 | | version: PG_V13,
33 | | ..Default::default()
34 | | };
| |__- rustdoc does not generate documentation for statements
warning: unused doc comment
--> src/main.rs:37:1
|
37 | /// async block only to show that these methods need to be executed in an async context
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38 | / async {
39 | | // Create a new instance
40 | | let mut pg = PgEmbed::new(pg_settings, fetch_settings).await?;
41 | |
... |
65 | | pg.stop_db().await;
66 | | };
| |_- rustdoc does not generate documentation for expressions
error[E0277]: the `?` operator can only be used in an async block that returns `Result` or `Option` (or another type that implements `FromResidual`)
--> src/main.rs:40:65
|
38 | async {
| _______-
39 | | // Create a new instance
40 | | let mut pg = PgEmbed::new(pg_settings, fetch_settings).await?;
| | ^ cannot use the `?` operator in an async block that returns `()`
41 | |
... |
65 | | pg.stop_db().await;
66 | | };
| |_- this function should return `Result` or `Option` to accept `?`
|
= help: the trait `FromResidual<Result<Infallible, PgEmbedError>>` is not implemented for `()`
= note: required by `from_residual`
error: aborting due to 6 previous errors; 3 warnings emitted
Some errors have detailed explanations: E0425, E0432, E0603.
For more information about an error, try `rustc --explain E0425`.
error: could not compile `pgexample`
The ?
error isn't too important (I guess I could just do fn 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.
faokunega commented
Hi,
the example in the README isn't meant to be run, it's just a quick overview of all the available methods.
Just take a look in the tests
folder, the following files will show you how to use it
common.rs
, migration_tokio.rs
and postgres_tokio.rs
Cheers :)
faokunega commented
.... and I will update the example so that this won't happen anymore.
Thanks for the hint
dlight commented
Thanks!
christopinka commented
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(())
}