Files
dify/api/tests/helpers/unit/second_precision_datetimes.py
JyongandClaude Fable 5.2 f3597537e9 fix(api): make invitation completion cluster-safe and fix second-precision tokens
- Complete invitation acceptance with single-key Redis operations; the
  four-key Lua script was rejected by Redis Cluster after the DB commit.
- Keep the invitation token valid after a login-time membership restore
  so the invite-settings step can still activate it once.
- Take the global workspace quota lock only on Enterprise, where a
  workspace-count limit exists.
- Write retry and summary CAS timestamps at second precision so equality
  holds on MySQL DateTime columns.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-17 15:36:05 +08:00

50 lines
1.8 KiB
Python

"""Make a SQLite engine store datetimes the way MySQL ``DATETIME`` columns do."""
import re
from collections.abc import Generator
from contextlib import contextmanager
from datetime import datetime, timedelta
from sqlalchemy import Engine, event
_SQLITE_DATETIME = re.compile(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{6}")
_SQLITE_DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S.%f"
def _round_to_second(value: object) -> object:
if not isinstance(value, str) or _SQLITE_DATETIME.fullmatch(value) is None:
return value
rounded = (datetime.strptime(value, _SQLITE_DATETIME_FORMAT) + timedelta(microseconds=500_000)).replace(
microsecond=0
)
return rounded.strftime(_SQLITE_DATETIME_FORMAT)
@contextmanager
def second_precision_datetimes(engine: Engine) -> Generator[None]:
"""Round every datetime bound on ``engine`` to whole seconds, as MySQL does on write.
SQLite keeps microseconds, which hides code that compares an in-memory
microsecond timestamp with the value a second-precision column returns.
"""
def round_parameters(
_connection: object,
_cursor: object,
statement: str,
parameters: object,
_context: object,
executemany: bool,
) -> tuple[str, object]:
if executemany and isinstance(parameters, (list, tuple)):
return statement, [tuple(_round_to_second(value) for value in row) for row in parameters]
if isinstance(parameters, (list, tuple)):
return statement, tuple(_round_to_second(value) for value in parameters)
return statement, parameters
event.listen(engine, "before_cursor_execute", round_parameters, retval=True)
try:
yield
finally:
event.remove(engine, "before_cursor_execute", round_parameters)