mirror of
https://github.com/rommapp/romm.git
synced 2026-12-02 10:08:40 +01:00
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:
@@ -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)
|
||||
|
||||
@@ -2,11 +2,20 @@ import threading
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import text
|
||||
|
||||
from exceptions.database_exceptions import LastAdminError
|
||||
from handler.database import db_user_handler
|
||||
from handler.database.base_handler import sync_session
|
||||
from handler.database import (
|
||||
db_device_handler,
|
||||
db_play_session_handler,
|
||||
db_user_handler,
|
||||
)
|
||||
from handler.database.base_handler import sync_engine, sync_session
|
||||
from models.device import Device
|
||||
from models.play_session import PlaySession
|
||||
from models.rom import Rom
|
||||
from models.user import Role, User
|
||||
from utils.database import is_postgresql
|
||||
from utils.datetime import to_utc
|
||||
|
||||
|
||||
@@ -104,6 +113,45 @@ class TestKeepAnAdmin:
|
||||
assert (_role(admin_user), _role(other_admin)) == (Role.ADMIN, Role.USER)
|
||||
|
||||
|
||||
class TestDeleteUser:
|
||||
def test_only_their_devices_and_play_sessions_go(
|
||||
self, admin_user: User, editor_user: User, rom: Rom
|
||||
):
|
||||
# A restored dump puts the unique key first, so InnoDB checks the user with it.
|
||||
with sync_engine.connect() as conn:
|
||||
if not is_postgresql(conn):
|
||||
conn.execute(text("ALTER TABLE play_sessions FORCE"))
|
||||
started = datetime.now(timezone.utc).replace(microsecond=0)
|
||||
for user in (admin_user, editor_user):
|
||||
device = db_device_handler.add_device(
|
||||
Device(id=f"{user.username}-device", user_id=user.id, name="Handheld")
|
||||
)
|
||||
db_play_session_handler.add_sessions(
|
||||
[
|
||||
PlaySession(
|
||||
user_id=user.id,
|
||||
device_id=device_id,
|
||||
rom_id=rom.id,
|
||||
start_time=started,
|
||||
end_time=started + timedelta(minutes=30),
|
||||
duration_ms=30 * 60 * 1000,
|
||||
)
|
||||
for device_id in (device.id, None)
|
||||
]
|
||||
)
|
||||
|
||||
db_user_handler.delete_user(editor_user.id)
|
||||
|
||||
assert db_user_handler.get_user(editor_user.id) is None
|
||||
assert db_device_handler.get_devices(user_id=editor_user.id) == []
|
||||
assert db_play_session_handler.get_sessions(user_id=editor_user.id) == []
|
||||
left = db_play_session_handler.get_sessions(user_id=admin_user.id)
|
||||
assert {session.device_id for session in left} == {
|
||||
f"{admin_user.username}-device",
|
||||
None,
|
||||
}
|
||||
|
||||
|
||||
class TestUpdateLastActive:
|
||||
def test_bumps_a_user_never_active(self, admin_user: User):
|
||||
db_user_handler.update_user(admin_user.id, {"last_active": None})
|
||||
|
||||
Reference in New Issue
Block a user