Skip to content

Commit

Permalink
[#3816] datastore_mv_create quick hack
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Sep 15, 2017
1 parent a65b0b0 commit ab07e63
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ckan/lib/dictization/model_save.py
Expand Up @@ -47,6 +47,9 @@ def resource_dict_save(res_dict, context):
obj.url_changed = True
setattr(obj, key, value)
else:
if key == 'query' and (new or obj.extras.get('query') != value):
obj.url_changed = True # XXX hack to notify datastore
obj.url_type = 'datastore'
# resources save extras directly onto the object, instead
# of in a separate extras field like packages and groups
new_extras[key] = value
Expand Down
14 changes: 14 additions & 0 deletions ckanext/datastore/backend/postgres.py
Expand Up @@ -1770,6 +1770,20 @@ def delete(self, context, data_dict):
finally:
context['connection'].close()

def mv_create(self, resource_id, query):
# XXX
# XXX not even pretending to be safe
# XXX
sql = u'''
DROP MATERIALIZED VIEW IF EXISTS {resource_id};
'''.format(resource_id=identifier(resource_id))
_write_engine_execute(sql)
sql = u'''
CREATE MATERIALIZED VIEW {resource_id} AS {query};
'''.format(resource_id=identifier(resource_id), query=query)
_write_engine_execute(sql)


def create(self, context, data_dict):
'''
The first row will be used to guess types not in the fields and the
Expand Down
7 changes: 7 additions & 0 deletions ckanext/datastore/logic/action.py
Expand Up @@ -171,6 +171,13 @@ def datastore_create(context, data_dict):
return result


def datastore_mv_create(context, data_dict):
backend = DatastoreBackend.get_active_backend()
resource_id = data_dict['resource_id']
p.toolkit.check_access('datastore_create', context, data_dict)
backend.mv_create(resource_id, data_dict['query'])


def datastore_run_triggers(context, data_dict):
''' update each record with trigger
Expand Down
7 changes: 7 additions & 0 deletions ckanext/datastore/plugin.py
Expand Up @@ -92,6 +92,12 @@ def configure(self, config):
# IResourceUrlChange

def notify(self, entity, operation=None):
# XXX: hack
if isinstance(entity, model.Resource) and 'query' in entity.extras:
p.toolkit.get_action('datastore_mv_create')(
{'model': model, 'ignore_auth': True},
{'resource_id': entity.id, 'query': entity.extras['query']})
return
if not isinstance(entity, model.Package) or self.legacy_mode:
return
# if a resource is new, it cannot have a datastore resource, yet
Expand Down Expand Up @@ -121,6 +127,7 @@ def get_actions(self):
'datastore_function_create': action.datastore_function_create,
'datastore_function_delete': action.datastore_function_delete,
'datastore_run_triggers': action.datastore_run_triggers,
'datastore_mv_create': action.datastore_mv_create,
}
if not self.legacy_mode:
if getattr(self.backend, 'enable_sql_search', False):
Expand Down

0 comments on commit ab07e63

Please sign in to comment.