]> git.openstreetmap.org Git - nominatim.git/commitdiff
check-database also checks database vs nominatim version
authorMarc Tobias <mtmail@gmx.net>
Thu, 2 Nov 2023 15:19:47 +0000 (16:19 +0100)
committermarc tobias <mtmail@gmx.net>
Sat, 6 Jan 2024 19:56:56 +0000 (20:56 +0100)
nominatim/tools/check_database.py
test/python/tools/test_check_database.py

index b1cfce57b9f4531d80a4ee80b2aaa95cd6af21ff..721f2ceea0630b61ea18b9cb117e34f7577a9421 100644 (file)
@@ -13,9 +13,11 @@ from textwrap import dedent
 
 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}
index 4dd536b68daae4bf3bf9cea090f941afd5536e6f..65f0590320ad09a309b91ff7f693ddae1b2cebe5 100644 (file)
@@ -10,6 +10,7 @@ Tests for database integrity checks.
 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