faokunega / pg-embed

Run a Postgresql database locally on Linux, MacOS or Windows as part of another Rust application or test.
MIT License
104 stars 23 forks source link

`pg.create_database("database_name").await;` method `create_database` not found in `PgEmbed` #19

Open SamuelMarks opened 1 year ago

SamuelMarks commented 1 year ago

Error:

  --> src/main.rs:47:12
   |
47 |         pg.create_database("database_name").await;
   |            ^^^^^^^^^^^^^^^ method not found in `PgEmbed`

Attempt, related to #8 :

use std::path::PathBuf;
use std::time::Duration;

use pg_embed::pg_enums::PgAuthMethod;
use pg_embed::pg_fetch::{PgFetchSettings, PG_V13};
use pg_embed::postgres::{PgEmbed, PgSettings};

fn main() {
    const PG_SETTINGS: PgSettings = 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,
    };

    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
    async {
        // 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;

        // 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");

        // 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;
    };
}

Planning to wrap this lib into a CLI. I'll probably expose subcommands: version (will install if nonexistent); start_server; create_db ; delete_db; and stop_server. Use-case is primarily CI/CD workflows.

Related: https://github.com/fergusstrange/embedded-postgres/issues/100

snazzybunny commented 1 year ago

I tried to compile pg-embed with features = ["rt_tokio_migrate"] and that worked for me.