Skip to content

Commit

Permalink
[#1879] Set DataStore to legacy mode when using PostgreSQL < 9.x
Browse files Browse the repository at this point in the history
Up until now the user had to actively set legacy mode when running PG
8.x, and if not you could end up with the full datastore running on an
old postgres version. Features like the SQL search are not properly
supported on these versions, so we need to explicitly disable them when
running on PG 8.x, by setting `legacy_mode` to True.

The current behaviour is that legacy mode will be activated if
`ckan.datastore.read_url` is not present in the configuration and/or
postgres < 9.x.
  • Loading branch information
amercader committed Aug 12, 2014
1 parent d636428 commit a259742
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
18 changes: 17 additions & 1 deletion ckanext/datastore/plugin.py
Expand Up @@ -23,6 +23,22 @@
ValidationError = p.toolkit.ValidationError


def _is_legacy_mode(config):
'''
Decides if the DataStore should run on legacy mode
Returns True if `ckan.datastore.read_url` is not set in the provided
config object or CKAN is running on Postgres < 9.x
'''
write_url = config.get('ckan.datastore.write_url')

engine = db._get_engine({'connection_url': write_url})
connection = engine.connect()

return (not config.get('ckan.datastore.read_url') or
not db._pg_version_is_at_least(connection, '9.0'))


class DatastoreException(Exception):
pass

Expand Down Expand Up @@ -62,7 +78,7 @@ def configure(self, config):
# 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).
self.legacy_mode = 'ckan.datastore.read_url' not in self.config
self.legacy_mode = _is_legacy_mode(self.config)

datapusher_formats = config.get('datapusher.formats', '').split()
self.datapusher_formats = datapusher_formats or DEFAULT_FORMATS
Expand Down
56 changes: 56 additions & 0 deletions ckanext/datastore/tests/test_unit.py
@@ -1,9 +1,11 @@
import unittest
import pylons
import nose
import mock

import ckan.tests as tests
import ckanext.datastore.db as db
import ckanext.datastore.plugin as plugin


class TestTypeGetters(unittest.TestCase):
Expand Down Expand Up @@ -35,3 +37,57 @@ def test_pg_version_check(self):
connection = engine.connect()
assert db._pg_version_is_at_least(connection, '8.0')
assert not db._pg_version_is_at_least(connection, '10.0')

@mock.patch('ckanext.datastore.db._pg_version_is_at_least')
def test_legacy_mode_set_if_no_read_url_and_pg_9(self, pgv):

pgv.return_value = True

from pylons import config

test_config = {
'ckan.datastore.write_url': config['ckan.datastore.write_url'],
}

assert plugin._is_legacy_mode(test_config)

@mock.patch('ckanext.datastore.db._pg_version_is_at_least')
def test_legacy_mode_set_if_no_read_url_and_pg_8(self, pgv):

pgv.return_value = False

from pylons import config

test_config = {
'ckan.datastore.write_url': config['ckan.datastore.write_url'],
}

assert plugin._is_legacy_mode(test_config)

@mock.patch('ckanext.datastore.db._pg_version_is_at_least')
def test_legacy_mode_set_if_read_url_and_pg_8(self, pgv):

pgv.return_value = False

from pylons import config

test_config = {
'ckan.datastore.write_url': config['ckan.datastore.write_url'],
'ckan.datastore.read_url': config['ckan.datastore.read_url'],
}

assert plugin._is_legacy_mode(test_config)

@mock.patch('ckanext.datastore.db._pg_version_is_at_least')
def test_legacy_mode_not_set_if_read_url_and_pg_9(self, pgv):

pgv.return_value = True

from pylons import config

test_config = {
'ckan.datastore.write_url': config['ckan.datastore.write_url'],
'ckan.datastore.read_url': config['ckan.datastore.read_url'],
}

assert not plugin._is_legacy_mode(test_config)

0 comments on commit a259742

Please sign in to comment.