fix(db): don't use STR_TO_DATE in a generated column

MariaDB 12.3 enforces that STR_TO_DATE (which reads lc_time_names) cannot
appear in a GENERATED ALWAYS AS clause, so migration 0098 failed with
error 1901 and RomM could not start. Earlier releases up to 12.2 accepted
it, which is why the problem only surfaced on 12.3.

The gamelist value is already pinned to a fixed-width "YYYYMMDDThhmmss" by
the regex guard, so it is reshaped into a canonical datetime literal with
SUBSTRING/CONCAT and fed to the existing TIMESTAMPDIFF. Stored values and
their time-zone independence are unchanged.

Fixes #3873

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-07-22 07:25:35 -04:00
parent ace658306e
commit 57e207cff2

View File

@@ -111,13 +111,30 @@ def _maria_first_release_date() -> str:
f"AND {val} REGEXP '^[0-9]+$' THEN {cast}"
)
gl = "JSON_UNQUOTE(JSON_EXTRACT(gamelist_metadata, '$.first_release_date'))"
# TIMESTAMPDIFF from the UTC epoch is pure calendar arithmetic, so (unlike
# UNIX_TIMESTAMP) the stored value does not depend on the session time zone.
# MariaDB rejects STR_TO_DATE in a GENERATED ALWAYS AS clause (it reads
# lc_time_names), so the fixed-width "YYYYMMDDThhmmss" string is reshaped
# into a canonical datetime literal instead. TIMESTAMPDIFF from the UTC
# epoch is pure calendar arithmetic, so (unlike UNIX_TIMESTAMP) the stored
# value does not depend on the session time zone.
parts = [
f"SUBSTRING({gl}, 1, 4)",
"'-'",
f"SUBSTRING({gl}, 5, 2)",
"'-'",
f"SUBSTRING({gl}, 7, 2)",
"' '",
f"SUBSTRING({gl}, 10, 2)",
"':'",
f"SUBSTRING({gl}, 12, 2)",
"':'",
f"SUBSTRING({gl}, 14, 2)",
]
gl_datetime = "CONCAT(" + ", ".join(parts) + ")"
branches.append(
f"WHEN JSON_CONTAINS_PATH(gamelist_metadata, 'one', '$.first_release_date') "
f"AND {gl} NOT IN ('null', 'None', '0', '0.0') "
f"AND {gl} REGEXP '^[0-9]{{8}}T[0-9]{{6}}$' "
f"THEN TIMESTAMPDIFF(SECOND, '1970-01-01 00:00:00', STR_TO_DATE({gl}, '%Y%m%dT%H%i%S')) * 1000"
f"THEN TIMESTAMPDIFF(SECOND, '1970-01-01 00:00:00', {gl_datetime}) * 1000"
)
return "CASE\n " + "\n ".join(branches) + "\n ELSE NULL END"