1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2025 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Helper functions for managing test databases.
11 from psycopg import sql as pysql
16 def __init__(self, purge=False):
19 def check_for_db(self, dbname):
20 """ Check if the given DB already exists.
21 When the purge option is set, then an existing database will
22 be deleted and the function returns that it does not exist.
28 return self.exists_db(dbname)
30 def drop_db(self, dbname):
31 """ Drop the given database if it exists.
33 with psycopg.connect(dbname='postgres') as conn:
34 conn.autocommit = True
35 conn.execute(pysql.SQL('DROP DATABASE IF EXISTS')
36 + pysql.Identifier(dbname))
38 def exists_db(self, dbname):
39 """ Check if a database with the given name exists already.
41 with psycopg.connect(dbname='postgres') as conn:
42 cur = conn.execute('select count(*) from pg_database where datname = %s',
44 return cur.fetchone()[0] == 1