]> git.openstreetmap.org Git - nominatim.git/commitdiff
use import date from osm2pgsql property table if available
authorSarah Hoffmann <lonvia@denofr.de>
Tue, 5 Mar 2024 10:33:32 +0000 (11:33 +0100)
committerSarah Hoffmann <lonvia@denofr.de>
Tue, 5 Mar 2024 10:33:32 +0000 (11:33 +0100)
nominatim/clicmd/setup.py
nominatim/db/status.py
test/python/db/test_status.py

index 38a5a5b520470b0deff8a69365073f1830f93eb0..2fd8b141a86c85be4eb22e78a649145a07ca9c06 100644 (file)
@@ -219,12 +219,11 @@ class SetupAll:
         """ Determine the database date and set the status accordingly.
         """
         with connect(dsn) as conn:
         """ Determine the database date and set the status accordingly.
         """
         with connect(dsn) as conn:
-            if not offline:
-                try:
-                    dbdate = status.compute_database_date(conn)
-                    status.set_status(conn, dbdate)
-                    LOG.info('Database is at %s.', dbdate)
-                except Exception as exc: # pylint: disable=broad-except
-                    LOG.error('Cannot determine date of database: %s', exc)
-
             properties.set_property(conn, 'database_version', str(NOMINATIM_VERSION))
             properties.set_property(conn, 'database_version', str(NOMINATIM_VERSION))
+
+            try:
+                dbdate = status.compute_database_date(conn, offline)
+                status.set_status(conn, dbdate)
+                LOG.info('Database is at %s.', dbdate)
+            except Exception as exc: # pylint: disable=broad-except
+                LOG.error('Cannot determine date of database: %s', exc)
index 2c01de71466dca8fb237461509062c5c8c3e248a..5f92d9599ce2577ffcc5373256d5b85c17b06529 100644 (file)
@@ -29,11 +29,24 @@ class StatusRow(TypedDict):
     indexed: Optional[bool]
 
 
     indexed: Optional[bool]
 
 
-def compute_database_date(conn: Connection) -> dt.datetime:
+def compute_database_date(conn: Connection, offline: bool = False) -> dt.datetime:
     """ Determine the date of the database from the newest object in the
         data base.
     """
     """ Determine the date of the database from the newest object in the
         data base.
     """
-    # First, find the node with the highest ID in the database
+    # If there is a date from osm2pgsql available, use that.
+    if conn.table_exists('osm2pgsql_properties'):
+        with conn.cursor() as cur:
+            cur.execute(""" SELECT value FROM osm2pgsql_properties
+                            WHERE property = 'current_timestamp' """)
+            row = cur.fetchone()
+            if row is not None:
+                return dt.datetime.strptime(row[0], "%Y-%m-%dT%H:%M:%SZ")\
+                                  .replace(tzinfo=dt.timezone.utc)
+
+    if offline:
+        raise UsageError("Cannot determine database date from data in offline mode.")
+
+    # Else, find the node with the highest ID in the database
     with conn.cursor() as cur:
         if conn.table_exists('place'):
             osmid = cur.scalar("SELECT max(osm_id) FROM place WHERE osm_type='N'")
     with conn.cursor() as cur:
         if conn.table_exists('place'):
             osmid = cur.scalar("SELECT max(osm_id) FROM place WHERE osm_type='N'")
index 0cb12e0297889e4714aa302657e8863443b62bfe..05fb2c7f1c8bd47826559ab07dc708d148abae2c 100644 (file)
@@ -31,6 +31,22 @@ def setup_status_table(status_table):
     pass
 
 
     pass
 
 
+@pytest.mark.parametrize('offline', [True, False])
+def test_compute_database_date_from_osm2pgsql(table_factory, temp_db_conn, offline):
+    table_factory('osm2pgsql_properties', 'property TEXT, value TEXT',
+                  content=(('current_timestamp', '2024-01-03T23:45:54Z'), ))
+
+    date = nominatim.db.status.compute_database_date(temp_db_conn, offline=offline)
+    assert date == iso_date('2024-01-03T23:45:54')
+
+
+def test_compute_database_date_from_osm2pgsql_nodata(table_factory, temp_db_conn):
+    table_factory('osm2pgsql_properties', 'property TEXT, value TEXT')
+
+    with pytest.raises(UsageError, match='Cannot determine database date from data in offline mode'):
+        nominatim.db.status.compute_database_date(temp_db_conn, offline=True)
+
+
 def test_compute_database_date_place_empty(place_table, temp_db_conn):
     with pytest.raises(UsageError):
         nominatim.db.status.compute_database_date(temp_db_conn)
 def test_compute_database_date_place_empty(place_table, temp_db_conn):
     with pytest.raises(UsageError):
         nominatim.db.status.compute_database_date(temp_db_conn)