Skip to content

Commit

Permalink
bug: Fix decimal.InvalidOperation on /system for some PG versions
Browse files Browse the repository at this point in the history
In #2730 the /system page was improved to warn the user if the version of Postgres they are using is out of date and should be updated. The current code attempts to determine the major versions by replacing `00` with `.` and then converting to a `Decimal`. Unfortunately, it appears the only value this method _does not_ work for are initial releases of major versions, like `16.0.0`.

For reference, either Postgres or the PsyCog driver represents the semver values but without the dots, so `16.0.0` becomes `1600000`.

This change removes the string replace and Decimal conversion in favor of using the divmod() function. In this application it will return a tuple with the first element being the major version of Postgres. This is then used as before to compare against deprecated versions.
  • Loading branch information
richid committed Mar 8, 2024
1 parent 8e9285a commit 3489216
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions cookbook/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,10 @@ def system(request):

if postgres:
postgres_current = 16 # will need to be updated as PostgreSQL releases new major versions
from decimal import Decimal

from django.db import connection

postgres_ver = Decimal(str(connection.pg_version).replace('00', '.'))
postgres_ver = divmod(connection.pg_version, 10000)
if postgres_ver >= postgres_current:
database_status = 'success'
database_message = _('Everything is fine!')
Expand Down

0 comments on commit 3489216

Please sign in to comment.