Skip to content

Commit

Permalink
Strip white space from name when adding a column or index.
Browse files Browse the repository at this point in the history
I have seen this in a live site and it is tricky to figure out what is wrong.
Solution if it is too late: delete the column or index and recreate without the space.
For the column, rebuild the catalog.  For the index, index that particular index.
  • Loading branch information
mauritsvanrees committed Sep 27, 2012
1 parent b219674 commit 22a6ea1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changelog
3.0b2 (unreleased)
------------------

- Strip white space from name when adding a column or index.

- Forward compatibility for Zope 4 removal of RequestContainer.

- Optimize brain instantiation, by creating underlying record items in a
Expand Down
14 changes: 14 additions & 0 deletions src/Products/ZCatalog/Catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ def addColumn(self, name, default_value=None, threshold=10000):
schema = self.schema
names = list(self.names)

if name != name.strip():
# Someone could have mistakenly added a space at the end
# of the input field.
LOG.warn("stripped space from new column %r -> %r", name,
name.strip())
name = name.strip()

if name in schema:
raise CatalogError('The column %s already exists' % name)

Expand Down Expand Up @@ -247,6 +254,13 @@ def addIndex(self, name, index_type):
if not name:
raise CatalogError('Name of index is empty')

if name != name.strip():
# Someone could have mistakenly added a space at the end
# of the input field.
LOG.warn("stripped space from new index %r -> %r", name,
name.strip())
name = name.strip()

indexes = self.indexes

if isinstance(index_type, str):
Expand Down
20 changes: 20 additions & 0 deletions src/Products/ZCatalog/tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ def test_add_bad(self):
catalog = self._make_one()
self.assertRaises(CatalogError, catalog.addColumn, '_id')

def test_add_with_space(self):
catalog = self._make_one()
catalog.addColumn(' space ')
self.assertEqual(' space ' not in catalog.schema, True,
'space not stripped in add column')
self.assertEqual('space' in catalog.schema, True,
'stripping space in add column failed')

def test_add_brains(self):
catalog = self._make_one()
catalog.addColumn('col1')
Expand Down Expand Up @@ -135,6 +143,18 @@ def test_add_keyword_index(self):
i = catalog.indexes['id']
self.assert_(isinstance(i, KeywordIndex))

def test_add_with_space(self):
catalog = self._make_one()
idx = KeywordIndex(' space ')
catalog.addIndex(' space ', idx)
self.assertEqual(' space ' not in catalog.indexes, True,
'space not stripped in add index')
self.assertEqual('space' in catalog.indexes, True,
'stripping space in add index failed')
i = catalog.indexes['space']
# Note: i.id still has spaces in it.
self.assert_(isinstance(i, KeywordIndex))

def test_del_field_index(self):
catalog = self._make_one()
idx = FieldIndex('id')
Expand Down

0 comments on commit 22a6ea1

Please sign in to comment.