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

Fixed bug where memberships remained after a team is deleted #5928

Merged

Conversation

safwanyp
Copy link
Contributor

@safwanyp safwanyp commented Aug 1, 2023

What does this PR do?

This PR closes #5517.

  • Fixed a bug that caused team memberships to remain linked to a user even after a team is deleted.

Test Plan

Did a couple of tests by manually creating teams and memberships randomly. Kept track of the memberships and made sure they persisted and/or deleted as needed.

Related PRs and Issues

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

Copy link
Contributor

@stnguyen90 stnguyen90 left a comment

Choose a reason for hiding this comment

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

Left a comment about your approach.

Could you do some investigation to actually see if there is any data in the database that needs to be deleted for each user? Or is it maybe somewhere else?

app/controllers/api/teams.php Outdated Show resolved Hide resolved
@safwanyp
Copy link
Contributor Author

safwanyp commented Aug 2, 2023

Hey @stnguyen90 just pushed a commit to this PR.
I solved it by checking if the $team variable is empty. After going through a lot of code in the Utopia package, as well as keeping track of changes in the mariadb tables, seems like this is a decent fix. Let me know if this is better.

Since there aren't any more API calls being made than there were originally, I think it's good. Regardless, I'm willing to learn and if you think I could have found a better solution, let me know!!

app/controllers/api/users.php Outdated Show resolved Hide resolved
@safwanyp
Copy link
Contributor Author

safwanyp commented Aug 3, 2023

Just pushed a fix that definitely works 😶‍🌫️

HUGE thanks to @stnguyen90 for the absolutely amazing support he provided till now. I have learnt so much more about the system design of Appwrite, and was mesmerized to say the least.

That being said, the fix was actually really simple, and I just needed to implement cache invalidation when a team is deleted. According to network timings in the dev tools, the cache invalidation has added ~0.4ms per user to the response time. With 7 users, it added 2.7ms, and with 2 users, it took less than 1ms.

Feedback is appreciated!

Copy link
Contributor

@stnguyen90 stnguyen90 left a comment

Choose a reason for hiding this comment

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

Looks good! Can you add something to the tests to ensure the user's memberships are updated?

@safwanyp
Copy link
Contributor Author

safwanyp commented Aug 3, 2023

Looks good! Can you add something to the tests to ensure the user's memberships are updated?

Will do that now

@safwanyp
Copy link
Contributor Author

safwanyp commented Aug 3, 2023

Added a test to check the same and it was a success on localhost

Copy link
Contributor

@stnguyen90 stnguyen90 left a comment

Choose a reason for hiding this comment

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

2 additional things:

  1. when you're done making changes, make sure to re-request a review from your reviewer in GitHub.
  2. some suggestions and resources on git commit messages:
    1. How to Write a Git Commit Message
    2. My favourite Git commit | dhwthompson.com

tests/e2e/Services/Teams/TeamsBase.php Outdated Show resolved Hide resolved
tests/e2e/Services/Teams/TeamsBase.php Outdated Show resolved Hide resolved
tests/e2e/Services/Teams/TeamsBase.php Outdated Show resolved Hide resolved
app/controllers/api/teams.php Outdated Show resolved Hide resolved
This commit contains changes in 3 places.

- First I changed the placement of an informative comment in the DELETE TEAM controller, and moved it outside of the loop. I did this when Steven pointed out that the behaviour I describe in the comment is for the whole loop.

- The second change is the removal of my first test. I was facing quite a few issues with creating users in the test, and ended up using the CREATE TEAM MEMBERSHIP to perform 2 actions at once -> create a new user if one doesn't exist with the provided email, and create a membership for the user. Before this approach, I had quite a bit of code that didn't work, and it seems like I removed some things that weren't supposed to be removed, and didn't change variable names where necessary. Anyway, I figured that the problem has something to do with the user being created on the client side, so I moved the test to the server side.

- The new test I implemented does the same thing as my previous failed test, but in more detailed and distinct steps. The test first creates 5 new users inside of a loop, and pushes each new user's ID to an array called 'new_users' if the response is as expected. Then a new team is created. The next step is to create memberships for all 5 users. If all these steps pass, the new team that was just created, is deleted, and we check to make sure the new users have 0 team memberships each.

Formatter and linter showed no errors. Tests were successful on localhost.
@safwanyp
Copy link
Contributor Author

safwanyp commented Aug 4, 2023

Re-requested review.

Thanks for the helpful resources Steven, I honestly used to try and write descriptive commit messages, but that habit died down slowly lol. Will definitely start writing more descriptive commits from now on!

Copy link
Contributor

@stnguyen90 stnguyen90 left a comment

Choose a reason for hiding this comment

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

Will definitely start writing more descriptive commits from now on!

Sounds good! 👍🏼 Also, don't forget to use imperative mood instead of past tense!

tests/e2e/Services/Teams/TeamsBaseServer.php Outdated Show resolved Hide resolved
tests/e2e/Services/Teams/TeamsBaseServer.php Outdated Show resolved Hide resolved
The CREATE TEAM MEMBERSHIP endpoint requires the email of the user to be added to the team. If the user does not exist in the project, a new user is created with the specified email and added to the team.

The first version of the test creates 5 users, and then adds them to the newly created team. This process is more streamlined now, by using the CREATE TEAM MEMBERSHIPS behaviour to create a user on the go and create a membership for them immediately after.

I also change the way I add user IDs to the array, by using the shorthand notation instead of the `array_push` function.
Copy link
Contributor

@stnguyen90 stnguyen90 left a comment

Choose a reason for hiding this comment

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

looks like linter failed.

Run the composer format and lint commands, which I forgot to run before.
@safwanyp
Copy link
Contributor Author

safwanyp commented Aug 4, 2023

looks like linter failed.

Honestly forgot to run the formatter and linter. Fixed 1/1 issue and pushed.

Copy link
Contributor

@stnguyen90 stnguyen90 left a comment

Choose a reason for hiding this comment

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

Great work!

@safwanyp
Copy link
Contributor Author

safwanyp commented Aug 4, 2023

Great work!

Huge thanks to you Steven! Really appreciate all the help!

@eldadfux eldadfux merged commit 06570a0 into appwrite:master Aug 5, 2023
5 checks passed
@eldadfux
Copy link
Member

eldadfux commented Aug 5, 2023

Thank you @safwanyp!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 Bug Report: Users can see stale membership
3 participants