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