From 1d7e078a2c9a12c99625a31887f23a23028124d3 Mon Sep 17 00:00:00 2001 From: Marc Tobias Date: Thu, 2 Nov 2023 16:19:47 +0100 Subject: [PATCH] check-database also checks database vs nominatim version --- nominatim/tools/check_database.py | 45 +++++++++++++++++++++++- test/python/tools/test_check_database.py | 15 ++++++-- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/nominatim/tools/check_database.py b/nominatim/tools/check_database.py index b1cfce57b9..721f2ceea0 100644 --- a/nominatim/tools/check_database.py +++ b/nominatim/tools/check_database.py @@ -13,9 +13,11 @@ from nominatim.config import Configuration from nominatim.db.connection import connect, Connection +from nominatim.db import properties from nominatim.errors import UsageError from nominatim.tokenizer import factory as tokenizer_factory from nominatim.tools import freeze +from nominatim.version import NOMINATIM_VERSION, parse_version CHECKLIST = [] @@ -146,11 +148,52 @@ def check_connection(conn: Any, config: Configuration) -> CheckResult: return CheckState.OK +@_check(hint="""\ + Database version ({db_version}) doesn't match Nominatim version ({nom_version}) + + Hints: + * Are you connecting to the correct database? + + {instruction} + + Check the Migration chapter of the Administration Guide. + + Project directory: {config.project_dir} + Current setting of NOMINATIM_DATABASE_DSN: {config.DATABASE_DSN} + """) +def check_database_version(conn: Connection, config: Configuration) -> CheckResult: + """ Checking database_version matches Nominatim software version + """ + + if conn.table_exists('nominatim_properties'): + db_version_str = properties.get_property(conn, 'database_version') + else: + db_version_str = None + + if db_version_str is not None: + db_version = parse_version(db_version_str) + + if db_version == NOMINATIM_VERSION: + return CheckState.OK + + instruction = ( + 'Run migrations: nominatim admin --migrate' + if db_version < NOMINATIM_VERSION + else 'You need to upgrade the Nominatim software.' + ) + else: + instruction = '' + + return CheckState.FATAL, dict(db_version=db_version_str, + nom_version=NOMINATIM_VERSION, + instruction=instruction, + config=config) + @_check(hint="""\ placex table not found Hints: - * Are you connecting to the right database? + * Are you connecting to the correct database? * Did the import process finish without errors? Project directory: {config.project_dir} diff --git a/test/python/tools/test_check_database.py b/test/python/tools/test_check_database.py index 4dd536b68d..65f0590320 100644 --- a/test/python/tools/test_check_database.py +++ b/test/python/tools/test_check_database.py @@ -10,6 +10,7 @@ import pytest from nominatim.tools import check_database as chkdb +import nominatim.version def test_check_database_unknown_db(def_config, monkeypatch): monkeypatch.setenv('NOMINATIM_DATABASE_DSN', 'pgsql:dbname=fjgkhughwgh2423gsags') @@ -20,15 +21,25 @@ def test_check_database_fatal_test(def_config, temp_db): assert chkdb.check_database(def_config) == 1 -def test_check_conection_good(temp_db_conn, def_config): +def test_check_connection_good(temp_db_conn, def_config): assert chkdb.check_connection(temp_db_conn, def_config) == chkdb.CheckState.OK -def test_check_conection_bad(def_config): +def test_check_connection_bad(def_config): badconn = chkdb._BadConnection('Error') assert chkdb.check_connection(badconn, def_config) == chkdb.CheckState.FATAL +def test_check_database_version_good(property_table, temp_db_conn, def_config): + property_table.set('database_version', + '{0[0]}.{0[1]}.{0[2]}-{0[3]}'.format(nominatim.version.NOMINATIM_VERSION)) + assert chkdb.check_database_version(temp_db_conn, def_config) == chkdb.CheckState.OK + +def test_check_database_version_bad(property_table, temp_db_conn, def_config): + property_table.set('database_version', '3.9.9-9') + assert chkdb.check_database_version(temp_db_conn, def_config) == chkdb.CheckState.FATAL + + def test_check_placex_table_good(table_factory, temp_db_conn, def_config): table_factory('placex') assert chkdb.check_placex_table(temp_db_conn, def_config) == chkdb.CheckState.OK