-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1052 from AletheiaFact/stage
Production Hotfix: Create source migration which moves extra properties to the props field #1051
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Db } from "mongodb"; | ||
|
||
export async function up(db: Db) { | ||
const sourcesCursor = await db.collection("sources").find(); | ||
|
||
while (await sourcesCursor.hasNext()) { | ||
const source = await sourcesCursor.next(); | ||
const { _id, user, href, targetId } = source; | ||
|
||
const updateData: any = { | ||
_id, | ||
targetId, | ||
href, | ||
user, | ||
props: {}, | ||
}; | ||
|
||
const copyPropsIfExist = (key) => { | ||
if (source[key] || source.props?.[key]) { | ||
updateData.props[key] = source[key] || source.props[key]; | ||
} | ||
}; | ||
|
||
copyPropsIfExist("targetReference"); | ||
copyPropsIfExist("targetText"); | ||
copyPropsIfExist("textRange"); | ||
|
||
await db.collection("sources").update({ _id: source._id }, updateData); | ||
} | ||
} |