]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tools/admin.py
remove postcodes from countries that don't have them
[nominatim.git] / nominatim / tools / admin.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Functions for database analysis and maintenance.
9 """
10 import logging
11
12 from nominatim.errors import UsageError
13
14 LOG = logging.getLogger()
15
16 def analyse_indexing(conn, osm_id=None, place_id=None):
17     """ Analyse indexing of a single Nominatim object.
18     """
19     with conn.cursor() as cur:
20         if osm_id:
21             osm_type = osm_id[0].upper()
22             if osm_type not in 'NWR' or not osm_id[1:].isdigit():
23                 LOG.fatal('OSM ID must be of form <N|W|R><id>. Got: %s', osm_id)
24                 raise UsageError("OSM ID parameter badly formatted")
25             cur.execute('SELECT place_id FROM placex WHERE osm_type = %s AND osm_id = %s',
26                         (osm_type, osm_id[1:]))
27
28             if cur.rowcount < 1:
29                 LOG.fatal("OSM object %s not found in database.", osm_id)
30                 raise UsageError("OSM object not found")
31
32             place_id = cur.fetchone()[0]
33
34         if place_id is None:
35             LOG.fatal("No OSM object given to index.")
36             raise UsageError("OSM object not found")
37
38         cur.execute("update placex set indexed_status = 2 where place_id = %s",
39                     (place_id, ))
40
41         cur.execute("""SET auto_explain.log_min_duration = '0';
42                        SET auto_explain.log_analyze = 'true';
43                        SET auto_explain.log_nested_statements = 'true';
44                        LOAD 'auto_explain';
45                        SET client_min_messages = LOG;
46                        SET log_min_messages = FATAL""")
47
48         cur.execute("update placex set indexed_status = 0 where place_id = %s",
49                     (place_id, ))
50
51     # we do not want to keep the results
52     conn.rollback()
53
54     for msg in conn.notices:
55         print(msg)