Skip to content

Commit

Permalink
simpler index creation
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Sep 13, 2012
1 parent 3265564 commit acefa2d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
10 changes: 6 additions & 4 deletions ckanext/datastore/db.py
Expand Up @@ -314,7 +314,9 @@ def create_indexes(context, data_dict):
if indexes == None and primary_key == None:
return

sql_index_string = u'CREATE {unique} INDEX ON "{res_id}" USING {method}({fields})'
sql_index_skeletton = u'CREATE {unique} INDEX ON "{res_id}"'
sql_index_string_method = sql_index_skeletton + u' USING {method}({fields})'
sql_index_string = sql_index_skeletton + u' ({fields})'
sql_index_strings = []
field_ids = _pluck('id', _get_fields(context, data_dict))

Expand All @@ -332,10 +334,10 @@ def create_indexes(context, data_dict):
fields_string = u','.join(['"%s"' % field for field in fields])
sql_index_strings.append(sql_index_string.format(
res_id=data_dict['resource_id'], unique='',
method='btree', fields=fields_string))
fields=fields_string))

# create index for faster full text search (indexes: gin or gist)
sql_index_strings.append(sql_index_string.format(
sql_index_strings.append(sql_index_string_method.format(
res_id=data_dict['resource_id'], unique='',
method='gist', fields='_full_text'))
if primary_key != None:
Expand All @@ -351,7 +353,7 @@ def create_indexes(context, data_dict):
if primary_key:
sql_index_strings.append(sql_index_string.format(
res_id=data_dict['resource_id'], unique='unique',
method='btree', fields=u','.join(['"%s"' % field for field in primary_key])))
fields=u','.join(['"%s"' % field for field in primary_key])))

map(context['connection'].execute, sql_index_strings)

Expand Down
4 changes: 2 additions & 2 deletions ckanext/datastore/tests/test_datastore.py
Expand Up @@ -217,7 +217,7 @@ def test_create_basic(self):
'aliases': aliases,
'fields': [{'id': 'book', 'type': 'text'},
{'id': 'author', 'type': 'json'}],
'indexes': ['book'],
'indexes': [['book', 'author'], 'book'],
'records': [
{'book': 'crime', 'author': ['tolstoy', 'dostoevsky']},
{'book': 'annakarenina', 'author': ['tolstoy', 'putin']},
Expand Down Expand Up @@ -1448,7 +1448,7 @@ def test_is_single_statement(self):
assert db._is_single_statement(multiple) is False

def test_invalid_statement(self):
query = 'SELECT ** FROM public.foo'
query = 'SELECT ** FROM public.foobar'
data = {'sql': query}
postparams = json.dumps(data)
auth = {'Authorization': str(self.sysadmin_user.apikey)}
Expand Down

0 comments on commit acefa2d

Please sign in to comment.