]> git.openstreetmap.org Git - nominatim.git/commitdiff
simplify constructor for SQL preprocessor
authorSarah Hoffmann <lonvia@denofr.de>
Mon, 19 Apr 2021 07:38:17 +0000 (09:38 +0200)
committerSarah Hoffmann <lonvia@denofr.de>
Mon, 19 Apr 2021 08:26:25 +0000 (10:26 +0200)
Use sql path from config.

nominatim/db/sql_preprocessor.py
nominatim/tools/database_import.py
nominatim/tools/refresh.py
nominatim/tools/tiger_data.py
test/python/conftest.py
test/python/test_tools_refresh_create_functions.py

index 7ffe88818ba94a9538a9f8a61e7e8bb658e55e00..c7009b34fc0a7b1e9d8a3a0717d8ac875a7c9b41 100644 (file)
@@ -75,9 +75,9 @@ class SQLPreprocessor: # pylint: disable=too-few-public-methods
         and follows its syntax.
     """
 
-    def __init__(self, conn, config, sqllib_dir):
+    def __init__(self, conn, config):
         self.env = jinja2.Environment(autoescape=False,
-                                      loader=jinja2.FileSystemLoader(str(sqllib_dir)))
+                                      loader=jinja2.FileSystemLoader(str(config.lib_dir.sql)))
 
         db_info = {}
         db_info['partitions'] = _get_partitions(conn)
index 964bc7026cff88dcab26b006331ef3515b2621d4..c8a4e51e44ae10e5d6336aeafc5df3507830fc2f 100644 (file)
@@ -184,7 +184,7 @@ def create_tables(conn, config, sqllib_dir, reverse_only=False):
         When `reverse_only` is True, then the main table for searching will
         be skipped and only reverse search is possible.
     """
-    sql = SQLPreprocessor(conn, config, sqllib_dir)
+    sql = SQLPreprocessor(conn, config)
     sql.env.globals['db']['reverse_only'] = reverse_only
 
     sql.run_sql_file(conn, 'tables.sql')
@@ -194,14 +194,14 @@ def create_table_triggers(conn, config, sqllib_dir):
     """ Create the triggers for the tables. The trigger functions must already
         have been imported with refresh.create_functions().
     """
-    sql = SQLPreprocessor(conn, config, sqllib_dir)
+    sql = SQLPreprocessor(conn, config)
     sql.run_sql_file(conn, 'table-triggers.sql')
 
 
 def create_partition_tables(conn, config, sqllib_dir):
     """ Create tables that have explicit partitioning.
     """
-    sql = SQLPreprocessor(conn, config, sqllib_dir)
+    sql = SQLPreprocessor(conn, config)
     sql.run_sql_file(conn, 'partition-tables.src.sql')
 
 
@@ -303,7 +303,7 @@ def create_search_indices(conn, config, sqllib_dir, drop=False):
             cur.execute('DROP INDEX "{}"'.format(idx))
     conn.commit()
 
-    sql = SQLPreprocessor(conn, config, sqllib_dir)
+    sql = SQLPreprocessor(conn, config)
 
     sql.run_sql_file(conn, 'indices.sql', drop=drop)
 
index 77eecf0457119c5d338af71c160659beca9f9a44..45aa1504652cfe0045d29e9d5625152f7c1bbd06 100644 (file)
@@ -81,7 +81,7 @@ def create_functions(conn, config, sqllib_dir,
                      enable_diff_updates=True, enable_debug=False):
     """ (Re)create the PL/pgSQL functions.
     """
