From 82c7988bf3718b00eb8e4ca57d2b0db5786259fb Mon Sep 17 00:00:00 2001 From: clementmouchet Date: Fri, 12 Dec 2014 13:10:40 +0000 Subject: [PATCH 1/2] Removed ResourceGroup from query when using CKAN 2.3 or above --- ckanext/harvest/logic/action/update.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/ckanext/harvest/logic/action/update.py b/ckanext/harvest/logic/action/update.py index e262def..75775ba 100644 --- a/ckanext/harvest/logic/action/update.py +++ b/ckanext/harvest/logic/action/update.py @@ -17,6 +17,7 @@ from ckan.lib.search.common import SearchIndexError, make_connection from ckan.model import Package from ckan import logic +from ckan.plugins.toolkit import check_ckan_version from ckan.logic import NotFound, check_access @@ -121,20 +122,34 @@ def harvest_source_clear(context,data_dict): delete from harvest_job where source_id = '{harvest_source_id}'; delete from package_role where package_id in (select id from package where state = 'to_delete' ); delete from user_object_role where id not in (select user_object_role_id from package_role) and context = 'Package'; - delete from resource_revision where resource_group_id in (select id from resource_group where package_id in (select id from package where state = 'to_delete')); - delete from resource_group_revision where package_id in (select id from package where state = 'to_delete'); delete from package_tag_revision where package_id in (select id from package where state = 'to_delete'); delete from member_revision where table_id in (select id from package where state = 'to_delete'); delete from package_extra_revision where package_id in (select id from package where state = 'to_delete'); delete from package_revision where id in (select id from package where state = 'to_delete'); delete from package_tag where package_id in (select id from package where state = 'to_delete'); - delete from resource where resource_group_id in (select id from resource_group where package_id in (select id from package where state = 'to_delete')); delete from package_extra where package_id in (select id from package where state = 'to_delete'); delete from member where table_id in (select id from package where state = 'to_delete'); - delete from resource_group where package_id in (select id from package where state = 'to_delete'); delete from related_dataset where dataset_id in (select id from package where state = 'to_delete'); delete from related where id in {related_ids}; - delete from package where id in (select id from package where state = 'to_delete'); commit;'''.format(harvest_source_id=harvest_source_id, related_ids=related_ids) + delete from package where id in (select id from package where state = 'to_delete');'''.format( + harvest_source_id=harvest_source_id, related_ids=related_ids) + + # Backwards-compatibility: support ResourceGroup (pre-CKAN-2.3) + if check_ckan_version(min_version='2.3'): + sql += 'commit;' + else: + sql += '''delete from resource_revision where resource_group_id in + (select id from resource_group where package_id in + (select id from package where state = 'to_delete')); + delete from resource where resource_group_id in + (select id from resource_group where package_id in + (select id from package where state = 'to_delete')); + delete from resource_group_revision where package_id in + (select id from package where state = 'to_delete'); + delete from resource_group where package_id in + (select id from package where state = 'to_delete'); + commit; + ''' model.Session.execute(sql) From ead9e67a3317739d7f3968aad703452f455e7608 Mon Sep 17 00:00:00 2001 From: clementmouchet Date: Mon, 23 Feb 2015 17:02:21 +0000 Subject: [PATCH 2/2] updated def harvest_source_clear() to delete resource views, resource revisions & resources in CKAN >= 2.3 --- ckanext/harvest/logic/action/update.py | 48 ++++++++++++++++---------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/ckanext/harvest/logic/action/update.py b/ckanext/harvest/logic/action/update.py index 75775ba..b4f779b 100644 --- a/ckanext/harvest/logic/action/update.py +++ b/ckanext/harvest/logic/action/update.py @@ -114,7 +114,32 @@ def harvest_source_clear(context,data_dict): ids.append(row[0]) related_ids = "('" + "','".join(ids) + "')" - sql = '''begin; update package set state = 'to_delete' where id in (select package_id from harvest_object where harvest_source_id = '{harvest_source_id}'); + sql = '''begin; + update package set state = 'to_delete' where id in (select package_id from harvest_object where harvest_source_id = '{harvest_source_id}');'''.format( + harvest_source_id=harvest_source_id) + + # CKAN-2.3 or above: delete resource views, resource revisions & resources + if check_ckan_version(min_version='2.3'): + sql += ''' + delete from resource_view where resource_id in (select id from resource where package_id in (select id from package where state = 'to_delete' )); + delete from resource_revision where package_id in (select id from package where state = 'to_delete' ); + delete from resource where package_id in (select id from package where state = 'to_delete' ); + ''' + # Backwards-compatibility: support ResourceGroup (pre-CKAN-2.3) + else: + sql += ''' + delete from resource_revision where resource_group_id in + (select id from resource_group where package_id in + (select id from package where state = 'to_delete')); + delete from resource where resource_group_id in + (select id from resource_group where package_id in + (select id from package where state = 'to_delete')); + delete from resource_group_revision where package_id in + (select id from package where state = 'to_delete'); + delete from resource_group where package_id in + (select id from package where state = 'to_delete'); + ''' + sql += ''' delete from harvest_object_error where harvest_object_id in (select id from harvest_object where harvest_source_id = '{harvest_source_id}'); delete from harvest_object_extra where harvest_object_id in (select id from harvest_object where harvest_source_id = '{harvest_source_id}'); delete from harvest_object where harvest_source_id = '{harvest_source_id}'; @@ -131,26 +156,11 @@ def harvest_source_clear(context,data_dict): delete from member where table_id in (select id from package where state = 'to_delete'); delete from related_dataset where dataset_id in (select id from package where state = 'to_delete'); delete from related where id in {related_ids}; - delete from package where id in (select id from package where state = 'to_delete');'''.format( + delete from package where id in (select id from package where state = 'to_delete'); + commit; + '''.format( harvest_source_id=harvest_source_id, related_ids=related_ids) - # Backwards-compatibility: support ResourceGroup (pre-CKAN-2.3) - if check_ckan_version(min_version='2.3'): - sql += 'commit;' - else: - sql += '''delete from resource_revision where resource_group_id in - (select id from resource_group where package_id in - (select id from package where state = 'to_delete')); - delete from resource where resource_group_id in - (select id from resource_group where package_id in - (select id from package where state = 'to_delete')); - delete from resource_group_revision where package_id in - (select id from package where state = 'to_delete'); - delete from resource_group where package_id in - (select id from package where state = 'to_delete'); - commit; - ''' - model.Session.execute(sql) # Refresh the index for this source to update the status object