X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/b1903f0fbf1e41c594be9b20f00b8a621dfe7552..bdded69ab636ab8e3bb46322ebbe4d2f9ec41614:/nominatim/tools/admin.py?ds=sidebyside diff --git a/nominatim/tools/admin.py b/nominatim/tools/admin.py index 1bf217e2..c37d0811 100644 --- a/nominatim/tools/admin.py +++ b/nominatim/tools/admin.py @@ -7,22 +7,28 @@ """ Functions for database analysis and maintenance. """ +from typing import Optional, Tuple, Any, cast import logging from psycopg2.extras import Json, register_hstore +from psycopg2 import DataError -from nominatim.db.connection import connect +from nominatim.config import Configuration +from nominatim.db.connection import connect, Cursor from nominatim.tokenizer import factory as tokenizer_factory from nominatim.errors import UsageError from nominatim.data.place_info import PlaceInfo +from nominatim.typing import DictCursorResult LOG = logging.getLogger() -def _get_place_info(cursor, osm_id, place_id): +def _get_place_info(cursor: Cursor, osm_id: Optional[str], + place_id: Optional[int]) -> DictCursorResult: sql = """SELECT place_id, extra.* FROM placex, LATERAL placex_indexing_prepare(placex) as extra """ + values: Tuple[Any, ...] if osm_id: osm_type = osm_id[0].upper() if osm_type not in 'NWR' or not osm_id[1:].isdigit(): @@ -44,10 +50,11 @@ def _get_place_info(cursor, osm_id, place_id): LOG.fatal("OSM object %s not found in database.", osm_id) raise UsageError("OSM object not found") - return cursor.fetchone() + return cast(DictCursorResult, cursor.fetchone()) -def analyse_indexing(config, osm_id=None, place_id=None): +def analyse_indexing(config: Configuration, osm_id: Optional[str] = None, + place_id: Optional[int] = None) -> None: """ Analyse indexing of a single Nominatim object. """ with connect(config.get_libpq_dsn()) as conn: @@ -81,3 +88,19 @@ def analyse_indexing(config, osm_id=None, place_id=None): for msg in conn.notices: print(msg) + + +def clean_deleted_relations(config: Configuration, age: str) -> None: + """ Clean deleted relations older than a given age + """ + with connect(config.get_libpq_dsn()) as conn: + with conn.cursor() as cur: + try: + cur.execute("""SELECT place_force_delete(p.place_id) + FROM import_polygon_delete d, placex p + WHERE p.osm_type = d.osm_type AND p.osm_id = d.osm_id + AND age(p.indexed_date) > %s::interval""", + (age, )) + except DataError as exc: + raise UsageError('Invalid PostgreSQL time interval format') from exc + conn.commit()