migrations: Enforce evaluation order in 0306 WHERE clause.

Depending on PostgreSQL’s query plan, it was possible for the value
condition to be evaluated before the field_type condition was checked,
leading to errors like

psycopg2.errors.InvalidDatetimeFormat: invalid value "stri" for "YYYY"
DETAIL:  Value must be an integer.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-01-28 13:14:55 -08:00 committed by Tim Abbott
parent a42f7a67e1
commit 69890f36b1
1 changed files with 4 additions and 1 deletions

View File

@ -20,7 +20,10 @@ class Migration(migrations.Migration):
FROM zerver_customprofilefield AS f
WHERE f.id = field_id
AND f.field_type = 4
AND value <> to_char(to_date(value, 'YYYY-MM-DD'), 'YYYY-MM-DD');
AND CASE
WHEN f.field_type = 4
THEN value <> to_char(to_date(value, 'YYYY-MM-DD'), 'YYYY-MM-DD')
END;
""",
reverse_sql="",
),