Expand description
📦 ARCHIVED 📦
This project is no longer maintained, and its repository is archived. The last release, 0.7.1, supports PostgreSQL 15 and later, and was tested with 15 to 18. It should keep working for a while, but it will not be updated for new versions of PostgreSQL or Rust. You are welcome to fork it.
§pgdo-lib
A Rust library for creating standalone PostgreSQL clusters and databases with a focus on convenience and rapid prototyping – such as one sees using SQLite. Scaling down the developer experience to meet individuals working to build something new, build something rapidly, is a key goal of this project.
It inherits code from the rust-postgresfixture project but deviates from that project’s goals and design. Way back, we can trace this tool’s origins to ideas in the Python postgresfixture library which saw heavy use in MAAS. That was (and is) a useful tool when experimenting with PostgreSQL. For example we could use it to bring up a cluster to run a development server. However, it came into its own in MAAS’s test suites, and was key to making MAAS’s test suites faster.
§Command-line application
There is a pgdo command-line application that uses this
library. That may be the easiest way to see how pgdo works.
It’s published as pgdo-cli on Crates.io and Lib.rs.
§Use as a library
The essential functionality in this crate is in the Cluster struct and its
implementation. This covers the logic you need to create, run, and destroy
PostgreSQL clusters of any version that is supported upstream, from PostgreSQL
15 onwards (see pgdo::runtime::MINIMUM_VERSION).
use pgdo::{
cluster::{Cluster, ClusterError, State},
runtime::{self, strategy::StrategyLike},
};
for runtime in runtime::strategy::Strategy::default().runtimes() {
let datadir = tempfile::tempdir()?;
let cluster = Cluster::new(&datadir, runtime)?;
cluster.start(&[])?;
assert_eq!(cluster.databases()?, vec!["postgres", "template0", "template1"]);
assert_eq!(cluster.createdb("example")?, State::Modified);
// Connect with your client library of choice via `cluster.datadir`, or
// with a URL from `cluster.url("example")`, e.g.:
let url = cluster.url("example")?.expect("datadir is not valid UTF-8");
assert_eq!(url.scheme(), "postgresql");
cluster.stop()?;
}However, you will usually want to use a Session
instead: cluster.session(&[])? creates and starts the cluster as necessary,
and stops it – or destroys it – when the session ends. Sessions use locking so
that multiple processes can safely share a single on-demand cluster; the cluster
is stopped only when the last session ends.
To connect with a different client library, such as SQLx, use
Cluster::datadir or
Cluster::url.
§Contributing
If you feel the urge to hack on this code, here’s how to get started:
- Install Cargo,
- Clone this repository,
- Build it:
cargo build.
§Running the tests
After installing the source (see above) run tests with: cargo test.
Most tests use pgdo’s platform-specific knowledge to test against all of the PostgreSQL runtimes that are installed. When writing new tests, try to mimic the pattern in preexisting tests to ensure that those tests are getting the broadest coverage. Specifically this means:
-
Install multiple versions of PostgreSQL on your machine. Read on for platform-specific notes.
-
runtime::strategy::Strategy::default()may be able to automatically find those installed runtimes – this is the function used by tests. -
If pgdo’s platform-specific knowledge doesn’t cover your platform, have a go at adding to it.
runtime::strategy::RuntimesOnPlatformis a good place to start.
§Debian & Ubuntu
From https://wiki.postgresql.org/wiki/Apt:
$ sudo apt-get install -y postgresql-common
$ sudo sh /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -y
$ sudo apt-get install -y postgresql-{15,16,17,18} # Adjust as necessary.§macOS
Using Homebrew:
$ brew install postgresql # Latest version.
$ brew install postgresql@{15,16,17,18} # Adjust as necessary.§Releasing new versions of pgdo-cli and pgdo-lib
The packages in this workspace are released together, with the same version number, and they must be uploaded in a certain order.
- Bump the version in the top-level (workspace)
Cargo.toml. - In
pgdo-cli/Cargo.toml, update the dependency onpgdo-libto match the new version from the previous step. - Run
cargo update --workspaceto ensure thatCargo.lockis up to date. - Paste updated
-houtput into pgdo-cli’sREADME.md. On macOS the commandcargo run -- -h | pbcopyis helpful. Note that--helpoutput is not the same as-houtput: it’s more verbose and too much for an overview. - Build, test, docs:
cargo build && cargo test && cargo doc --no-deps. - Commit everything with the message “Bump version to
$VERSION.” - Tag with “v
$VERSION”, e.g.git tag v1.0.10. - Push:
git push && git push --tags. - Publish library:
cargo publish --package pgdo-lib. - Publish binary:
cargo publish --package pgdo-cli.
§License
This package is licensed under the Apache 2.0 License.