generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 264
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
migration: added migration script for migrate old users status in new collections #2177
Draft
sahsisunny
wants to merge
2
commits into
develop
Choose a base branch
from
migrate/user-status
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import admin from "firebase-admin"; | ||
import firestore from "../utils/firestore"; | ||
|
||
const oldUserStatusCollection = firestore.collection("usersStatus"); | ||
const newUserStatusCollection = firestore.collection("userStatus"); | ||
const newUserFutureStatusCollection = firestore.collection("userFutureStatus"); | ||
|
||
const BATCH_SIZE = 500; | ||
|
||
interface OldUserStatus { | ||
userId: string; | ||
currentStatus?: { | ||
from: number; | ||
until?: number; | ||
state: string; | ||
message?: string; | ||
}; | ||
futureStatus?: { | ||
from: number; | ||
until?: number; | ||
state: string; | ||
message?: string; | ||
}; | ||
} | ||
|
||
interface NewUserStatus { | ||
userId: string; | ||
appliedOn: Date; | ||
status: string; | ||
state: "CURRENT" | "PAST"; | ||
endedOn?: Date; | ||
message?: string; | ||
} | ||
|
||
interface NewUserFutureStatus { | ||
userId: string; | ||
from: Date; | ||
status: string; | ||
state: "UPCOMING" | "APPLIED" | "NOT_APPLIED"; | ||
endsOn?: Date; | ||
message?: string; | ||
} | ||
|
||
interface MigrationResult { | ||
totalProcessed: number; | ||
totalMigrated: number; | ||
totalSkipped: number; | ||
batches: { | ||
processedCount: number; | ||
migratedCount: number; | ||
skippedCount: number; | ||
}[]; | ||
} | ||
|
||
async function fetchBatch(lastDoc?: admin.firestore.QueryDocumentSnapshot): Promise<admin.firestore.QuerySnapshot> { | ||
let query = oldUserStatusCollection.orderBy("userId").limit(BATCH_SIZE); | ||
if (lastDoc) { | ||
query = query.startAfter(lastDoc); | ||
} | ||
return await query.get(); | ||
} | ||
|
||
async function userExistsInNewCollections(userId: string): Promise<boolean> { | ||
const existingStatus = await newUserStatusCollection.where("userId", "==", userId).limit(1).get(); | ||
const existingFutureStatus = await newUserFutureStatusCollection.where("userId", "==", userId).limit(1).get(); | ||
return !existingStatus.empty || !existingFutureStatus.empty; | ||
} | ||
|
||
function prepareStatus(data: OldUserStatus): NewUserStatus | null { | ||
if (!data.currentStatus) return null; | ||
|
||
return { | ||
userId: data.userId, | ||
appliedOn: new Date(data.currentStatus.from), | ||
status: data.currentStatus.state, | ||
state: "CURRENT", | ||
endedOn: data.currentStatus.until ? new Date(data.currentStatus.until) : undefined, | ||
message: data.currentStatus.message, | ||
}; | ||
} | ||
|
||
function prepareFutureStatus(data: OldUserStatus): NewUserFutureStatus | null { | ||
if (!data.futureStatus) return null; | ||
|
||
return { | ||
userId: data.userId, | ||
from: new Date(data.futureStatus.from), | ||
status: data.futureStatus.state, | ||
state: "UPCOMING", | ||
endsOn: data.futureStatus.until ? new Date(data.futureStatus.until) : undefined, | ||
message: data.futureStatus.message, | ||
}; | ||
} | ||
|
||
async function migrateBatch( | ||
snapshot: admin.firestore.QuerySnapshot | ||
): Promise<{ migratedCount: number; skippedCount: number }> { | ||
const batch = firestore.batch(); | ||
let migratedCount = 0; | ||
let skippedCount = 0; | ||
|
||
for (const doc of snapshot.docs) { | ||
const data = doc.data() as OldUserStatus; | ||
const userId = data.userId; | ||
|
||
if (await userExistsInNewCollections(userId)) { | ||
skippedCount++; | ||
continue; | ||
} | ||
|
||
const status = prepareStatus(data); | ||
if (status) { | ||
batch.set(newUserStatusCollection.doc(), status); | ||
} | ||
|
||
const futureStatus = prepareFutureStatus(data); | ||
if (futureStatus) { | ||
batch.set(newUserFutureStatusCollection.doc(), futureStatus); | ||
} | ||
|
||
migratedCount++; | ||
} | ||
|
||
await batch.commit(); | ||
return { migratedCount, skippedCount }; | ||
} | ||
|
||
async function migrateUserStatus(): Promise<MigrationResult> { | ||
let lastDoc: admin.firestore.QueryDocumentSnapshot | undefined = undefined; | ||
let totalMigrated = 0; | ||
let totalSkipped = 0; | ||
let totalProcessed = 0; | ||
const batches: MigrationResult["batches"] = []; | ||
|
||
while (true) { | ||
const snapshot = await fetchBatch(lastDoc); | ||
if (snapshot.empty) { | ||
break; | ||
} | ||
|
||
const { migratedCount, skippedCount } = await migrateBatch(snapshot); | ||
totalMigrated += migratedCount; | ||
totalSkipped += skippedCount; | ||
totalProcessed += snapshot.size; | ||
|
||
batches.push({ | ||
processedCount: snapshot.size, | ||
migratedCount, | ||
skippedCount, | ||
}); | ||
|
||
lastDoc = snapshot.docs[snapshot.docs.length - 1]; | ||
} | ||
|
||
return { | ||
totalProcessed, | ||
totalMigrated, | ||
totalSkipped, | ||
batches, | ||
}; | ||
} | ||
|
||
export { migrateUserStatus }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Missing rate limiting High
Copilot Autofix AI 4 months ago
To fix the problem, we will add a rate-limiting middleware to the Express application. We will use the
express-rate-limit
package to limit the number of requests to the/migrate
endpoint. This will help prevent abuse and potential DoS attacks.express-rate-limit
package if it is not already installed.express-rate-limit
package in theroutes/userStatus.js
file./migrate
route.