Skip to content

Commit

Permalink
validor more explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
ychiucco committed Sep 24, 2024
1 parent 6df1252 commit f156c4b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fractal_server/app/models/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class UserOAuth(SQLModel, table=True):
foreign_key="user_settings.id", default=None
)
settings: Optional[UserSettings] = Relationship(
sa_relationship_kwargs=dict(lazy="selectin")
sa_relationship_kwargs=dict(lazy="selectin", cascade="all, delete")
)

class Config:
Expand Down
31 changes: 31 additions & 0 deletions tests/no_version/test_delete_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from sqlmodel import select

from fractal_server.app.models.security import UserOAuth
from fractal_server.app.models.security import UserSettings
from fractal_server.app.models.v1 import LinkUserProject
from fractal_server.app.models.v2 import LinkUserProjectV2

Expand Down Expand Up @@ -54,3 +55,33 @@ async def test_delete_user(
assert len(project_v2.user_list) == 0
assert len(project_v1_2.user_list) == 1
assert len(project_v2_2.user_list) == 1


async def test_cascade_on_delete_user_settings(db):

user = UserOAuth(
email="user@fractal.xy",
hashed_password="fake_hashed_password",
settings=UserSettings(slurm_accounts=["account1", "account2"]),
)
db.add(user)
await db.commit()

await db.refresh(user)
user_id = user.id
user_settings_id = user.user_settings_id

await db.close()

user = await db.get(UserOAuth, user_id)
assert user is not None
user_settings = await db.get(UserSettings, user_settings_id)
assert user_settings is not None

await db.delete(user)
await db.commit()

user = await db.get(UserOAuth, user_id)
assert user is None
user_settings = await db.get(UserSettings, user_settings_id)
assert user_settings is None

0 comments on commit f156c4b

Please sign in to comment.