]> git.openstreetmap.org Git - nominatim.git/blobdiff - nominatim/tools/admin.py
factor out SQL for filtering by location
[nominatim.git] / nominatim / tools / admin.py
index 49ba75261b92d212f09a3681969b152da1b84cd7..c37d0811ff84b0ebb7276c4b90293f8bf71369d6 100644 (file)
@@ -7,10 +7,11 @@
 """
 Functions for database analysis and maintenance.
 """
-from typing import Optional, Tuple, Any
+from typing import Optional, Tuple, Any, cast
 import logging
 
 from psycopg2.extras import Json, register_hstore
+from psycopg2 import DataError
 
 from nominatim.config import Configuration
 from nominatim.db.connection import connect, Cursor
@@ -49,7 +50,7 @@ def _get_place_info(cursor: Cursor, osm_id: Optional[str],
         LOG.fatal("OSM object %s not found in database.", osm_id)
         raise UsageError("OSM object not found")
 
-    return cursor.fetchone() # type: ignore[no-untyped-call]
+    return cast(DictCursorResult, cursor.fetchone())
 
 
 def analyse_indexing(config: Configuration, osm_id: Optional[str] = None,
@@ -87,3 +88,19 @@ def analyse_indexing(config: Configuration, osm_id: Optional[str] = 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()