GitHub duplication Fixed and Migration#1044
GitHub duplication Fixed and Migration#1044iamYashSinha wants to merge 72 commits intoRealDevSquad:developfrom
Conversation
prakashchoudhary07
left a comment
There was a problem hiding this comment.
Nicely done 🚀
prakashchoudhary07
left a comment
There was a problem hiding this comment.
Tests are failing, please check
|
Can you please add tests for the case where the GitHub id is updated? |
|
The test are failing rest things, look good. |
|
Yes working on the test cases. |
|
@iamYashSinha Please share the migration plan for existing uses, how will we add github_user_id to them? |
Migration Planinitially, the plan was to revoke all the users & tell them to reauthorize by visiting the Real Dev Squad website. But this method ended up creating another document of that user and then adding the github_user_id field to it. As of now, I've written the logic for the new users, but in order to do migration I've to find a way where can add the new github_user_id field to the existing user without creating any other document |
2023-04-26.21-20-29.mp4 |
cae90c7 to
6d309e3
Compare
…into bugfix/issue957
…ackend into bugfix/issue957
heyrandhir
left a comment
There was a problem hiding this comment.
One concern that comes to mind is the possibility of the request timing out. Since we are performing multiple operations for all users, there is a risk that it could impact the production data or what if some docs are changed and some fail before the request is timed out 👀
controllers/users.js
Outdated
| const invalidUsers = { | ||
| userId: `${userId}`, | ||
| username: `${username}`, | ||
| githubUsername: `${githubUsername}`, | ||
| }; |
There was a problem hiding this comment.
Nit : we could have just used the shorter format of creating objects instead of template literals
const invalidUsers = {userId, username, githubUsername };
controllers/users.js
Outdated
| }) | ||
| .catch((error) => { | ||
| if (error.response && error.response.status === 404) { | ||
| countUserNotFound++; |
There was a problem hiding this comment.
shouldn't count not found ++ and usersNotFound.push happen before the if block as if it doesnt go inside the if block then there will be a mismatch
There was a problem hiding this comment.
Thanks for pointing it out. Updated the code
iamitprakash
left a comment
There was a problem hiding this comment.
please add test coverage stats
controllers/users.js
Outdated
| }; | ||
|
|
||
| // one time script function to perform the migration - adding github_user_id field to the document | ||
| const migrate = async (req, res) => { |
There was a problem hiding this comment.
could we use a better name for this please
There was a problem hiding this comment.
Updated the name to addGtihubId
| const batchCount = Math.ceil(totalUsers / 500); | ||
| // Create batch write operations for each batch of documents | ||
| for (let i = 0; i < batchCount; i++) { | ||
| const batchDocs = usersSnapshot.docs.slice(i * 500, (i + 1) * 500); |
There was a problem hiding this comment.
rather than doing this why not directly fetch just 500 from db itself? something like paginated ones.
There was a problem hiding this comment.
It will lead to extra reads on our database as in firestore it reads all the documents up from the startAt document id and also this is simpler in terms of code readability
| data: { | ||
| totalUsers: totalUsers, | ||
| usersUpdated: countUserFound, | ||
| usersNotUpdated: countUserNotFound, |
There was a problem hiding this comment.
if we are sending this i think we should add the reason as well for not updating.
There was a problem hiding this comment.
There can be different reasons. So IMO, in such a case we can check our logs for the reason
test/integration/users.test.js
Outdated
| Sinon.restore(); | ||
| }); | ||
|
|
||
| it("Should update the user", async function () { |
There was a problem hiding this comment.
i think the title must be should add github_id to the user
test/integration/users.test.js
Outdated
| .post(`/users/migrate`) | ||
| .set("Cookie", `${cookieName}=${superUserAuthToken}`); | ||
| expect(usersMigrateResponse).to.have.status(200); | ||
| expect(usersMigrateResponse.body).to.eql({ |
There was a problem hiding this comment.
please use to deep equal or strict equal
| router.get("/suggestedUsers/:skillId", authenticate, authorizeRoles([SUPERUSER]), users.getSuggestedUsers); | ||
|
|
||
| // WARNING!! - One time Script/Route to do migration. | ||
| router.post("/migrate", authenticate, authorizeRoles([SUPERUSER]), users.addGithubId); |
Check failure
Code scanning / CodeQL
Missing rate limiting
RitikJaiswal75
left a comment
There was a problem hiding this comment.
Please make changes of removing the github_user_id from a few users and then testing if your script adds the field to the users.
| router.get("/suggestedUsers/:skillId", authenticate, authorizeRoles([SUPERUSER]), users.getSuggestedUsers); | ||
|
|
||
| // WARNING!! - One time Script/Route to do migration. | ||
| router.post("/migrate", authenticate, authorizeRoles([SUPERUSER]), users.addGithubId); |
There was a problem hiding this comment.
migrate represents an action/verb verbs should not be a part of the API name
There was a problem hiding this comment.
github-id-migrator or user-data-migrator
Is this fine?
Updated the test to add a user without https://github.com/Real-Dev-Squad/website-backend/blob/7f791932cfc60a53340f010613d07ae3cfa254a8/test/integration/users.test.js#L1259-L1262 |

Close: [RFC] GitHub Duplication #957
2. Refer to the video
2023-04-24.21-31-14.mp4
UNIT TESTS
✔ should add the github_user_id to the user collection
✔ should update the github_user_id in the user collection (40ms)
✔ should be stored correctly in the database
INTEGRATION TESTS
✔ Should update the user
✔ Should return an unauthorized error when not logged in
Test Coverage Stats
Controllers
RFC DOCUMENT
MIGRATION DOCUMENT
Ticket for cleanup strategy Remove temporary/one-time-use migration endpoint for GitHub duplication fix #1331