From e7c4dcfa140fa6bc774fe185cb57159eeb9062e7 Mon Sep 17 00:00:00 2001 From: Stepan Kuzmin Date: Sun, 18 Nov 2018 15:03:40 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20check=20if=20PostGIS=20is?= =?UTF-8?q?=20installed=20when=20starting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit check if PostGIS >= 2.4.0 is installed when starting martin Issues: #13 --- Cargo.lock | 1 + Cargo.toml | 1 + src/db.rs | 13 +++++++++++++ src/main.rs | 28 +++++++++++++++++++++++++--- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f2abf577..8422b6be 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -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)", diff --git a/Cargo.toml b/Cargo.toml index 2a63e9de..bae74d2c 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/db.rs b/src/db.rs index 932701f7..cd02ea0e 100755 --- a/src/db.rs +++ b/src/db.rs @@ -1,6 +1,7 @@ use r2d2::{Pool, PooledConnection}; use r2d2_postgres::{PostgresConnectionManager, TlsMode}; use std::error::Error; +use std::io; pub type PostgresPool = Pool; pub type PostgresConnection = PooledConnection; @@ -17,3 +18,15 @@ pub fn setup_connection_pool( Ok(pool) } + +pub fn select_postgis_verion(pool: &PostgresPool) -> io::Result { + 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) +} diff --git a/src/main.rs b/src/main.rs index 5169db9a..abec7ed7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) => {