diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 1e48a032..9a4334bc 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -15,6 +15,7 @@ - Added more test cases for Aggregation (#499) - fix translator initialization - List Addix among adopters (#649) +- Added quantum leap version in metadata (#615) ### Bug fixes diff --git a/src/_version.py b/src/_version.py new file mode 100644 index 00000000..f48ededb --- /dev/null +++ b/src/_version.py @@ -0,0 +1,6 @@ +# Most recent official release +__version_info__ = ('0', '8', '3') +__version__ = '.'.join(__version_info__) + +# Current dev version for the next release +__dev_version__ = '0.9.0-dev' diff --git a/src/reporter/tests/test_version.py b/src/reporter/tests/test_version.py index 75f484f8..85975ba0 100644 --- a/src/reporter/tests/test_version.py +++ b/src/reporter/tests/test_version.py @@ -1,4 +1,5 @@ from conftest import QL_BASE_URL +from _version import __dev_version__ import requests @@ -7,5 +8,5 @@ def test_version(): r = requests.get('{}'.format(version_url)) assert r.status_code == 200, r.text assert r.json() == { - "version": "0.9.0-dev" + "version": __dev_version__ } diff --git a/src/reporter/version.py b/src/reporter/version.py index 20ea9efa..6d29722f 100644 --- a/src/reporter/version.py +++ b/src/reporter/version.py @@ -1,6 +1,7 @@ +from _version import __dev_version__ def version(): return { - 'version': '0.9.0-dev' + 'version': __dev_version__ } diff --git a/src/translators/crate.py b/src/translators/crate.py index 0aecc820..4644a052 100644 --- a/src/translators/crate.py +++ b/src/translators/crate.py @@ -241,19 +241,19 @@ def _build_original_data_value(self, entity: dict, def _create_metadata_table(self): stmt = "create table if not exists {} " \ - "(table_name string primary key, entity_attrs object) " \ + "(table_name string primary key, version text, entity_attrs object) " \ "with (" \ "number_of_replicas = '2-all', " \ "column_policy = 'dynamic')" op = stmt.format(METADATA_TABLE_NAME) self.cursor.execute(op) - def _store_metadata(self, table_name, persisted_metadata): - stmt = "insert into {} (table_name, entity_attrs) values (?,?) " \ + def _store_metadata(self, table_name, version, persisted_metadata): + stmt = "insert into {} (table_name, version, entity_attrs) values (?,?,?) " \ "on conflict(table_name) " \ "DO UPDATE SET entity_attrs = excluded.entity_attrs" stmt = stmt.format(METADATA_TABLE_NAME) - self.cursor.execute(stmt, (table_name, persisted_metadata)) + self.cursor.execute(stmt, (table_name, version, persisted_metadata)) def _compute_db_specific_type(self, attr_t, attr): """ diff --git a/src/translators/sql_translator.py b/src/translators/sql_translator.py index b5a9bb95..133f7989 100644 --- a/src/translators/sql_translator.py +++ b/src/translators/sql_translator.py @@ -7,6 +7,7 @@ from utils.common import iter_entity_attrs from utils.jsondict import safe_get_value from utils.maybe import maybe_map +from _version import __version__ import logging from geocoding.slf import SlfQuery import dateutil.parser @@ -718,7 +719,7 @@ def _update_metadata_table(self, table_name, metadata): # by previous insert update = dict((k, metadata[k]) for k in diff if k in metadata) persisted_metadata.update(update) - self._store_metadata(table_name, persisted_metadata) + self._store_metadata(table_name, __version__, persisted_metadata) self._cache(self.dbCacheName, table_name, [[persisted_metadata]], @@ -728,7 +729,23 @@ def _update_metadata_table(self, table_name, metadata): # This implementation paves # the way to lost updates... - def _store_metadata(self, table_name, persisted_metadata): + def query_version_meta_table(self): + # Bring version from metadata table! + stmt = 'select version from "{}" limit 100'.format( + METADATA_TABLE_NAME) + # By design, one entry per table_name + try: + res = self.cursor.execute(stmt) + row = self.cursor.fetchall() + persisted_metadata = row[0][0] if row else {} + except Exception as e: + self.sql_error_handler(e) + # Metadata table still not created + logging.debug(str(e), exc_info=True) + persisted_metadata = {} + return persisted_metadata + + def _store_metadata(self, table_name, version, persisted_metadata): raise NotImplementedError def _get_et_table_names(self, fiware_service=None): diff --git a/src/translators/tests/test_insert.py b/src/translators/tests/test_insert.py index d312d658..3adc0080 100644 --- a/src/translators/tests/test_insert.py +++ b/src/translators/tests/test_insert.py @@ -6,6 +6,7 @@ from translators.base_translator import BaseTranslator from translators.sql_translator import NGSI_TEXT, NGSI_DATETIME, NGSI_STRUCTURED_VALUE from utils.common import * +from src._version import __version__ from utils.tests.common import * from datetime import datetime, timezone @@ -774,3 +775,18 @@ def test_ngsi_ld(translator, ngsi_ld): assert ngsi_ld['location']['value'] == loaded[0]['location']['values'][0] translator.clean() + + +@pytest.mark.parametrize("translator", translators, ids=["crate", "timescale"]) +def test_entity_meta_version(translator): + + entities = create_random_entities(1, 2, 3, use_time=True, use_geo=True) + result = translator.insert(entities) + assert result.rowcount > 0 + table_name = "et0" + data = translator.query_version_meta_table() + observed = data + expected = __version__ + + assert observed == expected + translator.clean() diff --git a/src/translators/tests/test_timescale_insert.py b/src/translators/tests/test_timescale_insert.py index 31aad8c9..74f456b0 100644 --- a/src/translators/tests/test_timescale_insert.py +++ b/src/translators/tests/test_timescale_insert.py @@ -2,6 +2,7 @@ import pg8000 import pytest import random +from _version import __version__ from time import sleep from geocoding.geojson.wktcodec import decode_wkb_hexstr @@ -105,6 +106,7 @@ def expected_entity_attrs_meta(): 'entity_id': ['id', 'Text'], 'entity_type': ['type', 'Text'], TIME_INDEX_NAME: ['time_index', 'DateTime'], + 'ql_version': [__version__, 'Text'], 'a_number': ['a_number', 'Number'], 'an_integer': ['an_integer', 'Integer'], 'a_bool': ['a_bool', 'Boolean'], diff --git a/src/translators/timescale.py b/src/translators/timescale.py index 54d4397b..fd099d79 100644 --- a/src/translators/timescale.py +++ b/src/translators/timescale.py @@ -269,20 +269,20 @@ def _should_insert_original_entities(self, def _create_metadata_table(self): def do_create(): stmt = "create table if not exists {} " \ - "(table_name text primary key, entity_attrs jsonb)" + "(table_name text primary key, version text, entity_attrs jsonb)" op = stmt.format(METADATA_TABLE_NAME) self.cursor.execute(op) self.with_connection_guard(do_create) - def _store_metadata(self, table_name, persisted_metadata): + def _store_metadata(self, table_name, version, persisted_metadata): def do_store(): - stmt = "insert into {} (table_name, entity_attrs) values (?, ?)" \ + stmt = "insert into {} (table_name, version, entity_attrs) values (?, ?, ?)" \ " on conflict (table_name)" \ " do update set entity_attrs = ?" stmt = stmt.format(METADATA_TABLE_NAME) entity_attrs_value = _encode_to_json_string(persisted_metadata) - self.cursor.execute(stmt, (table_name, entity_attrs_value, + self.cursor.execute(stmt, (table_name, version, entity_attrs_value, entity_attrs_value)) self.with_connection_guard(do_store)