Skip to content

Commit

Permalink
apply triggers to an existing datastore resource
Browse files Browse the repository at this point in the history
  • Loading branch information
fanjinfei committed May 16, 2017
1 parent 784a6e1 commit 222d207
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
31 changes: 31 additions & 0 deletions ckanext/datastore/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,37 @@ def datastore_create(context, data_dict):
return result


def datastore_trigger_each_row(context, data_dict):
''' update each record with trigger
The datastore_trigger_each_row API action allows you to apply triggers to
an existing DataStore resource.
:param resource_id: resource id that the data is going to be stored under.
:type resource_id: string
**Results:**
:returns: The rowcount in the table.
:rtype: int
'''
res_id = data_dict['resource_id']
p.toolkit.check_access('datastore_trigger_each_row', context, data_dict)

connection = db.get_write_engine().connect()

sql = sqlalchemy.text(u'''update {0} set _id=_id '''.format(
datastore_helpers.identifier(res_id)))
try:
results = connection.execute(sql)
except sqlalchemy.exc.DatabaseError as err:
message = err.args[0].split('\n')[0].decode('utf8')
raise p.toolkit.ValidationError({
u'records': [message.split(u') ', 1)[-1]]})
return results.rowcount


def datastore_upsert(context, data_dict):
'''Updates or inserts into a table in the DataStore
Expand Down
5 changes: 5 additions & 0 deletions ckanext/datastore/logic/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ def datastore_function_create(context, data_dict):

def datastore_function_delete(context, data_dict):
return {'success': False}


def datastore_trigger_each_row(context, data_dict):
'''sysadmin-only: functions can be used to skip access checks'''
return {'success': False}
2 changes: 2 additions & 0 deletions ckanext/datastore/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ def get_actions(self):
'datastore_info': action.datastore_info,
'datastore_function_create': action.datastore_function_create,
'datastore_function_delete': action.datastore_function_delete,
'datastore_trigger_each_row': action.datastore_trigger_each_row,
}
if not self.legacy_mode:
if self.enable_sql_search:
Expand All @@ -247,6 +248,7 @@ def get_auth_functions(self):
'datastore_change_permissions': auth.datastore_change_permissions,
'datastore_function_create': auth.datastore_function_create,
'datastore_function_delete': auth.datastore_function_delete,
'datastore_trigger_each_row': auth.datastore_trigger_each_row,
}

def before_map(self, m):
Expand Down

0 comments on commit 222d207

Please sign in to comment.