Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PXD-2916: add unique indexes for uniqueKeys #12

Merged
merged 4 commits into from
Apr 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions gdcdatamodel/models/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,42 @@ def get_secondary_key_indexes(cls):

#: use text_pattern_ops, allows LIKE statements not starting with %
index_op = 'text_pattern_ops'
secondary_keys = {key for pair in cls.__pg_secondary_keys for key in pair}

key_indexes = (
Index(
index_name(cls, key),
cls._props[key].astext.label(key),
postgresql_ops={key: index_op},
) for key in secondary_keys
unique=len(keys) == 1,
)
for keys in cls.__pg_secondary_keys
for key in keys
)

lower_key_indexes = (
Index(
index_name(cls, key+'_lower'),
func.lower(cls._props[key].astext).label(key+'_lower'),
postgresql_ops={key+'_lower': index_op},
) for key in secondary_keys
)
for keys in cls.__pg_secondary_keys
for key in keys
)

# __pg_secondary_keys are "uniqueKeys" in the dictionary yaml file, they are
# semantically supposed to be unique locally
unique_indexes = (
Index(
index_name(cls, "_".join(keys) + "_uniq"),
*(func.lower(cls._props[key].astext).label(key) for key in keys),
postgresql_ops=dict((key, index_op) for key in keys),
unique=True # https://bugs.python.org/issue9232
)
for keys in cls.__pg_secondary_keys
if len(keys) > 1 # skip duplicate indexes
)

return tuple(key_indexes) + tuple(lower_key_indexes)
return tuple(key_indexes) + tuple(lower_key_indexes) + tuple(unique_indexes)


def cls_add_indexes(cls, indexes):
Expand Down
3 changes: 1 addition & 2 deletions gdcdatamodel/validators/graph_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class GDCGraphValidator(object):
def __init__(self):
self.schemas = gdcdictionary
self.required_validators = {
'links_validator': GDCLinksValidator(),
'uniqueKeys_validator': GDCUniqueKeysValidator(),
'links_validator': GDCLinksValidator()
}
self.optional_validators = {}

Expand Down
15 changes: 0 additions & 15 deletions test/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,3 @@ def test_graph_validator_with_correct_node(self):
'target_type': 'sample'}]}])
self.graph_validator.record_errors(g, self.entities)
self.assertEquals(0, len(self.entities[0].errors))

def test_graph_validator_with_existing_unique_keys(self):
with g.session_scope() as session:
node = self.create_node({'type': 'data_format',
'props': {'name': 'test'},
'edges': {}},
session)
node = self.create_node({'type': 'data_format',
'props': {'name': 'test'},
'edges': {}},
session)
self.update_schema('data_format', 'uniqueKeys', [['name']])
self.entities[0].node = node
self.graph_validator.record_errors(g, self.entities)
self.assertEquals(['name'], self.entities[0].errors[0]['keys'])