Skip to content

Commit

Permalink
Merge pull request #2234 from ckan/2234-persist-datastore_active
Browse files Browse the repository at this point in the history
datastore sets datastore_active on resource at runtime.
  • Loading branch information
wardi committed Nov 11, 2015
2 parents 096106f + 832702f commit f745750
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 21 deletions.
2 changes: 1 addition & 1 deletion ckan/logic/schema.py
Expand Up @@ -98,7 +98,7 @@ def default_resource_schema():
'cache_last_updated': [ignore_missing, isodate],
'webstore_last_updated': [ignore_missing, isodate],
'tracking_summary': [ignore_missing],
'datastore_active': [ignore],
'datastore_active': [ignore_missing],
'__extras': [ignore_missing, extras_unicode_convert, keep_extras],
}

Expand Down
54 changes: 54 additions & 0 deletions ckan/migration/versions/081_set_datastore_active.py
@@ -0,0 +1,54 @@
import json
from sqlalchemy import create_engine
from sqlalchemy.sql import text
from pylons import config


def upgrade(migrate_engine):

datastore_connection_url = config.get(
'ckan.datastore.read_url', config.get('ckan.datastore.write_url'))

if not datastore_connection_url:
return

datastore_engine = create_engine(datastore_connection_url)

try:

resources_in_datastore = datastore_engine.execute('''
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name != '_table_metadata'
''')

if resources_in_datastore.rowcount:

resources = migrate_engine.execute('''
SELECT id, extras
FROM resource
WHERE id IN ({0}) AND extras IS NOT NULL
'''.format(
','.join(['\'{0}\''.format(_id[0])
for _id
in resources_in_datastore])
)
)
if resources.rowcount:
params = []
for resource in resources:
new_extras = json.loads(resource[1])
new_extras.update({'datastore_active': True})
params.append(
{'id': resource[0],
'extras': json.dumps(new_extras)})

migrate_engine.execute(
text('''
UPDATE resource
SET extras = :extras
WHERE id = :id'''),
params)
finally:
datastore_engine.dispose()
23 changes: 17 additions & 6 deletions ckanext/datastore/logic/action.py
Expand Up @@ -69,6 +69,7 @@ def datastore_create(context, data_dict):
records = data_dict.pop('records', None)
resource = data_dict.pop('resource', None)
data_dict, errors = _validate(data_dict, schema, context)
resource_dict = None
if records:
data_dict['records'] = records
if resource:
Expand All @@ -92,17 +93,17 @@ def datastore_create(context, data_dict):
has_url = 'url' in data_dict['resource']
# A datastore only resource does not have a url in the db
data_dict['resource'].setdefault('url', '_datastore_only_resource')
res = p.toolkit.get_action('resource_create')(context,
data_dict['resource'])
data_dict['resource_id'] = res['id']
resource_dict = p.toolkit.get_action('resource_create')(
context, data_dict['resource'])
data_dict['resource_id'] = resource_dict['id']

# create resource from file
if has_url:
if not p.plugin_loaded('datapusher'):
raise p.toolkit.ValidationError({'resource': [
'The datapusher has to be enabled.']})
p.toolkit.get_action('datapusher_submit')(context, {
'resource_id': res['id'],
'resource_id': resource_dict['id'],
'set_url_type': True
})
# since we'll overwrite the datastore resource anyway, we
Expand All @@ -112,8 +113,8 @@ def datastore_create(context, data_dict):
# create empty resource
else:
# no need to set the full url because it will be set in before_show
res['url_type'] = 'datastore'
p.toolkit.get_action('resource_update')(context, res)
resource_dict['url_type'] = 'datastore'
p.toolkit.get_action('resource_update')(context, resource_dict)
else:
if not data_dict.pop('force', False):
resource_id = data_dict['resource_id']
Expand Down Expand Up @@ -141,6 +142,10 @@ def datastore_create(context, data_dict):
except db.InvalidDataError as err:
raise p.toolkit.ValidationError(str(err))

# Set the datastore_active flag on the resource if necessary
p.toolkit.get_action('resource_patch')(
context, {'id': data_dict['resource_id'], 'datastore_active': True})

result.pop('id', None)
result.pop('private', None)
result.pop('connection_url')
Expand Down Expand Up @@ -328,6 +333,12 @@ def datastore_delete(context, data_dict):
))

result = db.delete(context, data_dict)

# Set the datastore_active flag on the resource if necessary
if not data_dict.get('filters'):
p.toolkit.get_action('resource_patch')(
context, {'id': data_dict['resource_id'], 'datastore_active': False})

result.pop('id', None)
result.pop('connection_url')
return result
Expand Down
16 changes: 2 additions & 14 deletions ckanext/datastore/plugin.py
Expand Up @@ -282,21 +282,9 @@ def before_show(self, resource_dict):
controller='ckanext.datastore.controller:DatastoreController',
action='dump', resource_id=resource_dict['id'])

connection = None
if 'datastore_active' not in resource_dict:
resource_dict[u'datastore_active'] = False

resource_dict['datastore_active'] = False

try:
connection = self.read_engine.connect()
result = connection.execute(
'SELECT 1 FROM "_table_metadata" WHERE name = %s AND alias_of IS NULL',
resource_dict['id']
).fetchone()
if result:
resource_dict['datastore_active'] = True
finally:
if connection:
connection.close()
return resource_dict

def datastore_validate(self, context, data_dict, fields_types):
Expand Down

0 comments on commit f745750

Please sign in to comment.