Add an assert
function to the standard library
#5040
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Using error raising functions in postgres (and thus in edgeql) is
somewhat fraught, since the semantics of when errors are raised and
when they may be "optimized out" is somewhat unclear.
We should document some recommended usage patterns:
In triggers
This is probably the most important usage pattern, and works without
complication.
Just put it at the top level or in a
FOR
loop:On some condition of each object you are returning
Put the assertion in an
ORDER BY
clause:Putting the
assert
call in anorder by
clause ensures it is onlyevaluated on objects that passed the
filter
.(In practice, putting the
assert
as part of thefilter
willprobably work, if it depends on the object itself, but
order by
should be less fragile.)
When you have a condition not directly related to the data
Here things get kind of ugly, there are a couple options.
FOR
iterator:This should always work if the data is non-empty, but in some cases
might not raise the assertion if the object is empty.
FILTER
orORDER BY
on the freeobject:
This works but means the data is wrapped in a free object.
This is solid, but kind of atrocious. It will be less nasty maybe once
we have DML-supporting
IF
.