]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tools/freeze.py
add git commit hash to --version output
[nominatim.git] / nominatim / tools / freeze.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 removing unnecessary data from the database.
9 """
10 from pathlib import Path
11
12 from psycopg2 import sql as pysql
13
14 UPDATE_TABLES = [
15     'address_levels',
16     'gb_postcode',
17     'import_osmosis_log',
18     'import_polygon_%',
19     'location_area%',
20     'location_road%',
21     'place',
22     'planet_osm_%',
23     'search_name_%',
24     'us_postcode',
25     'wikipedia_%'
26 ]
27
28 def drop_update_tables(conn):
29     """ Drop all tables only necessary for updating the database from
30         OSM replication data.
31     """
32     parts = (pysql.SQL("(tablename LIKE {})").format(pysql.Literal(t)) for t in UPDATE_TABLES)
33
34     with conn.cursor() as cur:
35         cur.execute(pysql.SQL("SELECT tablename FROM pg_tables WHERE ")
36                     + pysql.SQL(' or ').join(parts))
37         tables = [r[0] for r in cur]
38
39         for table in tables:
40             cur.drop_table(table, cascade=True)
41
42     conn.commit()
43
44
45 def drop_flatnode_file(fname):
46     """ Remove the flatnode file if it exists.
47     """
48     if fname:
49         fpath = Path(fname)
50         if fpath.exists():
51             fpath.unlink()