summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenjamin Dauvergne <bdauvergne@entrouvert.com>2017-03-14 14:07:56 (GMT)
committerBenjamin Dauvergne <bdauvergne@entrouvert.com>2017-03-14 14:07:56 (GMT)
commitb5d9c12fe92aa356c13e5becb452db423e7fcdbe (patch)
treec95854d4ee74ceaed049444edc744924f07d86bb
parent437f4924d2f88a4be6182280f34730c8b418c03e (diff)
downloadwcs-olap-b5d9c12fe92aa356c13e5becb452db423e7fcdbe.zip
wcs-olap-b5d9c12fe92aa356c13e5becb452db423e7fcdbe.tar.gz
wcs-olap-b5d9c12fe92aa356c13e5becb452db423e7fcdbe.tar.bz2
activate json_field only if postgres version supports JSONB (fixes #15427)
-rw-r--r--wcs_olap/feeder.py21
1 files changed, 15 insertions, 6 deletions
diff --git a/wcs_olap/feeder.py b/wcs_olap/feeder.py
index 946708f..c39bf40 100644
--- a/wcs_olap/feeder.py
+++ b/wcs_olap/feeder.py
@@ -85,7 +85,6 @@ class WcsOlapFeeder(object):
'name': 'all_formdata',
'label': u'Tous les formulaires',
'fact_table': 'formdata',
- 'json_field': 'json_data',
'key': 'id',
'joins': [
{
@@ -240,6 +239,13 @@ class WcsOlapFeeder(object):
self.base_cube = self.model['cubes'][0]
self.agents_mapping = {}
self.formdata_json_index = []
+ self.has_jsonb = self.detect_jsonb()
+ if self.has_jsonb:
+ cube['json_field'] = 'json_data'
+
+ def detect_jsonb(self):
+ self.cur.execute("SELECT 1 FROM pg_type WHERE typname = 'jsonb'")
+ return bool(self.cur.rowcount)
def hash_table_name(self, table_name):
table_name = table_name.format(**self.default_ctx)
@@ -390,8 +396,9 @@ CREATE TABLE public.dates AS (SELECT
['endpoint_delay', 'interval'],
['first_agent_id', 'smallint REFERENCES {agent_table} (id)'],
['geolocation_base', 'POINT NULL'],
- ['json_data', 'JSONB NULL'],
]
+ if self.has_jsonb:
+ self.columns.append(['json_data', 'JSONB NULL'])
self.comments = {
'formdef_id': u'formulaire',
'receipt_time': u'date de réception',
@@ -577,9 +584,10 @@ class WcsFormdefFeeder(object):
self.ex('COMMENT ON COLUMN {formdata_table}.%s IS %%s' % at, vars=(comment,))
# Creat index for JSON fields
- for field in fields:
- if field.varname:
- self.create_formdata_json_index(field.varname)
+ if self.has_jsonb:
+ for field in fields:
+ if field.varname:
+ self.create_formdata_json_index(field.varname)
# PostgreSQL does not propagate foreign key constraints to child tables
# so we must recreate them manually
@@ -683,7 +691,8 @@ class WcsFormdefFeeder(object):
json_data[field.varname] = raw
row['field_%s' % field.varname] = v
- row['json_data'] = json.dumps(json_data)
+ if self.has_jsonb:
+ row['json_data'] = json.dumps(json_data)
# add geolocation fields value
for geolocation, label in self.formdef.schema.geolocations:
v = (data.geolocations or {}).get(geolocation)