implicitly connect to database during setup

Make access to the DB object a function, so that the connection
can be opened implicitly when the object is accessed for the first
time. This way we no longer need to check beforehand if a specific
function of the setup needs DB access or not.

Also move the check for the module to the relevant sub step.
This commit is contained in:
Sarah Hoffmann
2020-07-26 11:56:00 +02:00
parent 840c692d5b
commit 8cd9550295
2 changed files with 58 additions and 51 deletions

View File

@@ -108,17 +108,11 @@ class SetupFunctions
if ($result != 0) fail('Error executing external command: '.$oCmd->escapedCmd()); if ($result != 0) fail('Error executing external command: '.$oCmd->escapedCmd());
} }
public function connect()
{
$this->oDB = new \Nominatim\DB();
$this->oDB->connect();
}
public function setupDB() public function setupDB()
{ {
info('Setup DB'); info('Setup DB');
$fPostgresVersion = $this->oDB->getPostgresVersion(); $fPostgresVersion = $this->db()->getPostgresVersion();
echo 'Postgres version found: '.$fPostgresVersion."\n"; echo 'Postgres version found: '.$fPostgresVersion."\n";
if ($fPostgresVersion < 9.03) { if ($fPostgresVersion < 9.03) {
@@ -128,7 +122,7 @@ class SetupFunctions
$this->pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS hstore'); $this->pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS hstore');
$this->pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS postgis'); $this->pgsqlRunScript('CREATE EXTENSION IF NOT EXISTS postgis');
$fPostgisVersion = $this->oDB->getPostgisVersion(); $fPostgisVersion = $this->db()->getPostgisVersion();
echo 'Postgis version found: '.$fPostgisVersion."\n"; echo 'Postgis version found: '.$fPostgisVersion."\n";
if ($fPostgisVersion < 2.2) { if ($fPostgisVersion < 2.2) {
@@ -136,7 +130,7 @@ class SetupFunctions
exit(1); exit(1);
} }
$i = $this->oDB->getOne("select count(*) from pg_user where usename = '".CONST_Database_Web_User."'"); $i = $this->db()->getOne("select count(*) from pg_user where usename = '".CONST_Database_Web_User."'");
if ($i == 0) { if ($i == 0) {
echo "\nERROR: Web user '".CONST_Database_Web_User."' does not exist. Create it with:\n"; echo "\nERROR: Web user '".CONST_Database_Web_User."' does not exist. Create it with:\n";
echo "\n createuser ".CONST_Database_Web_User."\n\n"; echo "\n createuser ".CONST_Database_Web_User."\n\n";
@@ -223,7 +217,7 @@ class SetupFunctions
$oCmd->addParams($sOSMFile); $oCmd->addParams($sOSMFile);
$oCmd->run(); $oCmd->run();
if (!$this->sIgnoreErrors && !$this->oDB->getRow('select * from place limit 1')) { if (!$this->sIgnoreErrors && !$this->db()->getRow('select * from place limit 1')) {
fail('No Data'); fail('No Data');
} }
@@ -257,7 +251,7 @@ class SetupFunctions
} }
$oAlParser = new AddressLevelParser(CONST_Address_Level_Config); $oAlParser = new AddressLevelParser(CONST_Address_Level_Config);
$oAlParser->createTable($this->oDB, 'address_levels'); $oAlParser->createTable($this->db(), 'address_levels');
} }
public function createTableTriggers() public function createTableTriggers()
@@ -305,40 +299,42 @@ class SetupFunctions
{ {
info('Drop old Data'); info('Drop old Data');
$this->oDB->exec('TRUNCATE word'); $oDB = $this->db();
$oDB->exec('TRUNCATE word');
echo '.'; echo '.';
$this->oDB->exec('TRUNCATE placex'); $oDB->exec('TRUNCATE placex');
echo '.'; echo '.';
$this->oDB->exec('TRUNCATE location_property_osmline'); $oDB->exec('TRUNCATE location_property_osmline');
echo '.'; echo '.';
$this->oDB->exec('TRUNCATE place_addressline'); $oDB->exec('TRUNCATE place_addressline');
echo '.'; echo '.';
$this->oDB->exec('TRUNCATE location_area'); $oDB->exec('TRUNCATE location_area');
echo '.'; echo '.';
if (!$this->dbReverseOnly()) { if (!$this->dbReverseOnly()) {
$this->oDB->exec('TRUNCATE search_name'); $oDB->exec('TRUNCATE search_name');
echo '.'; echo '.';
} }
$this->oDB->exec('TRUNCATE search_name_blank'); $oDB->exec('TRUNCATE search_name_blank');
echo '.'; echo '.';
$this->oDB->exec('DROP SEQUENCE seq_place'); $oDB->exec('DROP SEQUENCE seq_place');
echo '.'; echo '.';
$this->oDB->exec('CREATE SEQUENCE seq_place start 100000'); $oDB->exec('CREATE SEQUENCE seq_place start 100000');
echo '.'; echo '.';
$sSQL = 'select distinct partition from country_name'; $sSQL = 'select distinct partition from country_name';
$aPartitions = $this->oDB->getCol($sSQL); $aPartitions = $oDB->getCol($sSQL);
if (!$this->bNoPartitions) $aPartitions[] = 0; if (!$this->bNoPartitions) $aPartitions[] = 0;
foreach ($aPartitions as $sPartition) { foreach ($aPartitions as $sPartition) {
$this->oDB->exec('TRUNCATE location_road_'.$sPartition); $oDB->exec('TRUNCATE location_road_'.$sPartition);
echo '.'; echo '.';
} }
// used by getorcreate_word_id to ignore frequent partial words // used by getorcreate_word_id to ignore frequent partial words
$sSQL = 'CREATE OR REPLACE FUNCTION get_maxwordfreq() RETURNS integer AS '; $sSQL = 'CREATE OR REPLACE FUNCTION get_maxwordfreq() RETURNS integer AS ';
$sSQL .= '$$ SELECT '.CONST_Max_Word_Frequency.' as maxwordfreq; $$ LANGUAGE SQL IMMUTABLE'; $sSQL .= '$$ SELECT '.CONST_Max_Word_Frequency.' as maxwordfreq; $$ LANGUAGE SQL IMMUTABLE';
$this->oDB->exec($sSQL); $oDB->exec($sSQL);
echo ".\n"; echo ".\n";
// pre-create the word list // pre-create the word list
@@ -415,13 +411,13 @@ class SetupFunctions
info('Reanalysing database'); info('Reanalysing database');
$this->pgsqlRunScript('ANALYSE'); $this->pgsqlRunScript('ANALYSE');
$sDatabaseDate = getDatabaseDate($this->oDB); $sDatabaseDate = getDatabaseDate($oDB);
$this->oDB->exec('TRUNCATE import_status'); $oDB->exec('TRUNCATE import_status');
if (!$sDatabaseDate) { if (!$sDatabaseDate) {
warn('could not determine database date.'); warn('could not determine database date.');
} else { } else {
$sSQL = "INSERT INTO import_status (lastimportdate) VALUES('".$sDatabaseDate."')"; $sSQL = "INSERT INTO import_status (lastimportdate) VALUES('".$sDatabaseDate."')";
$this->oDB->exec($sSQL); $oDB->exec($sSQL);
echo "Latest data imported from $sDatabaseDate.\n"; echo "Latest data imported from $sDatabaseDate.\n";
} }
} }
@@ -499,7 +495,7 @@ class SetupFunctions
public function calculatePostcodes($bCMDResultAll) public function calculatePostcodes($bCMDResultAll)
{ {
info('Calculate Postcodes'); info('Calculate Postcodes');
$this->oDB->exec('TRUNCATE location_postcode'); $this->db()->exec('TRUNCATE location_postcode');
$sSQL = 'INSERT INTO location_postcode'; $sSQL = 'INSERT INTO location_postcode';
$sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) '; $sSQL .= ' (place_id, indexed_status, country_code, postcode, geometry) ';
@@ -510,7 +506,7 @@ class SetupFunctions
$sSQL .= " WHERE address ? 'postcode' AND address->'postcode' NOT SIMILAR TO '%(,|;)%'"; $sSQL .= " WHERE address ? 'postcode' AND address->'postcode' NOT SIMILAR TO '%(,|;)%'";
$sSQL .= ' AND geometry IS NOT null'; $sSQL .= ' AND geometry IS NOT null';
$sSQL .= ' GROUP BY country_code, pc'; $sSQL .= ' GROUP BY country_code, pc';
$this->oDB->exec($sSQL); $this->db()->exec($sSQL);
// only add postcodes that are not yet available in OSM // only add postcodes that are not yet available in OSM
$sSQL = 'INSERT INTO location_postcode'; $sSQL = 'INSERT INTO location_postcode';
@@ -520,7 +516,7 @@ class SetupFunctions
$sSQL .= ' FROM us_postcode WHERE postcode NOT IN'; $sSQL .= ' FROM us_postcode WHERE postcode NOT IN';
$sSQL .= ' (SELECT postcode FROM location_postcode'; $sSQL .= ' (SELECT postcode FROM location_postcode';
$sSQL .= " WHERE country_code = 'us')"; $sSQL .= " WHERE country_code = 'us')";
$this->oDB->exec($sSQL); $this->db()->exec($sSQL);
// add missing postcodes for GB (if available) // add missing postcodes for GB (if available)
$sSQL = 'INSERT INTO location_postcode'; $sSQL = 'INSERT INTO location_postcode';
@@ -529,21 +525,23 @@ class SetupFunctions
$sSQL .= ' FROM gb_postcode WHERE postcode NOT IN'; $sSQL .= ' FROM gb_postcode WHERE postcode NOT IN';
$sSQL .= ' (SELECT postcode FROM location_postcode'; $sSQL .= ' (SELECT postcode FROM location_postcode';
$sSQL .= " WHERE country_code = 'gb')"; $sSQL .= " WHERE country_code = 'gb')";
$this->oDB->exec($sSQL); $this->db()->exec($sSQL);
if (!$bCMDResultAll) { if (!$bCMDResultAll) {
$sSQL = "DELETE FROM word WHERE class='place' and type='postcode'"; $sSQL = "DELETE FROM word WHERE class='place' and type='postcode'";
$sSQL .= 'and word NOT IN (SELECT postcode FROM location_postcode)'; $sSQL .= 'and word NOT IN (SELECT postcode FROM location_postcode)';
$this->oDB->exec($sSQL); $this->db()->exec($sSQL);
} }
$sSQL = 'SELECT count(getorcreate_postcode_id(v)) FROM '; $sSQL = 'SELECT count(getorcreate_postcode_id(v)) FROM ';
$sSQL .= '(SELECT distinct(postcode) as v FROM location_postcode) p'; $sSQL .= '(SELECT distinct(postcode) as v FROM location_postcode) p';
$this->oDB->exec($sSQL); $this->db()->exec($sSQL);
} }
public function index($bIndexNoanalyse) public function index($bIndexNoanalyse)
{ {
checkModulePresence(); // raises exception on failure
$oBaseCmd = (new \Nominatim\Shell(CONST_BasePath.'/nominatim/nominatim.py')) $oBaseCmd = (new \Nominatim\Shell(CONST_BasePath.'/nominatim/nominatim.py'))
->addParams('--database', $this->aDSNInfo['database']) ->addParams('--database', $this->aDSNInfo['database'])
->addParams('--port', $this->aDSNInfo['port']) ->addParams('--port', $this->aDSNInfo['port'])
@@ -592,7 +590,7 @@ class SetupFunctions
info('Index postcodes'); info('Index postcodes');
$sSQL = 'UPDATE location_postcode SET indexed_status = 0'; $sSQL = 'UPDATE location_postcode SET indexed_status = 0';
$this->oDB->exec($sSQL); $this->db()->exec($sSQL);
} }
public function createSearchIndices() public function createSearchIndices()
@@ -601,11 +599,11 @@ class SetupFunctions
$sSQL = 'SELECT relname FROM pg_class, pg_index '; $sSQL = 'SELECT relname FROM pg_class, pg_index ';
$sSQL .= 'WHERE pg_index.indisvalid = false AND pg_index.indexrelid = pg_class.oid'; $sSQL .= 'WHERE pg_index.indisvalid = false AND pg_index.indexrelid = pg_class.oid';
$aInvalidIndices = $this->oDB->getCol($sSQL); $aInvalidIndices = $this->db()->getCol($sSQL);
foreach ($aInvalidIndices as $sIndexName) { foreach ($aInvalidIndices as $sIndexName) {
info("Cleaning up invalid index $sIndexName"); info("Cleaning up invalid index $sIndexName");
$this->oDB->exec("DROP INDEX $sIndexName;"); $this->db()->exec("DROP INDEX $sIndexName;");
} }
$sTemplate = file_get_contents(CONST_BasePath.'/sql/indices.src.sql'); $sTemplate = file_get_contents(CONST_BasePath.'/sql/indices.src.sql');
@@ -675,7 +673,7 @@ class SetupFunctions
); );
$aDropTables = array(); $aDropTables = array();
$aHaveTables = $this->oDB->getListOfTables(); $aHaveTables = $this->db()->getListOfTables();
foreach ($aHaveTables as $sTable) { foreach ($aHaveTables as $sTable) {
$bFound = false; $bFound = false;
@@ -732,6 +730,24 @@ if (file_exists(getenv('NOMINATIM_SETTINGS'))) require_once(getenv('NOMINATIM_SE
info(CONST_InstallPath.'/settings/settings-frontend.php has been set up successfully'); info(CONST_InstallPath.'/settings/settings-frontend.php has been set up successfully');
} }
/**
* Return the connection to the database.
*
* @return Database object.
*
* Creates a new connection if none exists yet. Otherwise reuses the
* already established connection.
*/
private function db()
{
if (is_null($this->oDB)) {
$this->oDB = new \Nominatim\DB();
$this->oDB->connect();
}
return $this->oDB;
}
private function removeFlatnodeFile() private function removeFlatnodeFile()
{ {
if (!is_null(CONST_Osm2pgsql_Flatnode_File) && CONST_Osm2pgsql_Flatnode_File) { if (!is_null(CONST_Osm2pgsql_Flatnode_File) && CONST_Osm2pgsql_Flatnode_File) {
@@ -761,13 +777,13 @@ if (file_exists(getenv('NOMINATIM_SETTINGS'))) require_once(getenv('NOMINATIM_SE
$sTemplate .= file_get_contents($sBasePath.'importance.sql'); $sTemplate .= file_get_contents($sBasePath.'importance.sql');
$sTemplate .= file_get_contents($sBasePath.'address_lookup.sql'); $sTemplate .= file_get_contents($sBasePath.'address_lookup.sql');
$sTemplate .= file_get_contents($sBasePath.'interpolation.sql'); $sTemplate .= file_get_contents($sBasePath.'interpolation.sql');
if ($this->oDB->tableExists('place')) { if ($this->db()->tableExists('place')) {
$sTemplate .= file_get_contents($sBasePath.'place_triggers.sql'); $sTemplate .= file_get_contents($sBasePath.'place_triggers.sql');
} }
if ($this->oDB->tableExists('placex')) { if ($this->db()->tableExists('placex')) {
$sTemplate .= file_get_contents($sBasePath.'placex_triggers.sql'); $sTemplate .= file_get_contents($sBasePath.'placex_triggers.sql');
} }
if ($this->oDB->tableExists('location_postcode')) { if ($this->db()->tableExists('location_postcode')) {
$sTemplate .= file_get_contents($sBasePath.'postcode_triggers.sql'); $sTemplate .= file_get_contents($sBasePath.'postcode_triggers.sql');
} }
$sTemplate = str_replace('{modulepath}', $this->sModulePath, $sTemplate); $sTemplate = str_replace('{modulepath}', $this->sModulePath, $sTemplate);
@@ -796,7 +812,7 @@ if (file_exists(getenv('NOMINATIM_SETTINGS'))) require_once(getenv('NOMINATIM_SE
private function pgsqlRunPartitionScript($sTemplate) private function pgsqlRunPartitionScript($sTemplate)
{ {
$sSQL = 'select distinct partition from country_name'; $sSQL = 'select distinct partition from country_name';
$aPartitions = $this->oDB->getCol($sSQL); $aPartitions = $this->db()->getCol($sSQL);
if (!$this->bNoPartitions) $aPartitions[] = 0; if (!$this->bNoPartitions) $aPartitions[] = 0;
preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER); preg_match_all('#^-- start(.*?)^-- end#ms', $sTemplate, $aMatches, PREG_SET_ORDER);
@@ -902,13 +918,11 @@ if (file_exists(getenv('NOMINATIM_SETTINGS'))) require_once(getenv('NOMINATIM_SE
* @param string $sName Name of table to remove. * @param string $sName Name of table to remove.
* *
* @return null * @return null
*
* @pre connect() must have been called.
*/ */
private function dropTable($sName) private function dropTable($sName)
{ {
if ($this->bVerbose) echo "Dropping table $sName\n"; if ($this->bVerbose) echo "Dropping table $sName\n";
$this->oDB->deleteTable($sName); $this->db()->deleteTable($sName);
} }
/** /**
@@ -918,6 +932,6 @@ if (file_exists(getenv('NOMINATIM_SETTINGS'))) require_once(getenv('NOMINATIM_SE
*/ */
private function dbReverseOnly() private function dbReverseOnly()
{ {
return !($this->oDB->tableExists('search_name')); return !($this->db()->tableExists('search_name'));
} }
} }

View File

@@ -77,18 +77,11 @@ if ($aCMDResult['create-db'] || $aCMDResult['all']) {
$oSetup->createDB(); $oSetup->createDB();
} }
if (!$aCMDResult['setup-website']) {
$oSetup->connect();
// Try accessing the C module, so we know early if something is wrong
checkModulePresence(); // raises exception on failure
}
if ($aCMDResult['setup-db'] || $aCMDResult['all']) { if ($aCMDResult['setup-db'] || $aCMDResult['all']) {
$bDidSomething = true; $bDidSomething = true;
$oSetup->setupDB(); $oSetup->setupDB();
} }
if ($aCMDResult['import-data'] || $aCMDResult['all']) { if ($aCMDResult['import-data'] || $aCMDResult['all']) {
$bDidSomething = true; $bDidSomething = true;
$oSetup->importData($aCMDResult['osm-file']); $oSetup->importData($aCMDResult['osm-file']);