diff --git a/ckanext/datastore/logic/action.py b/ckanext/datastore/logic/action.py index 07162fa1ac0..6a257ec9d5c 100644 --- a/ckanext/datastore/logic/action.py +++ b/ckanext/datastore/logic/action.py @@ -651,3 +651,24 @@ def datastore_function_delete(context, data_dict): p.toolkit.check_access('datastore_function_delete', context, data_dict) backend = DatastoreBackend.get_active_backend() backend.drop_function(data_dict['name'], data_dict['if_exists']) + + +@logic.validate(dsschema.datastore_analyze_schema) +def datastore_analyze(context, data_dict): + '''Runs postgres's ANALYZE + + :param resource_id: resource id for the table that will be analyzed + :type resource_id: string + ''' + p.toolkit.check_access('datastore_analyze', context, data_dict) + backend = DatastoreBackend.get_active_backend() + connection = backend._get_write_engine().connect() + + result = backend.analyze(context, data_dict) + #move to backend/postgres.py + sql = 'ANALYZE "{}"'.format(data_dict['resource_id']) + try: + results = connection.execute(sql) + except sqlalchemy.exc.DatabaseError as err: + raise p.toolkit.ValidationError({ + u'records': [message.split(u') ', 1)[-1]]}) diff --git a/ckanext/datastore/logic/auth.py b/ckanext/datastore/logic/auth.py index 06aad380f9b..5a20c20a728 100644 --- a/ckanext/datastore/logic/auth.py +++ b/ckanext/datastore/logic/auth.py @@ -83,3 +83,7 @@ def datastore_function_delete(context, data_dict): def datastore_run_triggers(context, data_dict): '''sysadmin-only: functions can be used to skip access checks''' return {'success': False} + + +def datastore_analyze(context, data_dict): + return {'success': False} diff --git a/ckanext/datastore/logic/schema.py b/ckanext/datastore/logic/schema.py index 510c112f8aa..161ef8a9748 100644 --- a/ckanext/datastore/logic/schema.py +++ b/ckanext/datastore/logic/schema.py @@ -196,3 +196,9 @@ def datastore_function_delete_schema(): 'name': [unicode_only, not_empty], 'if_exists': [default(False), boolean_validator], } + + +def datastore_analyze_schema(): + return { + 'resource_id': [text_type, resource_id_exists], + }