Repository: ckan/ckan

Issue:
"Data API" button disappears on resource page after empty update
ckan_version: 2.5.2
### Please describe the expected behaviour

When updating a resource without changing any fields, url or description, the green "Data API" button should still be there, because the data is still in the datastore (And API calls of this type confirms that it still is). Not sure if it is expected of the datapusher to trigger its update or not. 
### Please describe the actual behaviour

The green "Data API" button disappears, as if data is now removed from the datastore for the particular resource. It would seem like CKAN thinks this triggers the datapusher hook, even though it does not (logs confirm it nothing happens).
### What steps can be taken to reproduce the issue?

Push .CSV file to the datastore using the web interface, then update the resource using either the `resource_update` API call or the web interface, without changing anything.

I'm curious to see if i'm the only one with this issue, i can reproduce over several CKAN installations running 2.5.2. If i need to post this in the datapusher github issue tracker instead, close this and i will post it there instead.



Discussion:
@NicolaiMogensen this is indeed a bug.

The "Data API" button relies on the `datastore_active` resource extra being present and True. When editing via the frontend (or the API) this field is not passed and so it's dropped from the db. We need to persist the value when updating if the resource already has it and it's not provided.

PR incoming


Resolving patch:
diff --git a/ckan/logic/action/update.py b/ckan/logic/action/update.py
--- a/ckan/logic/action/update.py
+++ b/ckan/logic/action/update.py
@@ -80,6 +80,11 @@ def resource_update(context, data_dict):
         log.error('Could not find resource %s after all', id)
         raise NotFound(_('Resource was not found.'))
 
+    # Persist the datastore_active extra if already present and not provided
+    if ('datastore_active' in resource.extras and
+            'datastore_active' not in data_dict):
+        data_dict['datastore_active'] = resource.extras['datastore_active']
+
     for plugin in plugins.PluginImplementations(plugins.IResourceController):
         plugin.before_update(context, pkg_dict['resources'][n], data_dict)
 


Test patch:
diff --git a/ckan/tests/logic/action/test_update.py b/ckan/tests/logic/action/test_update.py
--- a/ckan/tests/logic/action/test_update.py
+++ b/ckan/tests/logic/action/test_update.py
@@ -723,6 +723,77 @@ def test_extra_gets_deleted_on_extra_only_update(self):
         assert_equals(res_returned['anotherfield'], 'second')
         assert 'newfield' not in res_returned
 
+    def test_datastore_active_is_persisted_if_true_and_not_provided(self):
+        dataset = factories.Dataset()
+        resource = factories.Resource(package=dataset,
+                                      url='http://example.com',
+                                      datastore_active=True)
+
+        res_returned = helpers.call_action('resource_update',
+                                           id=resource['id'],
+                                           url='http://example.com',
+                                           name='Test')
+
+        assert_equals(res_returned['datastore_active'], True)
+
+    def test_datastore_active_is_persisted_if_false_and_not_provided(self):
+        dataset = factories.Dataset()
+        resource = factories.Resource(package=dataset,
+                                      url='http://example.com',
+                                      datastore_active=False)
+
+        res_returned = helpers.call_action('resource_update',
+                                           id=resource['id'],
+                                           url='http://example.com',
+                                           name='Test')
+
+        assert_equals(res_returned['datastore_active'], False)
+
+    def test_datastore_active_is_updated_if_false_and_provided(self):
+        dataset = factories.Dataset()
+        resource = factories.Resource(package=dataset,
+                                      url='http://example.com',
+                                      datastore_active=False)
+
+        res_returned = helpers.call_action('resource_update',
+                                           id=resource['id'],
+                                           url='http://example.com',
+                                           name='Test',
+                                           datastore_active=True)
+
+        assert_equals(res_returned['datastore_active'], True)
+
+    def test_datastore_active_is_updated_if_true_and_provided(self):
+        dataset = factories.Dataset()
+        resource = factories.Resource(package=dataset,
+                                      url='http://example.com',
+                                      datastore_active=True)
+
+        res_returned = helpers.call_action('resource_update',
+                                           id=resource['id'],
+                                           url='http://example.com',
+                                           name='Test',
+                                           datastore_active=False)
+
+        assert_equals(res_returned['datastore_active'], False)
+
+    def test_datastore_active_not_present_if_not_provided_and_not_datastore_plugin_enabled(self):
+
+        assert not p.plugin_loaded('datastore')
+
+        dataset = factories.Dataset()
+        resource = factories.Resource(package=dataset,
+                                      url='http://example.com',
+                                      )
+
+        res_returned = helpers.call_action('resource_update',
+                                           id=resource['id'],
+                                           url='http://example.com',
+                                           name='Test',
+                                           )
+
+        assert 'datastore_active' not in res_returned
+
 
 class TestConfigOptionUpdate(object):