From: Sarah Hoffmann Date: Wed, 30 Mar 2016 20:48:18 +0000 (+0200) Subject: remove explicitly set postgres/postgis version X-Git-Tag: v3.0.0~182^2~4 X-Git-Url: https://git.openstreetmap.org./nominatim.git/commitdiff_plain/02bd322c1dc185a6eef072de7398c190fe7a2e0c remove explicitly set postgres/postgis version Get the version from the database where necessary or simply probe for existence of features. Fake hstore_to_json when necessary. Bumps the minimum required versions fro postgres to 9.1 and for postgis to 2.0. --- diff --git a/lib/Geocode.php b/lib/Geocode.php index d699263e..f2ca5e50 100644 --- a/lib/Geocode.php +++ b/lib/Geocode.php @@ -228,11 +228,9 @@ function loadParamArray($aParams) { if (isset($aParams['addressdetails'])) $this->bIncludeAddressDetails = (bool)$aParams['addressdetails']; - if ((float) CONST_Postgresql_Version > 9.2) - { - if (isset($aParams['extratags'])) $this->bIncludeExtraTags = (bool)$aParams['extratags']; - if (isset($aParams['namedetails'])) $this->bIncludeNameDetails = (bool)$aParams['namedetails']; - } + if (isset($aParams['extratags'])) $this->bIncludeExtraTags = (bool)$aParams['extratags']; + if (isset($aParams['namedetails'])) $this->bIncludeNameDetails = (bool)$aParams['namedetails']; + if (isset($aParams['bounded'])) $this->bBoundedSearch = (bool)$aParams['bounded']; if (isset($aParams['dedupe'])) $this->bDeDupe = (bool)$aParams['dedupe']; diff --git a/lib/PlaceLookup.php b/lib/PlaceLookup.php index 817d0ec5..94e414dc 100644 --- a/lib/PlaceLookup.php +++ b/lib/PlaceLookup.php @@ -42,18 +42,12 @@ function setIncludeExtraTags($bExtraTags = false) { - if ((float) CONST_Postgresql_Version > 9.2) - { - $this->bExtraTags = $bExtraTags; - } + $this->bExtraTags = $bExtraTags; } function setIncludeNameDetails($bNameDetails = false) { - if ((float) CONST_Postgresql_Version > 9.2) - { - $this->bNameDetails = $bNameDetails; - } + $this->bNameDetails = $bNameDetails; } diff --git a/lib/db.php b/lib/db.php index d7c85e7b..ab578e4b 100644 --- a/lib/db.php +++ b/lib/db.php @@ -24,3 +24,16 @@ return "'".pg_escape_string($s)."'"; } + function getPostgresVersion(&$oDB) + { + $sVersionString = $oDB->getOne('select version()'); + preg_match('#PostgreSQL ([0-9]+)[.]([0-9]+)[^0-9]#', $sVersionString, $aMatches); + return (float) ($aMatches[1].'.'.$aMatches[2]); + } + + function getPostgisVersion(&$oDB) + { + $sVersionString = $oDB->getOne('select postgis_full_version()'); + preg_match('#POSTGIS="([0-9]+)[.]([0-9]+)[.]([0-9]+)( r([0-9]+))?"#', $sVersionString, $aMatches); + return (float) ($aMatches[1].'.'.$aMatches[2]); + } diff --git a/settings/settings.php b/settings/settings.php index ea664683..0c8d76bd 100644 --- a/settings/settings.php +++ b/settings/settings.php @@ -19,13 +19,7 @@ @define('CONST_HTTP_Proxy_Login', ''); @define('CONST_HTTP_Proxy_Password', ''); - // Software versions - @define('CONST_Postgresql_Version', '9.3'); // values: 9.0, ... , 9.4 - @define('CONST_Postgis_Version', '2.1'); // values: 1.5, 2.0, 2.1 - // Paths - @define('CONST_Path_Postgresql_Contrib', '/usr/share/postgresql/'.CONST_Postgresql_Version.'/contrib'); - @define('CONST_Path_Postgresql_Postgis', CONST_Path_Postgresql_Contrib.'/postgis-'.CONST_Postgis_Version); @define('CONST_Osm2pgsql_Binary', CONST_InstallPath.'/osm2pgsql/osm2pgsql'); @define('CONST_Osmosis_Binary', '/usr/bin/osmosis'); @define('CONST_Tiger_Data_Path', CONST_BasePath.'/data/tiger'); diff --git a/sql/functions.sql b/sql/functions.sql index b88a7f81..5b068dfe 100644 --- a/sql/functions.sql +++ b/sql/functions.sql @@ -1,3 +1,15 @@ +-- Splits the line at the given point and returns the two parts +-- in a multilinestring. +CREATE OR REPLACE FUNCTION split_line_on_node(line GEOMETRY, point GEOMETRY) +RETURNS GEOMETRY + AS $$ +BEGIN + RETURN ST_Split(ST_Snap(line, point, 0.0005), point); +END; +$$ +LANGUAGE plpgsql; + + CREATE OR REPLACE FUNCTION geometry_sector(partition INTEGER, place geometry) RETURNS INTEGER AS $$ DECLARE diff --git a/sql/postgis_15_aux.sql b/sql/postgis_15_aux.sql deleted file mode 100644 index 548e0fe5..00000000 --- a/sql/postgis_15_aux.sql +++ /dev/null @@ -1,14 +0,0 @@ --- Splits the line at the given point and returns the two parts --- in a multilinestring. -CREATE OR REPLACE FUNCTION split_line_on_node(line GEOMETRY, point GEOMETRY) -RETURNS GEOMETRY - AS $$ -DECLARE - frac FLOAT; -BEGIN - frac := ST_Line_Locate_Point(line, point); - RETURN ST_Collect(ST_Line_Substring(line, 0, frac), - ST_Line_Substring(line, frac, 1)); -END -$$ -LANGUAGE plpgsql; diff --git a/sql/postgis_20_aux.sql b/sql/postgis_20_aux.sql deleted file mode 100644 index 749e2a43..00000000 --- a/sql/postgis_20_aux.sql +++ /dev/null @@ -1,10 +0,0 @@ --- Splits the line at the given point and returns the two parts --- in a multilinestring. -CREATE OR REPLACE FUNCTION split_line_on_node(line GEOMETRY, point GEOMETRY) -RETURNS GEOMETRY - AS $$ -BEGIN - RETURN ST_Split(ST_Snap(line, point, 0.0005), point); -END; -$$ -LANGUAGE plpgsql; diff --git a/utils/setup.php b/utils/setup.php index 8c343e34..60fe5dac 100755 --- a/utils/setup.php +++ b/utils/setup.php @@ -90,8 +90,6 @@ $aDSNInfo = DB::parseDSN(CONST_Database_DSN); if (!isset($aDSNInfo['port']) || !$aDSNInfo['port']) $aDSNInfo['port'] = 5432; - $fPostgisVersion = (float) CONST_Postgis_Version; - if ($aCMDResult['create-db'] || $aCMDResult['all']) { echo "Create DB\n"; @@ -112,39 +110,38 @@ $oDB =& getDB(); - $sVersionString = $oDB->getOne('select version()'); - preg_match('#PostgreSQL ([0-9]+)[.]([0-9]+)[^0-9]#', $sVersionString, $aMatches); - if (CONST_Postgresql_Version != $aMatches[1].'.'.$aMatches[2]) + $fPostgresVersion = getPostgresVersion($oDB); + echo 'Postgres version found: '.$fPostgresVersion."\n"; + + if ($fPostgresVersion < 9.1) { - echo "ERROR: PostgreSQL version is not correct. Expected ".CONST_Postgresql_Version." found ".$aMatches[1].'.'.$aMatches[2]."\n"; - exit; + fail("Minimum supported version of Postgresql is 9.1."); } - passthru('createlang plpgsql -p '.$aDSNInfo['port'].' '.$aDSNInfo['database']); - $pgver = (float) CONST_Postgresql_Version; - if ($pgver < 9.1) { - pgsqlRunScriptFile(CONST_Path_Postgresql_Contrib.'/hstore.sql'); - pgsqlRunScriptFile(CONST_BasePath.'/sql/hstore_compatability_9_0.sql'); - } else { - pgsqlRunScript('CREATE EXTENSION hstore'); - } + pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS hstore'); + pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS postgis'); - if ($fPostgisVersion < 2.0) { - pgsqlRunScriptFile(CONST_Path_Postgresql_Postgis.'/postgis.sql'); - pgsqlRunScriptFile(CONST_Path_Postgresql_Postgis.'/spatial_ref_sys.sql'); - } else { - pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS postgis'); + // For extratags and namedetails the hstore_to_json converter is + // needed which is only available from Postgresql 9.3+. For older + // versions add a dummy function that returns nothing. + $iNumFunc = $oDB->getOne("select count(*) from pg_proc where proname = 'hstore_to_json'"); + if (PEAR::isError($iNumFunc)) + { + fail("Cannot query stored procedures.", $iNumFunc); } - if ($fPostgisVersion < 2.1) { - // Function was renamed in 2.1 and throws an annoying deprecation warning - pgsqlRunScript('ALTER FUNCTION st_line_interpolate_point(geometry, double precision) RENAME TO ST_LineInterpolatePoint'); + if ($iNumFunc == 0) + { + pgsqlRunScript("create function hstore_to_json(dummy hstore) returns text AS 'select null::text' language sql immutable"); + echo "WARNING: Postgresql is too old. extratags and namedetails API not available."; } - $sVersionString = $oDB->getOne('select postgis_full_version()'); - preg_match('#POSTGIS="([0-9]+)[.]([0-9]+)[.]([0-9]+)( r([0-9]+))?"#', $sVersionString, $aMatches); - if (CONST_Postgis_Version != $aMatches[1].'.'.$aMatches[2]) + + $fPostgisVersion = getPostgisVersion($oDB); + echo 'Postgis version found: '.$fPostgisVersion."\n"; + + if ($fPostgisVersion < 2.1) { - echo "ERROR: PostGIS version is not correct. Expected ".CONST_Postgis_Version." found ".$aMatches[1].'.'.$aMatches[2]."\n"; - exit; + // Function was renamed in 2.1 and throws an annoying deprecation warning + pgsqlRunScript('ALTER FUNCTION st_line_interpolate_point(geometry, double precision) RENAME TO ST_LineInterpolatePoint'); } pgsqlRunScriptFile(CONST_BasePath.'/data/country_name.sql'); @@ -225,15 +222,6 @@ if ($aCMDResult['enable-debug-statements']) $sTemplate = str_replace('--DEBUG:', '', $sTemplate); if (CONST_Limit_Reindexing) $sTemplate = str_replace('--LIMIT INDEXING:', '', $sTemplate); pgsqlRunScript($sTemplate); - - if ($fPostgisVersion < 2.0) { - echo "Helper functions for postgis < 2.0\n"; - $sTemplate = file_get_contents(CONST_BasePath.'/sql/postgis_15_aux.sql'); - } else { - echo "Helper functions for postgis >= 2.0\n"; - $sTemplate = file_get_contents(CONST_BasePath.'/sql/postgis_20_aux.sql'); - } - pgsqlRunScript($sTemplate); } if ($aCMDResult['create-tables'] || $aCMDResult['all']) diff --git a/utils/update.php b/utils/update.php index f5dec537..34d30a4f 100755 --- a/utils/update.php +++ b/utils/update.php @@ -178,8 +178,8 @@ if ($aResult['deduplicate']) { - $pgver = (float) CONST_Postgresql_Version; - if ($pgver < 9.3) { + if (getPostgresVersion() < 9.3) + { fail("ERROR: deduplicate is only currently supported in postgresql 9.3"); }