pg_embed/lib.rs
1//!
2//! # pg-embed
3//!
4//! [](http://crates.io/crates/pg-embed)
5//! [](https://docs.rs/pg-embed)
6//! [](http://crates.io/crates/pg-embed)
7//! [](https://github.com/faokunega/pg-embed/blob/master/LICENSE)
8//!
9//! Run a Postgresql database locally on Linux, MacOS or Windows as part of another Rust application or test.
10//!
11//! The currently supported async runtime for **pg-embed** is [tokio](https://crates.io/crates/tokio).
12//!
13//! Support for [async-std](https://crates.io/crates/async-std) and [actix](https://crates.io/crates/actix) is planned
14//! and will be available soon.
15//!
16//! # Usage
17//!
18//! - Add pg-embed to your Cargo.toml
19//!
20//! *Library without sqlx migration support*
21//!
22//! ```toml
23//! # Cargo.toml
24//! [dependencies]
25//! pg-embed = { version = "0.6", default-features = false, features = ["rt_tokio"] }
26//! ```
27//!
28//! *Library with sqlx migration support*
29//!
30//! ```toml
31//! # Cargo.toml
32//! [dependencies]
33//! pg-embed = "0.6"
34//! ```
35//!
36//!
37//! # Examples
38//!
39//! ```rust, ignore
40//!
41//! use pg_embed::postgres::{PgEmbed, PgSettings, PgAuthMethod};
42//! use pg_embed::pg_fetch;
43//! use pg_embed::pg_fetch::{PgFetchSettings, PG_V13};
44//! use std::time::Duration;
45//! use std::path::PathBuf;
46//!
47//! /// Postgresql settings
48//! let pg_settings = PgSettings{
49//! // Where to store the postgresql database
50//! database_dir: PathBuf::from("data/db"),
51//! port: 5432,
52//! user: "postgres".to_string(),
53//! password: "password".to_string(),
54//! // authentication method
55//! auth_method: PgAuthMethod::Plain,
56//! // If persistent is false clean up files and directories on drop, otherwise keep them
57//! persistent: false,
58//! // duration to wait before terminating process execution
59//! // pg_ctl start/stop and initdb timeout
60//! // if set to None the process will not be terminated
61//! timeout: Some(Duration::from_secs(15)),
62//! // If migration sql scripts need to be run, the directory containing those scripts can be
63//! // specified here with `Some(PathBuf(path_to_dir)), otherwise `None` to run no migrations.
64//! // To enable migrations view the **Usage** section for details
65//! migration_dir: None,
66//! };
67//!
68//! /// Postgresql binaries download settings
69//! let fetch_settings = PgFetchSettings{
70//! version: PG_V13,
71//! ..Default::default()
72//! };
73//!
74//!
75//! /// async block only to show that these methods need to be executed in an async context
76//! async {
77//! // Create a new instance
78//! let mut pg = PgEmbed::new(pg_settings, fetch_settings).await?;
79//!
80//! // Download, unpack, create password file and database cluster
81//! pg.setup().await;
82//!
83//! // start postgresql database
84//! pg.start_db().await;
85//!
86//! // create a new database
87//! // to enable migrations view the [Usage] section for details
88//! pg.create_database("database_name").await;
89//!
90//! // drop a database
91//! // to enable migrations view [Usage] for details
92//! pg.drop_database("database_name").await;
93//!
94//! // check database existence
95//! // to enable migrations view [Usage] for details
96//! pg.database_exists("database_name").await;
97//!
98//! // run migration sql scripts
99//! // to enable migrations view [Usage] for details
100//! pg.migrate("database_name").await;
101//!
102//! // stop postgresql database
103//! pg.stop_db().await;
104//! };
105//! // get the base postgresql uri
106//! // `postgres://{username}:{password}@localhost:{port}`
107//! let pg_uri: &str = &pg.db_uri;
108//!
109//! // get a postgresql database uri
110//! // `postgres://{username}:{password}@localhost:{port}/{specified_database_name}`
111//! let pg_db_uri: String = pg.full_db_uri("database_name");
112//!
113//!
114//!
115//! ```
116//! ## Info
117//!
118//! The downloaded postgresql binaries are cached in the following directories:
119//!
120//! - On Linux:
121//!
122//! `$XDG_CACHE_HOME/pg-embed`
123//!
124//! or
125//!
126//! `$HOME/.cache/pg-embed`
127//! - On Windows:
128//!
129//! `{FOLDERID_LocalAppData}/pg-embed`
130//! - On MacOS:
131//!
132//! `$HOME/Library/Caches/pg-embed`
133//!
134//!
135//! ## Recent Breaking Changes
136//!
137//! pg-embed follows semantic versioning, so breaking changes should only happen upon major version bumps. The only exception to this rule is breaking changes that happen due to implementation that was deemed to be a bug, security concerns, or it can be reasonably proved to affect no code. For the full details, see [CHANGELOG.md](https://github.com/faokunega/pg-embed/blob/master/CHANGELOG.md).
138//!
139//!
140//!
141//! ## License
142//!
143//! pg-embed is licensed under the MIT license. Please read the [LICENSE-MIT](https://github.com/faokunega/pg-embed/blob/master/LICENSE) file in this repository for more information.
144//!
145//! # Notes
146//!
147//! Reliant on the great work being done by [zonkyio/embedded-postgres-binaries](https://github.com/zonkyio/embedded-postgres-binaries) in order to fetch precompiled binaries from [Maven](https://mvnrepository.com/artifact/io.zonky.test.postgres/embedded-postgres-binaries-bom).
148//!
149
150extern crate dirs;
151#[macro_use]
152extern crate lazy_static;
153#[cfg(not(any(
154 feature = "rt_tokio_migrate",
155 feature = "rt_tokio",
156 feature = "rt_actix_migrate",
157 feature = "rt_actix",
158 feature = "rt_async_std_migrate",
159 feature = "rt_async_std",
160)))]
161compile_error!(
162 "one of the features ['rt_tokio_migrate', 'rt_tokio', \
163 'rt_actix_migrate', 'rt_actix', 'rt_async_std_migrate', \
164 'rt_async_std'] must be enabled"
165);
166
167#[cfg(any(
168 all(feature = "rt_tokio", feature = "rt_async_std"),
169 all(feature = "rt_tokio", feature = "rt_async_std_migrate"),
170 all(feature = "rt_tokio_migrate", feature = "rt_async_std"),
171 all(feature = "rt_tokio_migrate", feature = "rt_async_std_migrate"),
172))]
173compile_error!(
174 "only one of ['rt_tokio', 'rt_tokio_migrate', \
175 'rt_async_std', 'rt_async_std_migrate'] can be enabled"
176);
177
178pub mod command_executor;
179pub mod pg_access;
180pub mod pg_commands;
181pub mod pg_enums;
182pub mod pg_errors;
183pub mod pg_fetch;
184pub mod pg_types;
185pub mod pg_unpack;
186pub mod postgres;