pg-setup

Crates.iopg-setup
lib.rspg-setup
version0.2.0
sourcesrc
created_at2023-01-15 18:28:38.62993
updated_at2023-10-07 02:44:31.428814
descriptionHelper to create and drop postgres DBs. Useful for testing.
homepagehttps://github.com/rksm/pg-setup-rs
repositoryhttps://github.com/rksm/pg-setup-rs
max_upload_size
id759612
size58,586
Robert Krahn (rksm)

documentation

README

pg-setup

Crates.io License

Simple helper to create and drop Postgres databases. Useful for tests.

This uses either the psql command line utility (default) or the sqlx and sqlx-cli (which makes use of sqlx migrations). Use the sqlx feature for that.

Example:

#
    let db_uri = "postgres://localhost:5432/pg_setup_example";

    let db = PostgresDBBuilder::new(db_uri)
        .schema("public")
        // optionally keep db
        .keep_db()
        .start()
        .await?;

    // optionally create a table
    db.create_table("users", |t| {
        t.add_column("id", "uuid", |c| c.primary_key());
        t.add_column("name", "text", |c| c.not_null());
        t.add_column("email", "text", |c| c.not_null());
        t.add_column("created_at", "timestamp", |c| c.not_null());
    })
    .await?;

    // execute sql
    db.execute("SELECT table_schema,table_name, table_type FROM information_schema.tables WHERE table_schema = 'public';").await?;

    // db will be dropped at the end of the scope, unless `keep_db` is called!

In case you want to keep the db around for debugging you can call [PostgresDB::keep_db].

Will use the public schema by default but you can set this with [PostgresDB::schema].

License: MIT

Commit count: 14

cargo fmt