2022-01-03 18:23:58 +03:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
#
|
|
|
|
# This file is part of Nominatim. (https://nominatim.org)
|
|
|
|
#
|
|
|
|
# Copyright (C) 2022 by the Nominatim developer community.
|
|
|
|
# For a full list of authors see the git log.
|
2021-02-17 23:43:15 +03:00
|
|
|
"""
|
|
|
|
Functions for removing unnecessary data from the database.
|
|
|
|
"""
|
2022-07-03 20:04:05 +03:00
|
|
|
from typing import Optional
|
2021-02-17 23:43:15 +03:00
|
|
|
from pathlib import Path
|
|
|
|
|
2021-07-12 23:05:22 +03:00
|
|
|
from psycopg2 import sql as pysql
|
|
|
|
|
2022-07-03 20:04:05 +03:00
|
|
|
from nominatim.db.connection import Connection
|
|
|
|
|
2021-02-17 23:43:15 +03:00
|
|
|
UPDATE_TABLES = [
|
|
|
|
'address_levels',
|
|
|
|
'gb_postcode',
|
|
|
|
'import_osmosis_log',
|
|
|
|
'import_polygon_%',
|
|
|
|
'location_area%',
|
|
|
|
'location_road%',
|
|
|
|
'place',
|
|
|
|
'planet_osm_%',
|
|
|
|
'search_name_%',
|
|
|
|
'us_postcode',
|
|
|
|
'wikipedia_%'
|
|
|
|
]
|
|
|
|
|
2022-07-03 20:04:05 +03:00
|
|
|
def drop_update_tables(conn: Connection) -> None:
|
2021-02-17 23:43:15 +03:00
|
|
|
""" Drop all tables only necessary for updating the database from
|
|
|
|
OSM replication data.
|
|
|
|
"""
|
2021-07-12 23:05:22 +03:00
|
|
|
parts = (pysql.SQL("(tablename LIKE {})").format(pysql.Literal(t)) for t in UPDATE_TABLES)
|
2021-02-17 23:43:15 +03:00
|
|
|
|
|
|
|
with conn.cursor() as cur:
|
2021-07-12 23:05:22 +03:00
|
|
|
cur.execute(pysql.SQL("SELECT tablename FROM pg_tables WHERE ")
|
|
|
|
+ pysql.SQL(' or ').join(parts))
|
2021-02-17 23:43:15 +03:00
|
|
|
tables = [r[0] for r in cur]
|
|
|
|
|
|
|
|
for table in tables:
|
2021-07-12 21:32:46 +03:00
|
|
|
cur.drop_table(table, cascade=True)
|
2021-02-17 23:43:15 +03:00
|
|
|
|
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
|
2022-07-03 20:04:05 +03:00
|
|
|
def drop_flatnode_file(fpath: Optional[Path]) -> None:
|
2021-02-17 23:43:15 +03:00
|
|
|
""" Remove the flatnode file if it exists.
|
|
|
|
"""
|
2022-07-03 19:36:33 +03:00
|
|
|
if fpath and fpath.exists():
|
|
|
|
fpath.unlink()
|
2023-05-02 19:37:36 +03:00
|
|
|
|
|
|
|
def is_frozen(conn: Connection) -> bool:
|
|
|
|
""" Returns true if database is in a frozen state
|
|
|
|
"""
|
|
|
|
|
|
|
|
return conn.table_exists('place') is False
|