Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow user deletion without deleting task references #1848

Merged
merged 1 commit into from
Nov 9, 2024

Conversation

Anuj-Gupta4
Copy link
Contributor

What type of PR is this? (check all applicable)

  • 🍕 Feature

Related Issue

Fixes #1661

Describe this PR

On user deletion, task history will retain username from user
and rest of the user data will be deleted.

Checklist before requesting a review

@github-actions github-actions bot added backend Related to backend code migration Contains a DB migration labels Nov 4, 2024
@Anuj-Gupta4 Anuj-Gupta4 marked this pull request as draft November 4, 2024 12:07
@spwoodcock
Copy link
Member

So sorry @Anuj-Gupta4 , but this PR is based on staging, which is using SQLAlchemy 😅

We have since done a huge refactor on development to remove this and use psycopg, so this PR might need to be started from scratch with development as the base 😥

@spwoodcock spwoodcock changed the base branch from staging to development November 5, 2024 19:47
@spwoodcock
Copy link
Member

There is some really nice code here 😄

Could you possibly rebase with development (might be a lot of hassle considering the amount of change), or stash your changes somewhere, then reset the branch and reapply?

Also feel free to make a new PR if it's easier, then I'll take a look!

@Anuj-Gupta4
Copy link
Contributor Author

@spwoodcock I have reset my local branch and am working over development code. I will be overwriting the git history once its completed.

Copy link
Member

@spwoodcock spwoodcock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice work! 🙌

src/backend/app/db/models.py Outdated Show resolved Hide resolved
src/backend/app/db/models.py Show resolved Hide resolved
src/backend/app/db/models.py Outdated Show resolved Hide resolved
src/backend/migrations/010-delete-user.sql Outdated Show resolved Hide resolved
@classmethod
async def delete(cls, db: Connection, user_id: int) -> bool:
"""Delete a user and their related data."""
async with db.cursor() as cur:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change this to a single SQL and await the execution at once? What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately that's not possible with psycopg! It's only one statement per cursor 😅
(SQLAlchemy hides this fact!).

It looks inefficient, but it still executes them all in the same session, so it's not bad

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you have done a similar delete api for the organisation with a single sql.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right! Does it actually work? I would think it might error, but could be wrong.

I think the project deletion has it separated for this reason

Copy link
Member

@spwoodcock spwoodcock Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ignore me, I see what you mean now!

The org deletion is a single (chained) prepared statement, so that works fine

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if it works, I haven't tested it yet. 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whereas the project deletion is multiple statements I think!

The approach of a single statement is actually quite nice - we can go with that if possible 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if it works, I haven't tested it yet. 🤔

I think it does!

@Anuj-Gupta4
Copy link
Contributor Author

@spwoodcock The tests are passing locally. I am not sure why it is failing here.

)
await DbUser.delete(db, user.id)
log.info(f"User {user.id} deleted successfully.")
return Response(status_code=HTTPStatus.NO_CONTENT)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's best to return a response with a similar message as in the log instead of NO_CONTENT.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good status code to use, but you are right we could include a message too 👍

I think this was replicated from elsewhere where I did similar, so we could also update those deletion APIs to return a message too!

@spwoodcock spwoodcock merged commit ae6ed15 into hotosm:development Nov 9, 2024
4 of 6 checks passed
@spwoodcock
Copy link
Member

Oops forgot to wait for the other PR comments to be addressed before merge.

It would be nice to possibly address them in another PR if time @Anuj-Gupta4 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Related to backend code migration Contains a DB migration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Backend API to delete user account
3 participants