Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ql version in metadata #615

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions src/_version.py
Original file line number Diff line number Diff line change
@@ -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'
3 changes: 2 additions & 1 deletion src/reporter/tests/test_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from conftest import QL_BASE_URL
from _version import __dev_version__
import requests


Expand All @@ -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__
}
3 changes: 2 additions & 1 deletion src/reporter/version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from _version import __dev_version__


def version():
return {
'version': '0.9.0-dev'
'version': __dev_version__
}
8 changes: 4 additions & 4 deletions src/translators/crate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
21 changes: 19 additions & 2 deletions src/translators/sql_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]],
Expand All @@ -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):
Expand Down
16 changes: 16 additions & 0 deletions src/translators/tests/test_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
2 changes: 2 additions & 0 deletions src/translators/tests/test_timescale_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'],
Expand Down
8 changes: 4 additions & 4 deletions src/translators/timescale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down