Skip to content

Commit e929693

Browse files
authored
Merge pull request #3356 from lonvia/use-date-from-osm2pgsql-prop
Use import date from osm2pgsql property table if available
2 parents 4d5faf9 + ae7c584 commit e929693

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

nominatim/clicmd/setup.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,11 @@ def _finalize_database(self, dsn: str, offline: bool) -> None:
219219
""" Determine the database date and set the status accordingly.
220220
"""
221221
with connect(dsn) as conn:
222-
if not offline:
223-
try:
224-
dbdate = status.compute_database_date(conn)
225-
status.set_status(conn, dbdate)
226-
LOG.info('Database is at %s.', dbdate)
227-
except Exception as exc: # pylint: disable=broad-except
228-
LOG.error('Cannot determine date of database: %s', exc)
229-
230222
properties.set_property(conn, 'database_version', str(NOMINATIM_VERSION))
223+
224+
try:
225+
dbdate = status.compute_database_date(conn, offline)
226+
status.set_status(conn, dbdate)
227+
LOG.info('Database is at %s.', dbdate)
228+
except Exception as exc: # pylint: disable=broad-except
229+
LOG.error('Cannot determine date of database: %s', exc)

nominatim/db/status.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,24 @@ class StatusRow(TypedDict):
2929
indexed: Optional[bool]
3030

3131

32-
def compute_database_date(conn: Connection) -> dt.datetime:
32+
def compute_database_date(conn: Connection, offline: bool = False) -> dt.datetime:
3333
""" Determine the date of the database from the newest object in the
3434
data base.
3535
"""
36-
# First, find the node with the highest ID in the database
36+
# If there is a date from osm2pgsql available, use that.
37+
if conn.table_exists('osm2pgsql_properties'):
38+
with conn.cursor() as cur:
39+
cur.execute(""" SELECT value FROM osm2pgsql_properties
40+
WHERE property = 'current_timestamp' """)
41+
row = cur.fetchone()
42+
if row is not None:
43+
return dt.datetime.strptime(row[0], "%Y-%m-%dT%H:%M:%SZ")\
44+
.replace(tzinfo=dt.timezone.utc)
45+
46+
if offline:
47+
raise UsageError("Cannot determine database date from data in offline mode.")
48+
49+
# Else, find the node with the highest ID in the database
3750
with conn.cursor() as cur:
3851
if conn.table_exists('place'):
3952
osmid = cur.scalar("SELECT max(osm_id) FROM place WHERE osm_type='N'")

test/python/db/test_status.py

+16
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ def setup_status_table(status_table):
3131
pass
3232

3333

34+
@pytest.mark.parametrize('offline', [True, False])
35+
def test_compute_database_date_from_osm2pgsql(table_factory, temp_db_conn, offline):
36+
table_factory('osm2pgsql_properties', 'property TEXT, value TEXT',
37+
content=(('current_timestamp', '2024-01-03T23:45:54Z'), ))
38+
39+
date = nominatim.db.status.compute_database_date(temp_db_conn, offline=offline)
40+
assert date == iso_date('2024-01-03T23:45:54')
41+
42+
43+
def test_compute_database_date_from_osm2pgsql_nodata(table_factory, temp_db_conn):
44+
table_factory('osm2pgsql_properties', 'property TEXT, value TEXT')
45+
46+
with pytest.raises(UsageError, match='Cannot determine database date from data in offline mode'):
47+
nominatim.db.status.compute_database_date(temp_db_conn, offline=True)
48+
49+
3450
def test_compute_database_date_place_empty(place_table, temp_db_conn):
3551
with pytest.raises(UsageError):
3652
nominatim.db.status.compute_database_date(temp_db_conn)

0 commit comments

Comments
 (0)