-
Notifications
You must be signed in to change notification settings - Fork 191
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
Code
: add additional validation before storing
#5204
Conversation
@giovannipizzi @chrisjsewell @ramirezfranciscof any reason why a codes label should be allowed to be empty? I think this should be pretty uncontroversial as a change. Making the full label unique is bit trickier. I think it is the right thing to do, but are there reasonable use cases to allow duplicates? And if not, are there any risks with disallowing this from v2.0 by merging this PR? I am trying to think if there are potential problems with databases that currently violate this uniqueness constraint. For example, if codes with duplicate full labels are exported and then imported (and this goes through the front-end ORM) it would raise. Good thing is that the label of the code is mutable so people can manually "fix" the problem if they have duplicates. |
d81ed24
to
b0f6ac6
Compare
|
f71ee05
to
a0e5ea8
Compare
Up till now it was possible to store a `Code` instance without a label; either an empty string or even `None`. This is now checked in the `_validate` method, which is automatically called when `store` is called. It will raise a `ValidationError` if no valid label is defined. In addition, a check is added for the uniqueness of the full label, i.e., the string `label@computer.label`. Since this is usually used as an identifier for a code, allowing duplicates will make it impossible to load a code with it and one has to resort to using the PK or UUID. One would imagine that these conditions should be enforced on the database level, however, for the empty label this is not straight-forward as the `Code` is just a special case of the node table and there the label is optional and so `None` and the empty string are perfectly possible. The same goes for the full label. This is a uniqueness constraint across two tables, but it requires a `WHERE` statement, since it should only apply if `node_type == data.code.Code.`. This is possible in Postgres through partial indexes, but it is not clear if both Django and SqlAlchemy support defining these on the models. Finally, some tests have been cleaned up: * `tests/test_generic.py`: has been deleted and its contents have been moved to `tests/orm/data/test_code.py` and `tests/orm/data/test_bool.py` * `tests/test_nodes.py`: tests related to the `Code` class have been moved to `tests/orm/data/test_code.py`.
a0e5ea8
to
7e748c9
Compare
Thinking about this, the real problem is maybe that the uniqueness condition is not |
Codecov Report
@@ Coverage Diff @@
## develop #5204 +/- ##
===========================================
- Coverage 81.20% 81.18% -0.01%
===========================================
Files 532 532
Lines 37307 37317 +10
===========================================
+ Hits 30290 30293 +3
- Misses 7017 7024 +7
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
After discussion it was decided that the potential for breaking existing archives and databases was great and the added benefit was small, so we decide to leave this as is. |
Fixes #5203
Up till now it was possible to store a
Code
instance without a label;either an empty string or even
None
. This is now checked in the_validate
method, which is automatically called whenstore
iscalled. It will raise a
ValidationError
if no valid label is defined.In addition, a check is added for the uniqueness of the full label,
i.e., the string
label@computer.label
. Since this is usually used asan identifier for a code, allowing duplicates will make it impossible to
load a code with it and one has to resort to using the PK or UUID.
One would imagine that these conditions should be enforced on the
database level, however, for the empty label this is not
straight-forward as the
Code
is just a special case of the node tableand there the label is optional and so
None
and the empty string areperfectly possible. The same goes for the full label. This is a
uniqueness constraint across two tables, but it requires a
WHERE
statement, since it should only apply if
node_type == data.code.Code.
.This is possible in Postgres through partial indexes, but it is not
clear if both Django and SqlAlchemy support defining these on the models.