1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for migration functions
12 from nominatim_db.tools import migration
13 from nominatim_db.errors import UsageError
14 from nominatim_db.db.connection import server_version_tuple
15 import nominatim_db.version
19 def update_sql_functions(self, config):
24 def postprocess_mock(monkeypatch):
25 monkeypatch.setattr(migration.refresh, 'create_functions', lambda *args: args)
26 monkeypatch.setattr(migration.tokenizer_factory, 'get_tokenizer_for_db',
27 lambda *args: DummyTokenizer())
30 def test_no_migration_old_versions(temp_db_with_extensions, def_config, property_table):
31 property_table.set('database_version', '4.2.99-0')
33 with pytest.raises(UsageError, match='Migration not possible'):
34 migration.migrate(def_config, {})
37 def test_already_at_version(temp_db_with_extensions, def_config, property_table):
39 property_table.set('database_version',
40 str(nominatim_db.version.NOMINATIM_VERSION))
42 assert migration.migrate(def_config, {}) == 0
45 def test_run_single_migration(temp_db_with_extensions, def_config, temp_db_cursor,
46 property_table, monkeypatch, postprocess_mock):
47 oldversion = [4, 4, 99, 0]
48 property_table.set('database_version',
49 str(nominatim_db.version.NominatimVersion(*oldversion)))
51 done = {'old': False, 'new': False}
53 """ Dummy migration"""
56 def _old_migration(**_):
57 """ Dummy migration"""
61 monkeypatch.setattr(migration, '_MIGRATION_FUNCTIONS',
62 [(tuple(oldversion), _old_migration),
63 (nominatim_db.version.NOMINATIM_VERSION, _migration)])
65 assert migration.migrate(def_config, {}) == 0
68 assert not done['old']
69 assert property_table.get('database_version') == str(nominatim_db.version.NOMINATIM_VERSION)
72 ###### Tests for specific migrations
74 # Each migration should come with two tests:
75 # 1. Test that migration from old to new state works as expected.
76 # 2. Test that the migration can be rerun on the new state without side effects.