-
Notifications
You must be signed in to change notification settings - Fork 0
Migrate legacy images #32
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
Open
rawcomposition
wants to merge
2
commits into
main
Choose a base branch
from
legacy-images-model
base: main
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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,45 @@ | ||
| import mongoose from "mongoose"; | ||
| const { Schema, model, models } = mongoose; | ||
| import { LegacyImage as LegacyImageT } from "lib/types"; | ||
|
|
||
| const LegacyImageSchema = new Schema( | ||
| { | ||
| locationId: { | ||
| type: String, | ||
| required: true, | ||
| }, | ||
| type: { | ||
| type: String, | ||
| enum: ["hotspot", "group"], | ||
| required: true, | ||
| }, | ||
| xsUrl: String, | ||
| smUrl: { | ||
| type: String, | ||
| required: true, | ||
| }, | ||
| lgUrl: String, | ||
| by: String, | ||
| email: String, | ||
| uid: String, | ||
| caption: String, | ||
| width: Number, | ||
| height: Number, | ||
| size: Number, | ||
| isMap: Boolean, | ||
| isStreetview: Boolean, | ||
| isPublicDomain: Boolean, | ||
| legacy: Boolean, | ||
| streetviewData: Object, | ||
| isMigrated: Boolean, | ||
| hideFromChildren: Boolean, | ||
| order: Number, | ||
| }, | ||
| { timestamps: true } | ||
| ); | ||
|
|
||
| LegacyImageSchema.index({ locationId: 1, type: 1, isMap: 1, isMigrated: 1, order: 1 }); | ||
|
|
||
| const LegacyImage = models.LegacyImage || model("LegacyImage", LegacyImageSchema); | ||
|
|
||
| export default LegacyImage as mongoose.Model<LegacyImageT>; | ||
This file contains hidden or 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 hidden or 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,124 @@ | ||
| import mongoose from "mongoose"; | ||
| import * as dotenv from "dotenv"; | ||
| import Hotspot from "../models/Hotspot"; | ||
| import Group from "../models/Group"; | ||
| import LegacyImage from "../models/LegacyImage"; | ||
|
|
||
| type ModelName = "Hotspot" | "Group"; | ||
| type SourceIdField = "locationId"; | ||
|
|
||
| dotenv.config(); | ||
|
|
||
| const MODEL_TO_MIGRATE: ModelName = "Group"; | ||
| const BATCH_SIZE = 1000; | ||
|
|
||
| const URI = process.env.MONGO_URI; | ||
| const connect = async () => (URI ? mongoose.connect(URI) : null); | ||
|
|
||
| type LegacyType = "hotspot" | "group"; | ||
|
|
||
| const MODEL_CONFIG: Record< | ||
| ModelName, | ||
| { model: mongoose.Model<any>; sourceIdField: SourceIdField; legacyType: LegacyType } | ||
| > = { | ||
| Hotspot: { model: Hotspot, sourceIdField: "locationId", legacyType: "hotspot" }, | ||
| Group: { model: Group, sourceIdField: "locationId", legacyType: "group" }, | ||
| }; | ||
|
|
||
| const migrateImages = async () => { | ||
| await connect(); | ||
|
|
||
| const { model: Model, sourceIdField, legacyType } = MODEL_CONFIG[MODEL_TO_MIGRATE]; | ||
|
|
||
| let lastCursor: string | null = null; | ||
| let totalDocs = 0; | ||
| let totalImages = 0; | ||
| let totalWrites = 0; | ||
| let totalSkipped = 0; | ||
|
|
||
| while (true) { | ||
| const query: any = { | ||
| images: { $exists: true, $ne: [] }, | ||
| [sourceIdField]: { $exists: true, $ne: "" }, | ||
| }; | ||
| if (lastCursor) { | ||
| query[sourceIdField] = { $gt: lastCursor }; | ||
| } | ||
|
|
||
| const docs = await Model.find(query) | ||
| .select({ images: 1, [sourceIdField]: 1 }) | ||
| .sort({ [sourceIdField]: 1 }) | ||
| .limit(BATCH_SIZE) | ||
| .lean(); | ||
| if (!docs.length) { | ||
| break; | ||
| } | ||
|
|
||
| const legacyDocs: any[] = []; | ||
|
|
||
| for (const doc of docs) { | ||
| totalDocs += 1; | ||
| const sourceId = String(doc[sourceIdField]); | ||
| lastCursor = sourceId; | ||
|
|
||
| const images = Array.isArray(doc.images) ? doc.images : []; | ||
| images.forEach((image: any, index: number) => { | ||
| if (!!image?.ebirdId) { | ||
| totalSkipped += 1; | ||
| return; | ||
| } | ||
| if (!image?.smUrl) { | ||
| totalSkipped += 1; | ||
| return; | ||
| } | ||
|
|
||
| totalImages += 1; | ||
|
|
||
| const legacyDoc = { | ||
| locationId: sourceId, | ||
| type: legacyType, | ||
| xsUrl: image.xsUrl, | ||
| smUrl: image.smUrl, | ||
| lgUrl: image.lgUrl, | ||
| by: image.by, | ||
| email: image.email, | ||
| uid: image.uid, | ||
| caption: image.caption, | ||
| width: image.width, | ||
| height: image.height, | ||
| size: image.size, | ||
| isMap: legacyType === "group" ? true : image.isMap, | ||
| isStreetview: image.isStreetview, | ||
| isPublicDomain: image.isPublicDomain, | ||
| legacy: image.legacy, | ||
| streetviewData: image.streetviewData, | ||
| isMigrated: image.isMigrated, | ||
| hideFromChildren: image.hideFromChildren, | ||
| order: index, | ||
| }; | ||
|
|
||
| legacyDocs.push(legacyDoc); | ||
| }); | ||
| } | ||
|
|
||
| if (legacyDocs.length) { | ||
| try { | ||
| const inserted = await LegacyImage.insertMany(legacyDocs, { ordered: false }); | ||
| totalWrites += inserted.length; | ||
| } catch (err: any) { | ||
| const insertedCount = err?.insertedDocs?.length ?? 0; | ||
| totalWrites += insertedCount; | ||
| console.warn(`InsertMany had errors; inserted ${insertedCount} docs in this batch.`); | ||
| } | ||
| } | ||
|
|
||
| console.log( | ||
| `Processed ${totalDocs} docs | migrated ${totalImages} images | skipped ${totalSkipped} | writes ${totalWrites}` | ||
| ); | ||
| } | ||
|
|
||
| console.log("Done!"); | ||
| process.exit(); | ||
| }; | ||
|
|
||
| migrateImages(); |
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.
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.
Type enum mismatch between TypeScript and Mongoose schema
The
LegacyImageTypeScript type inlib/types.tsdefinestypeas"hotspot" | "group" | "drive", but the Mongoose schema inmodels/LegacyImage.tsonly allowsenum: ["hotspot", "group"]. Since theDrivemodel has animagesarray, attempting to create aLegacyImagewithtype: "drive"will pass TypeScript validation but fail at runtime with a Mongoose validation error.Additional Locations (1)
lib/types.ts#L33-L34