feat: upgrade postgres crates (#85)

* feat: upgrade postgres crates

* style: rename db types

* ci: switch to x86_64-unknown-linux-gnu
This commit is contained in:
Stepan Kuzmin 2020-05-04 21:21:11 +03:00 committed by GitHub
parent dbf6a83eb5
commit 9887d2bb99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 472 additions and 248 deletions

View File

@ -1,6 +1,6 @@
name: CI name: CI
on: [push, pull_request] on: [push]
jobs: jobs:
check: check:
@ -142,7 +142,7 @@ jobs:
include: include:
- os: ubuntu-latest - os: ubuntu-latest
rust: stable rust: stable
target: x86_64-unknown-linux-musl target: x86_64-unknown-linux-gnu
bin: martin bin: martin
name: martin-Linux-x86_64.tar.gz name: martin-Linux-x86_64.tar.gz
- os: windows-latest - os: windows-latest
@ -163,10 +163,6 @@ jobs:
toolchain: ${{ matrix.rust }} toolchain: ${{ matrix.rust }}
override: true override: true
target: ${{ matrix.target }} target: ${{ matrix.target }}
- name: Install musl tools
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt-get install -y musl-tools
- name: Checkout - name: Checkout
uses: actions/checkout@v1 uses: actions/checkout@v1
- name: Run build - name: Run build

595
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -14,17 +14,18 @@ path = "src/bin/main.rs"
[dependencies] [dependencies]
actix = "0.9" actix = "0.9"
actix-cors = "0.2"
actix-rt = "1.1" actix-rt = "1.1"
actix-web = "2.0" actix-web = "2.0"
actix-cors = "0.2"
docopt = "1" docopt = "1"
env_logger = "0.7" env_logger = "0.7"
futures = "0.1"
log = "0.4" log = "0.4"
native-tls = "0.2"
num_cpus = "1.13" num_cpus = "1.13"
postgres = { version = "0.15", features = ["with-time", "with-uuid", "with-serde_json"] } postgres = { version = "0.17.3", features = ["with-time-0_2", "with-uuid-0_8", "with-serde_json-1"] }
postgres-native-tls = "0.3.0"
r2d2 = "0.8" r2d2 = "0.8"
r2d2_postgres = "0.14" r2d2_postgres = "0.16"
semver = "0.9" semver = "0.9"
serde = "1.0" serde = "1.0"
serde_derive = "1.0" serde_derive = "1.0"

View File

@ -6,7 +6,7 @@ use serde::Deserialize;
use std::{env, io}; use std::{env, io};
use martin::config::{read_config, Config, ConfigBuilder}; use martin::config::{read_config, Config, ConfigBuilder};
use martin::db::{check_postgis_version, setup_connection_pool, PostgresPool}; use martin::db::{check_postgis_version, setup_connection_pool, get_connection, Pool};
use martin::function_source::get_function_sources; use martin::function_source::get_function_sources;
use martin::server; use martin::server;
use martin::table_source::get_table_sources; use martin::table_source::get_table_sources;
@ -50,14 +50,11 @@ pub struct Args {
pub fn generate_config( pub fn generate_config(
args: Args, args: Args,
connection_string: String, connection_string: String,
pool: &PostgresPool, pool: &Pool,
) -> io::Result<Config> { ) -> io::Result<Config> {
let conn = pool let mut connection = get_connection(pool)?;
.get() let table_sources = get_table_sources(&mut connection)?;
.map_err(|err| io::Error::new(io::ErrorKind::Other, err.to_string()))?; let function_sources = get_function_sources(&mut connection)?;
let table_sources = get_table_sources(&conn)?;
let function_sources = get_function_sources(&conn)?;
let config = ConfigBuilder { let config = ConfigBuilder {
connection_string, connection_string,
@ -74,7 +71,7 @@ pub fn generate_config(
Ok(config) Ok(config)
} }
fn setup_from_config(file_name: String) -> Result<(Config, PostgresPool), std::io::Error> { fn setup_from_config(file_name: String) -> Result<(Config, Pool), std::io::Error> {
let config = read_config(&file_name).map_err(prettify_error("Can't read config"))?; let config = read_config(&file_name).map_err(prettify_error("Can't read config"))?;
let pool = setup_connection_pool(&config.connection_string, Some(config.pool_size)) let pool = setup_connection_pool(&config.connection_string, Some(config.pool_size))
@ -85,7 +82,7 @@ fn setup_from_config(file_name: String) -> Result<(Config, PostgresPool), std::i
Ok((config, pool)) Ok((config, pool))
} }
fn setup_from_database(args: Args) -> Result<(Config, PostgresPool), std::io::Error> { fn setup_from_database(args: Args) -> Result<(Config, Pool), std::io::Error> {
let connection_string = if args.arg_connection.is_some() { let connection_string = if args.arg_connection.is_some() {
args.arg_connection.clone().unwrap() args.arg_connection.clone().unwrap()
} else { } else {

View File

@ -1,16 +1,21 @@
use r2d2::{Pool, PooledConnection}; use std::io;
use r2d2_postgres::{PostgresConnectionManager, TlsMode}; use std::str::FromStr;
use postgres::NoTls;
use r2d2_postgres::PostgresConnectionManager;
use semver::Version; use semver::Version;
use semver::VersionReq; use semver::VersionReq;
use std::io;
pub type PostgresPool = Pool<PostgresConnectionManager>; pub type Pool = r2d2::Pool<PostgresConnectionManager<NoTls>>;
pub type PostgresConnection = PooledConnection<PostgresConnectionManager>; pub type Connection = r2d2::PooledConnection<PostgresConnectionManager<NoTls>>;
pub fn setup_connection_pool(cn_str: &str, pool_size: Option<u32>) -> io::Result<PostgresPool> { pub fn setup_connection_pool(cn_str: &str, pool_size: Option<u32>) -> io::Result<Pool> {
let manager = PostgresConnectionManager::new(cn_str, TlsMode::None)?; let config = postgres::config::Config::from_str(cn_str)
.map_err(|err| io::Error::new(io::ErrorKind::Other, err.to_string()))?;
let pool = Pool::builder() let manager = PostgresConnectionManager::new(config, NoTls);
let pool = r2d2::Pool::builder()
.max_size(pool_size.unwrap_or(20)) .max_size(pool_size.unwrap_or(20))
.build(manager) .build(manager)
.map_err(|err| io::Error::new(io::ErrorKind::Other, err.to_string()))?; .map_err(|err| io::Error::new(io::ErrorKind::Other, err.to_string()))?;
@ -18,21 +23,28 @@ pub fn setup_connection_pool(cn_str: &str, pool_size: Option<u32>) -> io::Result
Ok(pool) Ok(pool)
} }
pub fn select_postgis_verion(pool: &PostgresPool) -> io::Result<String> { pub fn get_connection(pool: &Pool) -> io::Result<Connection> {
let conn = pool let connection = pool
.get() .get()
.map_err(|err| io::Error::new(io::ErrorKind::Other, err.to_string()))?; .map_err(|err| io::Error::new(io::ErrorKind::Other, err.to_string()))?;
let version: String = conn Ok(connection)
.query(r#"select (regexp_matches(postgis_lib_version(), '^(\d+\.\d+\.\d+)', 'g'))[1] as postgis_lib_version"#, &[]) }
.map(|rows| rows.get(0).get("postgis_lib_version"))?;
pub fn select_postgis_verion(pool: &Pool) -> io::Result<String> {
let mut connection = get_connection(pool)?;
let version = connection
.query_one(include_str!("scripts/get_postgis_version.sql"), &[])
.map(|row| row.get::<_, String>("postgis_version"))
.map_err(|err| io::Error::new(io::ErrorKind::Other, err.to_string()))?;
Ok(version) Ok(version)
} }
pub fn check_postgis_version( pub fn check_postgis_version(
required_postgis_version: &str, required_postgis_version: &str,
pool: &PostgresPool, pool: &Pool,
) -> io::Result<bool> { ) -> io::Result<bool> {
let postgis_version = select_postgis_verion(&pool)?; let postgis_version = select_postgis_verion(&pool)?;

View File

@ -1,13 +1,13 @@
use actix::{Actor, Handler, SyncContext}; use actix::{Actor, Handler, SyncContext};
use std::io; use std::io;
use crate::db::PostgresPool; use crate::db::{get_connection, Pool};
use crate::function_source::{get_function_sources, FunctionSources}; use crate::function_source::{get_function_sources, FunctionSources};
use crate::messages; use crate::messages;
use crate::source::Tile; use crate::source::Tile;
use crate::table_source::{get_table_sources, TableSources}; use crate::table_source::{get_table_sources, TableSources};
pub struct DBActor(pub PostgresPool); pub struct DBActor(pub Pool);
impl Actor for DBActor { impl Actor for DBActor {
type Context = SyncContext<Self>; type Context = SyncContext<Self>;
@ -17,8 +17,8 @@ impl Handler<messages::GetTableSources> for DBActor {
type Result = Result<TableSources, io::Error>; type Result = Result<TableSources, io::Error>;
fn handle(&mut self, _msg: messages::GetTableSources, _: &mut Self::Context) -> Self::Result { fn handle(&mut self, _msg: messages::GetTableSources, _: &mut Self::Context) -> Self::Result {
let conn = self.0.get().unwrap(); let mut connection = get_connection(&self.0)?;
let table_sources = get_table_sources(&conn)?; let table_sources = get_table_sources(&mut connection)?;
Ok(table_sources) Ok(table_sources)
} }
} }
@ -27,8 +27,8 @@ impl Handler<messages::GetFunctionSources> for DBActor {
type Result = Result<FunctionSources, io::Error>; type Result = Result<FunctionSources, io::Error>;
fn handle(&mut self, _msg: messages::GetFunctionSources, _: &mut Self::Context) -> Self::Result { fn handle(&mut self, _msg: messages::GetFunctionSources, _: &mut Self::Context) -> Self::Result {
let conn = self.0.get().unwrap(); let mut connection = get_connection(&self.0)?;
let function_sources = get_function_sources(&conn)?; let function_sources = get_function_sources(&mut connection)?;
Ok(function_sources) Ok(function_sources)
} }
} }
@ -37,9 +37,8 @@ impl Handler<messages::GetTile> for DBActor {
type Result = Result<Tile, io::Error>; type Result = Result<Tile, io::Error>;
fn handle(&mut self, msg: messages::GetTile, _: &mut Self::Context) -> Self::Result { fn handle(&mut self, msg: messages::GetTile, _: &mut Self::Context) -> Self::Result {
let conn = self.0.get().unwrap(); let mut connection = get_connection(&self.0)?;
let tile = msg.source.get_tile(&mut connection, &msg.xyz, &msg.query)?;
let tile = msg.source.get_tile(&conn, &msg.xyz, &msg.query)?;
Ok(tile) Ok(tile)
} }

View File

@ -3,7 +3,7 @@ use std::collections::HashMap;
use std::io; use std::io;
use tilejson::{TileJSON, TileJSONBuilder}; use tilejson::{TileJSON, TileJSONBuilder};
use crate::db::PostgresConnection; use crate::db::Connection;
use crate::source::{Query, Source, Tile, XYZ}; use crate::source::{Query, Source, Tile, XYZ};
use crate::utils::query_to_json_string; use crate::utils::query_to_json_string;
@ -33,7 +33,7 @@ impl Source for FunctionSource {
fn get_tile( fn get_tile(
&self, &self,
conn: &PostgresConnection, conn: &mut Connection,
xyz: &XYZ, xyz: &XYZ,
query: &Option<Query>, query: &Option<Query>,
) -> Result<Tile, io::Error> { ) -> Result<Tile, io::Error> {
@ -54,15 +54,15 @@ impl Source for FunctionSource {
); );
let tile: Tile = conn let tile: Tile = conn
.query(&query, &[]) .query_one(query.as_str(), &[])
.map(|rows| rows.get(0).get(self.function.as_str())) .map(|row| row.get(self.function.as_str()))
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?; .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
Ok(tile) Ok(tile)
} }
} }
pub fn get_function_sources(conn: &PostgresConnection) -> Result<FunctionSources, io::Error> { pub fn get_function_sources(conn: &mut Connection) -> Result<FunctionSources, io::Error> {
let mut sources = HashMap::new(); let mut sources = HashMap::new();
let rows = conn let rows = conn

View File

@ -0,0 +1 @@
select (regexp_matches(postgis_lib_version(), '^(\d+\.\d+\.\d+)', 'g'))[1] as postgis_version;

View File

@ -11,7 +11,7 @@ use actix_web::{
use crate::config::Config; use crate::config::Config;
use crate::coordinator_actor::CoordinatorActor; use crate::coordinator_actor::CoordinatorActor;
use crate::db::PostgresPool; use crate::db::Pool;
use crate::db_actor::DBActor; use crate::db_actor::DBActor;
use crate::function_source::FunctionSources; use crate::function_source::FunctionSources;
use crate::messages; use crate::messages;
@ -318,7 +318,7 @@ fn create_state(
} }
} }
pub fn new(pool: PostgresPool, config: Config, watch_mode: bool) -> SystemRunner { pub fn new(pool: Pool, config: Config, watch_mode: bool) -> SystemRunner {
let sys = actix_rt::System::new("server"); let sys = actix_rt::System::new("server");
let db = SyncArbiter::start(3, move || DBActor(pool.clone())); let db = SyncArbiter::start(3, move || DBActor(pool.clone()));

View File

@ -4,7 +4,7 @@ use std::io;
use tilejson::TileJSON; use tilejson::TileJSON;
use crate::db::PostgresConnection; use crate::db::Connection;
pub type Tile = Vec<u8>; pub type Tile = Vec<u8>;
pub type Query = HashMap<String, String>; pub type Query = HashMap<String, String>;
@ -21,12 +21,7 @@ pub trait Source: Debug {
fn get_tilejson(&self) -> Result<TileJSON, io::Error>; fn get_tilejson(&self) -> Result<TileJSON, io::Error>;
fn get_tile( fn get_tile(&self, conn: &mut Connection, xyz: &XYZ, query: &Option<Query>) -> Result<Tile, io::Error>;
&self,
conn: &PostgresConnection,
xyz: &XYZ,
query: &Option<Query>,
) -> Result<Tile, io::Error>;
} }
// pub type Sources = HashMap<String, Box<dyn Source>>; // pub type Sources = HashMap<String, Box<dyn Source>>;

View File

@ -4,7 +4,7 @@ use std::io;
use tilejson::{TileJSON, TileJSONBuilder}; use tilejson::{TileJSON, TileJSONBuilder};
use crate::db::PostgresConnection; use crate::db::Connection;
use crate::source::{Query, Source, Tile, XYZ}; use crate::source::{Query, Source, Tile, XYZ};
use crate::utils; use crate::utils;
@ -41,7 +41,7 @@ impl Source for TableSource {
fn get_tile( fn get_tile(
&self, &self,
conn: &PostgresConnection, conn: &mut Connection,
xyz: &XYZ, xyz: &XYZ,
_query: &Option<Query>, _query: &Option<Query>,
) -> Result<Tile, io::Error> { ) -> Result<Tile, io::Error> {
@ -89,8 +89,8 @@ impl Source for TableSource {
); );
let tile: Tile = conn let tile: Tile = conn
.query(&query, &[]) .query_one(query.as_str(), &[])
.map(|rows| rows.get(0).get("st_asmvt")) .map(|row| row.get("st_asmvt"))
.map_err(|err| io::Error::new(io::ErrorKind::Other, err.to_string()))?; .map_err(|err| io::Error::new(io::ErrorKind::Other, err.to_string()))?;
Ok(tile) Ok(tile)
@ -101,7 +101,7 @@ static DEFAULT_EXTENT: u32 = 4096;
static DEFAULT_BUFFER: u32 = 64; static DEFAULT_BUFFER: u32 = 64;
static DEFAULT_CLIP_GEOM: bool = true; static DEFAULT_CLIP_GEOM: bool = true;
pub fn get_table_sources(conn: &PostgresConnection) -> Result<TableSources, io::Error> { pub fn get_table_sources(conn: &mut Connection) -> Result<TableSources, io::Error> {
let mut sources = HashMap::new(); let mut sources = HashMap::new();
let rows = conn let rows = conn