-
Notifications
You must be signed in to change notification settings - Fork 5
/
mod.rs
74 lines (63 loc) · 2.57 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//! Catalyst Election Database crate
use std::str::FromStr;
use bb8::Pool;
use bb8_postgres::PostgresConnectionManager;
use dotenvy::dotenv;
use error::Error;
use tokio_postgres::NoTls;
pub(crate) mod config;
pub(crate) mod error;
pub(crate) mod follower;
pub(crate) mod legacy;
pub(crate) mod schema_check;
pub(crate) mod utxo;
pub(crate) mod voter_registration;
/// Database URL Environment Variable name.
/// eg: "`postgres://catalyst-dev:CHANGE_ME@localhost/CatalystDev`"
const DATABASE_URL_ENVVAR: &str = "EVENT_DB_URL";
/// Database version this crate matches.
/// Must equal the last Migrations Version Number.
pub(crate) const DATABASE_SCHEMA_VERSION: i32 = 9;
#[allow(unused)]
/// Connection to the Election Database
pub(crate) struct EventDB {
/// Internal database connection. DO NOT MAKE PUBLIC.
/// All database operations (queries, inserts, etc) should be constrained
/// to this crate and should be exported with a clean data access api.
pool: Pool<PostgresConnectionManager<NoTls>>,
}
/// Establish a connection to the database, and check the schema is up-to-date.
///
/// # Parameters
///
/// * `url` set to the postgres connection string needed to connect to the database. IF
/// it is None, then the env var "`DATABASE_URL`" will be used for this connection
/// string. eg: "`postgres://catalyst-dev:CHANGE_ME@localhost/CatalystDev`"
/// * `do_schema_check` boolean flag to decide whether to verify the schema version or
/// not. If it is `true`, a query is made to verify the DB schema version.
///
/// # Errors
///
/// This function will return an error if:
/// * `url` is None and the environment variable "`DATABASE_URL`" isn't set.
/// * There is any error communicating the the database to check its schema.
/// * The database schema in the DB does not 100% match the schema supported by this
/// library.
///
/// # Notes
///
/// The env var "`DATABASE_URL`" can be set directly as an anv var, or in a
/// `.env` file.
pub(crate) async fn establish_connection(url: Option<String>) -> Result<EventDB, Error> {
// Support env vars in a `.env` file, doesn't need to exist.
dotenv().ok();
let database_url = match url {
Some(url) => url,
// If the Database connection URL is not supplied, try and get from the env var.
None => std::env::var(DATABASE_URL_ENVVAR).map_err(|_| Error::NoDatabaseUrl)?,
};
let config = tokio_postgres::config::Config::from_str(&database_url)?;
let pg_mgr = PostgresConnectionManager::new(config, tokio_postgres::NoTls);
let pool = Pool::builder().build(pg_mgr).await?;
Ok(EventDB { pool })
}