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