Skip to content

Commit

Permalink
Merge pull request #3238 from mtmail/check-database-for-version-match
Browse files Browse the repository at this point in the history
admin --check-database also checks database vs nominatim version
  • Loading branch information
lonvia authored Jan 7, 2024
2 parents d3a5753 + 1d7e078 commit dc52d09
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
45 changes: 44 additions & 1 deletion nominatim/tools/check_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down Expand Up @@ -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}
Expand Down
15 changes: 13 additions & 2 deletions test/python/tools/test_check_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand Down

0 comments on commit dc52d09

Please sign in to comment.