Files
Nominatim/src/nominatim_db/tools/freeze.py
Itz-Agasta 45972811e3 Preserve import error tables during freeze
- Remove 'import_polygon_%' from UPDATE_TABLES to keep import_polygon_error
and import_polygon_delete tables in frozen databases.

- These tables contain permanent import error tracking data and should not
be deleted during freeze. The ro-access grant system expects them to exist
in all database states.
2026-01-31 22:50:18 +05:30

59 lines
1.5 KiB
Python

# SPDX-License-Identifier: GPL-3.0-or-later
#
# This file is part of Nominatim. (https://nominatim.org)
#
# Copyright (C) 2025 by the Nominatim developer community.
# For a full list of authors see the git log.
"""
Functions for removing unnecessary data from the database.
"""
from typing import Optional
from pathlib import Path
from psycopg import sql as pysql
from ..db.connection import Connection, drop_tables, table_exists
UPDATE_TABLES = [
'address_levels',
'gb_postcode',
'import_osmosis_log',
'location_area%',
'location_road%',
'place',
'place_entrance',
'place_postcode',
'planet_osm_%',
'search_name_%',
'us_postcode',
'wikipedia_%'
]
def drop_update_tables(conn: Connection) -> None:
""" Drop all tables only necessary for updating the database from
OSM replication data.
"""
parts = (pysql.SQL("(tablename LIKE {})").format(pysql.Literal(t)) for t in UPDATE_TABLES)
with conn.cursor() as cur:
cur.execute(pysql.SQL("SELECT tablename FROM pg_tables WHERE ")
+ pysql.SQL(' or ').join(parts))
tables = [r[0] for r in cur]
drop_tables(conn, *tables, cascade=True)
conn.commit()
def drop_flatnode_file(fpath: Optional[Path]) -> None:
""" Remove the flatnode file if it exists.
"""
if fpath and fpath.exists():
fpath.unlink()
def is_frozen(conn: Connection) -> bool:
""" Returns true if database is in a frozen state
"""
return table_exists(conn, 'place') is False