Lock Receipt during validation and other concurrency improvements#217
Draft
npazosmendez wants to merge 1 commit intoWhyNotHugo:mainfrom
Draft
Lock Receipt during validation and other concurrency improvements#217npazosmendez wants to merge 1 commit intoWhyNotHugo:mainfrom
Receipt during validation and other concurrency improvements#217npazosmendez wants to merge 1 commit intoWhyNotHugo:mainfrom
Conversation
Receipt during validation and other concurrency improvements
WhyNotHugo
reviewed
Jul 3, 2024
Owner
WhyNotHugo
left a comment
There was a problem hiding this comment.
Se ve bien, sólo tengo feedback sobre detalles menores.
| ) | ||
| if cae_data.Observaciones: | ||
| with transaction.atomic(): | ||
| qs = Receipt.objects.filter(id__in=ids, receipt_number__isnull=False).select_for_update() |
Owner
There was a problem hiding this comment.
Podés reusar el QuerySet existente acá:
Suggested change
| qs = Receipt.objects.filter(id__in=ids, receipt_number__isnull=False).select_for_update() | |
| qs = qs.select_for_update() |
Contributor
Author
There was a problem hiding this comment.
Eso intenté en un principio, pero me da esta excepción:
self = <django.db.backends.utils.CursorWrapper object at 0x7f219c6762a0>
sql = 'SELECT "afip_receipt"."id", "afip_receipt"."point_of_sales_id", "afip_receipt"."receipt_type_id", "afip_receipt"."con..."."id" = %s AND "afip_receiptvalidation"."id" IS NULL) ORDER BY "afip_receipt"."receipt_number" ASC LIMIT 1 FOR UPDATE'
params = (12,), ignored_wrapper_args = (False, {'connection': <DatabaseWrapper vendor='postgresql' alias='default'>, 'cursor': <django.db.backends.utils.CursorWrapper object at 0x7f219c6762a0>})
def _execute(self, sql, params, *ignored_wrapper_args):
# Raise a warning during app initialization (stored_app_configs is only
# ever set during testing).
if not apps.ready and not apps.stored_app_configs:
warnings.warn(self.APPS_NOT_READY_WARNING_MSG, category=RuntimeWarning)
self.db.validate_no_broken_transaction()
with self.db.wrap_database_errors:
if params is None:
# params default might be backend specific.
return self.cursor.execute(sql)
else:
> return self.cursor.execute(sql, params)
E django.db.utils.NotSupportedError: FOR UPDATE cannot be applied to the nullable side of an outer join
.tox/live/lib/python3.12/site-packages/django/db/backends/utils.py:105: NotSupportedErrorMe parece que el problema es el qs = self.filter(validation__isnull=True) de un par de lineas más arriba. Por lo que leí, Postgres dice que no puede lockear las filas según un criterio como ese, porque en cualquier momento podría suceder que validation__isnull=True si se agregara un Validation que apunte al Receipt; tendría que lockear la tabla Validations entera supongo, o algo similar.
Decime si estoy tirando fruta, no sé mucho PG pero menos aun Django
|
|
||
| # Remove the number from ones that failed to validate: | ||
| qs.filter(validation__isnull=True).update(receipt_number=None) | ||
| Receipt.objects.filter(id__in=ids, validation__isnull=True).update(receipt_number=None) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.