fix(users): delete a user who has play sessions on a device (#5757)

* fix(users): delete a user who has play sessions on a device

On MariaDB, deleting a user whose play sessions name a device failed with
error 1452 once play_sessions had been rebuilt, as restoring a dump does.
The cascade clears device_id on rows of the user being deleted, and InnoDB
rechecks their user through the index that also holds device_id. The user's
devices are now deleted first, while the user still exists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(users): force the play_sessions rebuild in the delete-user test

OPTIMIZE TABLE can defragment instead of rebuilding when innodb_defragment
is on (MariaDB 10.11), which would leave the index order unchanged.
ALTER TABLE ... FORCE always rebuilds. Addresses Greptile review.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
This commit is contained in:
MMagTech
2026-11-24 09:24:54 -05:00
committed by GitHub
parent 7b3dc380ed
commit 695fd5a9f1
2 changed files with 58 additions and 2 deletions

View File

@@ -8,6 +8,7 @@ from sqlalchemy.sql import Delete, Select, Update
from decorators.database import INJECTED_SESSION, begin_session
from exceptions.database_exceptions import LastAdminError
from models.device import Device
from models.snapshot import Snapshot
from models.user import Role, User
@@ -236,6 +237,13 @@ class DBUsersHandler(DBBaseHandler):
.where(Snapshot.user_id == id)
.execution_options(synchronize_session=False)
)
# InnoDB can recheck a play session's user while clearing its device, so
# the devices go while the user is still there.
session.execute(
delete(Device)
.where(Device.user_id == id)
.execution_options(synchronize_session=False)
)
return session.execute(
delete(User)
.where(User.id == id)