fix: 🐛 check if PostGIS is installed when starting

check if PostGIS >= 2.4.0 is installed when starting martin

Issues: #13
This commit is contained in:
Stepan Kuzmin 2018-11-18 15:03:40 +03:00
parent 0b6000a511
commit e7c4dcfa14
4 changed files with 40 additions and 3 deletions

1
Cargo.lock generated
View File

@ -741,6 +741,7 @@ dependencies = [
"postgres 0.15.2 (registry+https://github.com/rust-lang/crates.io-index)",
"r2d2 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)",
"r2d2_postgres 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
"semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -15,6 +15,7 @@ num_cpus = "1.8"
postgres = { version = "0.15", features = ["with-time", "with-uuid", "with-serde_json"] }
r2d2 = "0.8"
r2d2_postgres = "0.14"
semver = "0.9"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"

View File

@ -1,6 +1,7 @@
use r2d2::{Pool, PooledConnection};
use r2d2_postgres::{PostgresConnectionManager, TlsMode};
use std::error::Error;
use std::io;
pub type PostgresPool = Pool<PostgresConnectionManager>;
pub type PostgresConnection = PooledConnection<PostgresConnectionManager>;
@ -17,3 +18,15 @@ pub fn setup_connection_pool(
Ok(pool)
}
pub fn select_postgis_verion(pool: &PostgresPool) -> io::Result<String> {
let conn = pool
.get()
.map_err(|err| io::Error::new(io::ErrorKind::Other, err.description()))?;
let version: String = conn
.query("select postgis_lib_version()", &[])
.map(|rows| rows.get(0).get("postgis_lib_version"))?;
Ok(version)
}

View File

@ -1,5 +1,4 @@
#![warn(clippy)]
#[cfg_attr(feature = "cargo-clippy", allow(clippy))]
extern crate actix;
extern crate actix_web;
extern crate docopt;
@ -13,6 +12,7 @@ extern crate num_cpus;
extern crate postgres;
extern crate r2d2;
extern crate r2d2_postgres;
extern crate semver;
extern crate serde;
#[macro_use]
extern crate serde_derive;
@ -32,13 +32,16 @@ mod table_source;
mod utils;
use docopt::Docopt;
use semver::Version;
use semver::VersionReq;
use std::env;
use cli::{Args, USAGE};
use config::build_config;
use db::setup_connection_pool;
use db::{select_postgis_verion, setup_connection_pool};
const VERSION: &str = env!("CARGO_PKG_VERSION");
const REQUIRED_POSTGIS_VERSION: &str = ">= 2.4.0";
fn main() {
let env = env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "martin=info");
@ -77,6 +80,25 @@ fn main() {
}
};
match select_postgis_verion(&pool) {
Ok(postgis_version) => {
info!("PostGIS version: {}", postgis_version);
let req = VersionReq::parse(REQUIRED_POSTGIS_VERSION).unwrap();
let version = Version::parse(postgis_version.as_str()).unwrap();
if !req.matches(&version) {
error!("Martin requires PostGIS {}", REQUIRED_POSTGIS_VERSION);
std::process::exit(-1);
}
}
Err(error) => {
error!("Can't get PostGIS version: {}", error);
error!("Martin requires PostGIS {}", REQUIRED_POSTGIS_VERSION);
std::process::exit(-1);
}
};
let config = match build_config(&pool, args) {
Ok(config) => config,
Err(error) => {