Skip to content

Commit 1cd1f98

Browse files
Merge pull request #371 from rustprooflabs/improve-version-checks
Add check against prior PgOSM Flex version with replication
2 parents 822ddd3 + b4a709b commit 1cd1f98

File tree

6 files changed

+56
-8
lines changed

6 files changed

+56
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ output/
44
**/__pycache__
55
pgosm-data/*
66
docs/book/*
7+
.vscode/*

docker/db.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,8 @@ def get_prior_import(schema_name: str) -> dict:
656656
SELECT id, osm_date, region, layerset, import_status,
657657
import_mode ->> 'replication' AS replication,
658658
import_mode ->> 'update' AS use_update,
659-
import_mode
659+
import_mode,
660+
split_part(pgosm_flex_version, '-', 1) AS pgosm_flex_version_no_hash
660661
FROM {schema_name}.pgosm_flex
661662
ORDER BY imported DESC
662663
LIMIT 1

docker/helpers.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,27 +166,45 @@ def get_region_combined(region: str, subregion: str) -> str:
166166
return pgosm_region
167167

168168

169-
def get_git_info() -> str:
169+
def get_git_info(tag_only: bool=False) -> str:
170170
"""Provides git info in the form of the latest tag and most recent short sha
171171
172172
Sends info to logger and returns string.
173173
174+
Parameters
175+
----------------------
176+
tag_only : bool
177+
When true, omits the short sha portion, only returning the tag.
178+
174179
Returns
175180
----------------------
176181
git_info : str
177182
"""
178183
logger = logging.getLogger('pgosm-flex')
179-
repo = git.Repo()
184+
185+
try:
186+
repo = git.Repo()
187+
except git.exc.InvalidGitRepositoryError:
188+
# This error happens when running via make for some reason...
189+
# This appears to fix it.
190+
repo = git.Repo('../')
191+
180192
try:
181193
sha = repo.head.object.hexsha
182194
short_sha = repo.git.rev_parse(sha, short=True)
183195
latest_tag = repo.git.describe('--abbrev=0', tags=True)
184-
git_info = f'{latest_tag}-{short_sha}'
185196
except ValueError:
186197
git_info = 'Git info unavailable'
187198
logger.error('Unable to get git information.')
199+
return '-- (version unknown) --'
200+
201+
if tag_only:
202+
git_info = latest_tag
203+
else:
204+
git_info = f'{latest_tag}-{short_sha}'
205+
# Logging only this full version, not the tag_only run
206+
logger.info(f'PgOSM Flex version: {git_info}')
188207

189-
logger.info(f'PgOSM Flex version: {git_info}')
190208
return git_info
191209

192210

docker/import_mode.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"""
33
import logging
44
import json
5+
from packaging.version import parse as parse_version
6+
7+
import helpers
58

69

710
class ImportMode():
@@ -83,6 +86,24 @@ def okay_to_run(self, prior_import: dict) -> bool:
8386

8487
prior_replication = prior_import['replication']
8588

89+
# Check git version against latest.
90+
# If current version is lower than prior version from latest import, stop.
91+
prior_import_version = prior_import['pgosm_flex_version_no_hash']
92+
git_tag = helpers.get_git_info(tag_only=True)
93+
94+
if git_tag == '-- (version unknown) --':
95+
msg = 'Unable to detect PgOSM Flex version from Git.'
96+
msg += ' Not enforcing version check against prior version.'
97+
self.logger.warning(msg)
98+
elif parse_version(git_tag) < parse_version(prior_import_version):
99+
msg = f'PgOSM Flex version ({git_tag}) is lower than latest import'
100+
msg += f' tracked in the pgosm_flex table ({prior_import_version}).'
101+
msg += f' Use PgOSM Flex version {prior_import_version} or newer'
102+
self.logger.error(msg)
103+
return False
104+
else:
105+
self.logger.info(f'Prior import used PgOSM Flex: {prior_import_version}')
106+
86107
if self.replication:
87108
if not prior_replication:
88109
self.logger.error('Running w/ replication but prior import did not. Requires --force to proceed.')

docker/tests/test_import_mode.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ def test_import_mode_okay_to_run_returns_false_when_prior_record_not_replication
146146
This should return False to avoid overwriting data.
147147
"""
148148
replication = True
149-
prior_import = {'replication': False}
149+
prior_import = {'replication': False,
150+
'pgosm_flex_version_no_hash': '99.99.99'
151+
}
150152
replication_update = False
151153
update = None
152154
force = False
@@ -170,7 +172,9 @@ def test_import_mode_okay_to_run_returns_true_when_replication_prior_record_repl
170172
This should return True to allow replication to updated
171173
"""
172174
replication = True
173-
prior_import = {'replication': True}
175+
prior_import = {'replication': True,
176+
'pgosm_flex_version_no_hash': '99.99.99'
177+
}
174178
replication_update = False
175179
update = None
176180
force = False
@@ -193,7 +197,9 @@ def test_import_mode_okay_to_run_returns_false_when_prior_import(self):
193197
This should return False to protect the data.
194198
"""
195199
replication = False
196-
prior_import = {'replication': False}
200+
prior_import = {'replication': False,
201+
'pgosm_flex_version_no_hash': '99.99.99'
202+
}
197203
replication_update = False
198204
update = None
199205
force = False

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ coverage>=6.4.1
33
GitPython>=3.1.31
44
osm2pgsql-tuner==0.0.6
55
osmium>=3.4.1
6+
packaging>=23.0
67
psycopg>=3.1
78
psycopg-binary>=3.1
89
sh>=1.14.2

0 commit comments

Comments
 (0)