Skip to content

Commit

Permalink
Enable legacy mode (no sql search) if read url is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz authored and amercader committed Oct 12, 2012
1 parent 1cbebd2 commit 8244430
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
11 changes: 4 additions & 7 deletions ckanext/datastore/logic/action.py
Expand Up @@ -76,7 +76,7 @@ def datastore_upsert(context, data_dict):
'''
res_id = _get_or_bust(data_dict, 'resource_id')

data_dict['connection_url'] = pylons.config['ckan.datastore.read_url']
data_dict['connection_url'] = pylons.config['ckan.datastore.write_url']

resources_sql = sqlalchemy.text(u'''SELECT 1 FROM "_table_metadata"
WHERE name = :id AND alias_of IS NULL''')
Expand All @@ -90,8 +90,6 @@ def datastore_upsert(context, data_dict):

p.toolkit.check_access('datastore_upsert', context, data_dict)

data_dict['connection_url'] = pylons.config['ckan.datastore.write_url']

result = db.upsert(context, data_dict)
result.pop('id')
result.pop('connection_url')
Expand All @@ -112,7 +110,7 @@ def datastore_delete(context, data_dict):
'''
res_id = _get_or_bust(data_dict, 'resource_id')

data_dict['connection_url'] = pylons.config['ckan.datastore.read_url']
data_dict['connection_url'] = pylons.config['ckan.datastore.write_url']

resources_sql = sqlalchemy.text(u'''SELECT 1 FROM "_table_metadata"
WHERE name = :id AND alias_of IS NULL''')
Expand All @@ -126,8 +124,6 @@ def datastore_delete(context, data_dict):

p.toolkit.check_access('datastore_delete', context, data_dict)

data_dict['connection_url'] = pylons.config['ckan.datastore.write_url']

result = db.delete(context, data_dict)
result.pop('id')
result.pop('connection_url')
Expand Down Expand Up @@ -172,7 +168,8 @@ def datastore_search(context, data_dict):
'''
res_id = _get_or_bust(data_dict, 'resource_id')

data_dict['connection_url'] = pylons.config['ckan.datastore.read_url']
data_dict['connection_url'] = pylons.config.get('ckan.datastore.read_url',
pylons.config['ckan.datastore.write_url'])

resources_sql = sqlalchemy.text(u'SELECT 1 FROM "_table_metadata" WHERE name = :id')
results = db._get_engine(None, data_dict).execute(resources_sql, id=res_id)
Expand Down
20 changes: 11 additions & 9 deletions ckanext/datastore/plugin.py
Expand Up @@ -30,10 +30,6 @@ def configure(self, config):
error_msg = 'ckan.datastore.write_url not found in config'
raise DatastoreException(error_msg)

if (not 'ckan.datastore.read_url' in config):
error_msg = 'ckan.datastore.read_url not found in config'
raise DatastoreException(error_msg)

# Legacy mode means that we have no read url. Consequently sql search is not
# available and permissions do not have to be changed. In legacy mode, the
# datastore runs on PG prior to 9.0 (for example 8.4).
Expand All @@ -43,20 +39,26 @@ def configure(self, config):
# that we should ignore the following tests.
import sys
if sys.argv[0].split('/')[-1] == 'paster' and "datastore" in [sys.argv[1], sys.argv[2]]:
log.warn("Omitting permission checks because you are "
"running paster commands.")
log.warn('Omitting permission checks because you are '
'running paster commands.')
return

self.ckan_url = self.config['sqlalchemy.url']
self.write_url = self.config['ckan.datastore.write_url']
self.read_url = self.config['ckan.datastore.read_url']
if self.legacy_mode:
self.read_url = self.write_url
else:
self.read_url = self.config['ckan.datastore.read_url']

if not self._is_read_only_database():
# Make sure that the right permissions are set
# so that no harmful queries can be made
if not ('debug' in config and config['debug']):
self._check_separate_db()
self._check_read_permissions()
if self.legacy_mode:
log.warn('Legacy mode active. Thse sql search will not be available.')
else:
self._check_read_permissions()

self._create_alias_table()
else:
Expand All @@ -74,7 +76,7 @@ def configure(self, config):
def new_resource_show(context, data_dict):
engine = db._get_engine(
context,
{'connection_url': config['ckan.datastore.read_url']}
{'connection_url': self.read_url}
)
new_data_dict = resource_show(context, data_dict)
try:
Expand Down
2 changes: 1 addition & 1 deletion ckanext/datastore/tests/test_datastore.py
Expand Up @@ -33,7 +33,7 @@ def rebuild_all_dbs(Session):
''' If the tests are running on the same db, we have to make sure that
the ckan tables are recrated.
'''
db_read_url_parts = cli.parse_db_config('ckan.datastore.read_url')
db_read_url_parts = cli.parse_db_config('ckan.datastore.write_url')
db_ckan_url_parts = cli.parse_db_config('sqlalchemy.url')
same_db = db_read_url_parts['db_name'] == db_ckan_url_parts['db_name']

Expand Down

0 comments on commit 8244430

Please sign in to comment.