-
Notifications
You must be signed in to change notification settings - Fork 273
GitHub duplication Fixed and Migration #1044
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
base: develop
Are you sure you want to change the base?
GitHub duplication Fixed and Migration #1044
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nicely done 🚀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit : we could have just used the shorter format of creating objects instead of template literals
const invalidUsers = {userId, username, githubUsername };
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
controllers/users.js
Outdated
}) | ||
.catch((error) => { | ||
if (error.response && error.response.status === 404) { | ||
countUserNotFound++; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing it out. Updated the code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we use a better name for this please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rather than doing this why not directly fetch just 500 from db itself? something like paginated ones.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we are sending this i think we should add the reason as well for not updating.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think the title must be should add github_id to the user
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the title
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use to deep equal or strict equal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the code
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
migrate represents an action/verb verbs should not be a part of the API name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
github-id-migrator
or user-data-migrator
Is this fine?
Updated the test to add a user without website-backend/test/integration/users.test.js Lines 1259 to 1262 in 7f79193
website-backend/test/integration/users.test.js Lines 1286 to 1290 in 7f79193
|
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