Skip to content

Commit

Permalink
Check the current PgOSM Flex version from git against the latest impo…
Browse files Browse the repository at this point in the history
…rt tracked in pgosm_flex table.
  • Loading branch information
rustprooflabs committed Nov 22, 2023
1 parent 822ddd3 commit 77b0571
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ output/
**/__pycache__
pgosm-data/*
docs/book/*
.vscode/*
3 changes: 2 additions & 1 deletion docker/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,8 @@ def get_prior_import(schema_name: str) -> dict:
SELECT id, osm_date, region, layerset, import_status,
import_mode ->> 'replication' AS replication,
import_mode ->> 'update' AS use_update,
import_mode
import_mode,
split_part(pgosm_flex_version, '-', 1) AS pgosm_flex_version_no_hash
FROM {schema_name}.pgosm_flex
ORDER BY imported DESC
LIMIT 1
Expand Down
16 changes: 13 additions & 3 deletions docker/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,16 @@ def get_region_combined(region: str, subregion: str) -> str:
return pgosm_region


def get_git_info() -> str:
def get_git_info(tag_only: bool=False) -> str:
"""Provides git info in the form of the latest tag and most recent short sha
Sends info to logger and returns string.
Parameters
----------------------
tag_only : bool
When true, omits the short sha portion, only returning the tag.
Returns
----------------------
git_info : str
Expand All @@ -181,12 +186,17 @@ def get_git_info() -> str:
sha = repo.head.object.hexsha
short_sha = repo.git.rev_parse(sha, short=True)
latest_tag = repo.git.describe('--abbrev=0', tags=True)
git_info = f'{latest_tag}-{short_sha}'
except ValueError:
git_info = 'Git info unavailable'
logger.error('Unable to get git information.')

logger.info(f'PgOSM Flex version: {git_info}')
if tag_only:
git_info = latest_tag
else:
git_info = f'{latest_tag}-{short_sha}'
# Logging only this full version, not the tag_only run
logger.info(f'PgOSM Flex version: {git_info}')

return git_info


Expand Down
16 changes: 16 additions & 0 deletions docker/import_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"""
import logging
import json
from packaging.version import parse as parse_version

import helpers


class ImportMode():
Expand Down Expand Up @@ -83,6 +86,19 @@ def okay_to_run(self, prior_import: dict) -> bool:

prior_replication = prior_import['replication']

# Check git version against latest.
# If current version is lower than prior version from latest import, stop.
prior_import_version = prior_import['pgosm_flex_version_no_hash']
git_tag = helpers.get_git_info(tag_only=True)
if parse_version(git_tag) < parse_version(prior_import_version):
msg = f'PgOSM Flex version ({git_tag}) is lower than latest import'
msg += f' tracked in the pgosm_flex table ({prior_import_version}).'
msg += f' Use PgOSM Flex version {git_tag} or newer'
self.logger.error(msg)
return False
else:
self.logger.info(f'Prior import used PgOSM Flex: {prior_import_version}')

if self.replication:
if not prior_replication:
self.logger.error('Running w/ replication but prior import did not. Requires --force to proceed.')
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ coverage>=6.4.1
GitPython>=3.1.31
osm2pgsql-tuner==0.0.6
osmium>=3.4.1
packaging>=23.0
psycopg>=3.1
psycopg-binary>=3.1
sh>=1.14.2

0 comments on commit 77b0571

Please sign in to comment.