-    sql = SQLPreprocessor(conn, config, sqllib_dir)
+    sql = SQLPreprocessor(conn, config)
 
     sql.run_sql_file(conn, 'functions.sql',
                      disable_diff_updates=not enable_diff_updates,
index 90789e79e6165cb5254ff7eb0bc2feb967053ab7..07772c702a53f3a554cb8a42f54f9de388da56cd 100644 (file)
@@ -86,7 +86,7 @@ def add_tiger_data(data_dir, config, threads):
         return
 
     with connect(dsn) as conn:
-        sql = SQLPreprocessor(conn, config, config.lib_dir.sql)
+        sql = SQLPreprocessor(conn, config)
         sql.run_sql_file(conn, 'tiger_import_start.sql')
 
     # Reading sql_files and then for each file line handling
@@ -116,5 +116,5 @@ def add_tiger_data(data_dir, config, threads):
     print('\n')
     LOG.warning("Creating indexes on Tiger data")
     with connect(dsn) as conn:
-        sql = SQLPreprocessor(conn, config, config.lib_dir.sql)
+        sql = SQLPreprocessor(conn, config)
         sql.run_sql_file(conn, 'tiger_import_finish.sql')
index 2a2d212960f3b1ad0312c892638b5a5188abaa7c..4b9749c01f4c2f2d9159f30fc53961107705ce90 100644 (file)
@@ -280,7 +280,11 @@ def osm2pgsql_options(temp_db):
                                  main_data='', main_index=''))
 
 @pytest.fixture
-def sql_preprocessor(temp_db_conn, tmp_path, def_config, monkeypatch, table_factory):
+def sql_preprocessor(temp_db_conn, tmp_path, monkeypatch, table_factory):
     monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', '.')
     table_factory('country_name', 'partition INT', (0, 1, 2))
-    return SQLPreprocessor(temp_db_conn, def_config, tmp_path)
+    cfg = Configuration(None, SRC_DIR.resolve() / 'settings')
+    cfg.set_libdirs(module='.', osm2pgsql='.', php=SRC_DIR / 'lib-php',
+                    sql=tmp_path, data=SRC_DIR / 'data')
+
+    return SQLPreprocessor(temp_db_conn, cfg)
index 40d4c81af315a8014a6571ce5891e1f954f707a9..53641cf983f883f0afe175df2287a76f11cb2b88 100644 (file)
@@ -5,6 +5,11 @@ import pytest
 
 from nominatim.tools.refresh import create_functions
 
+@pytest.fixture
+def sql_tmp_path(tmp_path, def_config):
+    def_config.lib_dir.sql = tmp_path
+    return tmp_path
+
 @pytest.fixture
 def conn(temp_db_conn, table_factory, monkeypatch):
     monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', '.')
@@ -12,8 +17,8 @@ def conn(temp_db_conn, table_factory, monkeypatch):
     return temp_db_conn
 
 
-def test_create_functions(temp_db_cursor, conn, def_config, tmp_path):
-    sqlfile = tmp_path / 'functions.sql'
+def test_create_functions(temp_db_cursor, conn, def_config, sql_tmp_path):
+    sqlfile = sql_tmp_path / 'functions.sql'
     sqlfile.write_text("""CREATE OR REPLACE FUNCTION test() RETURNS INTEGER
                           AS $$
                           BEGIN
@@ -22,14 +27,14 @@ def test_create_functions(temp_db_cursor, conn, def_config, tmp_path):
                           $$ LANGUAGE plpgsql IMMUTABLE;
                        """)
 
-    create_functions(conn, def_config, tmp_path)
+    create_functions(conn, def_config, sql_tmp_path)
 
     assert temp_db_cursor.scalar('SELECT test()') == 43
 
 
 @pytest.mark.parametrize("dbg,ret", ((True, 43), (False, 22)))
-def test_create_functions_with_template(temp_db_cursor, conn, def_config, tmp_path, dbg, ret):
-    sqlfile = tmp_path / 'functions.sql'
+def test_create_functions_with_template(temp_db_cursor, conn, def_config, sql_tmp_path, dbg, ret):
+    sqlfile = sql_tmp_path / 'functions.sql'
     sqlfile.write_text("""CREATE OR REPLACE FUNCTION test() RETURNS INTEGER
                           AS $$
                           BEGIN
@@ -42,6 +47,6 @@ def test_create_functions_with_template(temp_db_cursor, conn, def_config, tmp_pa
                           $$ LANGUAGE plpgsql IMMUTABLE;
                        """)
 
-    create_functions(conn, def_config, tmp_path, enable_debug=dbg)
+    create_functions(conn, def_config, sql_tmp_path, enable_debug=dbg)
 
     assert temp_db_cursor.scalar('SELECT test()') == ret