]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tools/freeze.py
interpret stand-alone special terms always as near term
[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 typing import Optional
11 from pathlib import Path
12
13 from psycopg2 import sql as pysql
14
15 from nominatim.db.connection import Connection
16
17 UPDATE_TABLES = [
18     'address_levels',
19     'gb_postcode',
20     'import_osmosis_log',
21     'import_polygon_%',
22     'location_area%',
23     'location_road%',
24     'place',
25     'planet_osm_%',
26     'search_name_%',
27     'us_postcode',
28     'wikipedia_%'
29 ]
30
31 def drop_update_tables(conn: Connection) -> None:
32     """ Drop all tables only necessary for updating the database from
33         OSM replication data.
34     """
35     parts = (pysql.SQL("(tablename LIKE {})").format(pysql.Literal(t)) for t in UPDATE_TABLES)
36
37     with conn.cursor() as cur:
38         cur.execute(pysql.SQL("SELECT tablename FROM pg_tables WHERE ")
39                     + pysql.SQL(' or ').join(parts))
40         tables = [r[0] for r in cur]
41
42         for table in tables:
43             cur.drop_table(table, cascade=True)
44
45     conn.commit()
46
47
48 def drop_flatnode_file(fpath: Optional[Path]) -> None:
49     """ Remove the flatnode file if it exists.
50     """
51     if fpath and fpath.exists():
52         fpath.unlink()
53
54 def is_frozen(conn: Connection) -> bool:
55     """ Returns true if database is in a frozen state
56     """
57
58     return conn.table_exists('place') is False