-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat: add script for data integrity after postgres migration #37998
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
Merged
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| import { MongoClient } from "mongodb"; | ||
| import pg from "pg"; | ||
| import { transformFields } from "./utils.mjs"; | ||
|
|
||
| function isArchivedObject(doc) { | ||
| return doc.deleted === true || doc.deletedAt != null; | ||
| } | ||
|
|
||
| /** | ||
| * Verifies data integrity between MongoDB and PostgreSQL | ||
| * @param {string} mongoUrl - MongoDB connection URL | ||
| * @param {string} pgUrl - PostgreSQL connection URL | ||
| * @returns {Promise<void>} | ||
| */ | ||
| // usage node verify-migration.mjs --mongodb-url="mongodb://localhost:27017/dbname" --postgres-url="postgresql://user:password@localhost:5432/dbname" | ||
| async function verifyMigration(mongoUrl, pgUrl) { | ||
| const mongoClient = new MongoClient(mongoUrl); | ||
| const pgClient = new pg.Client(pgUrl); | ||
| const BATCH_SIZE = 1000; // Process 1000 documents at a time | ||
|
|
||
| try { | ||
| await mongoClient.connect(); | ||
| await pgClient.connect(); | ||
|
|
||
| const mongoDb = mongoClient.db(); | ||
| const collections = await mongoDb | ||
| .listCollections({}, { nameOnly: true }) | ||
| .toArray(); | ||
|
|
||
| let hasDiscrepancy = false; | ||
| const verificationResults = []; | ||
|
|
||
| for (const collection of collections) { | ||
| const collectionName = collection.name; | ||
|
|
||
| if (collectionName.startsWith('mongock')) { | ||
| continue; | ||
| } | ||
|
|
||
| console.log(`\nVerifying collection: ${collectionName}`); | ||
| const pgTableName = collectionName.toLowerCase(); | ||
|
|
||
| // Get total count for progress tracking | ||
| const totalCount = await mongoDb | ||
| .collection(collectionName) | ||
| .countDocuments({ deleted: { $ne: true }, deletedAt: null }); | ||
|
|
||
| let processedCount = 0; | ||
| const missingInPostgres = []; | ||
| const fieldDiscrepancies = []; | ||
|
|
||
| // Process in batches | ||
| while (processedCount < totalCount) { | ||
| const mongoDocs = await mongoDb | ||
| .collection(collectionName) | ||
| .find({ deleted: { $ne: true }, deletedAt: null }) | ||
| .skip(processedCount) | ||
| .limit(BATCH_SIZE) | ||
| .toArray(); | ||
|
|
||
| for (const mongoDoc of mongoDocs) { | ||
| transformFields(mongoDoc); | ||
|
|
||
| // Get full PostgreSQL record | ||
| const pgRecord = await pgClient.query( | ||
| `SELECT * FROM ${pgTableName} WHERE id = $1 AND "deletedAt" IS NULL`, | ||
| [mongoDoc.id] | ||
| ); | ||
|
|
||
| if (pgRecord.rows.length === 0) { | ||
| missingInPostgres.push(mongoDoc.id); | ||
| hasDiscrepancy = true; | ||
| continue; | ||
| } | ||
|
|
||
| // Compare all fields | ||
| const pgDoc = pgRecord.rows[0]; | ||
| const differences = compareDocuments(mongoDoc, pgDoc); | ||
|
|
||
| if (differences.length > 0) { | ||
| fieldDiscrepancies.push({ | ||
| id: mongoDoc.id, | ||
| differences | ||
| }); | ||
| hasDiscrepancy = true; | ||
| } | ||
| } | ||
|
|
||
| processedCount += mongoDocs.length; | ||
| console.log(`Progress: ${processedCount}/${totalCount} documents`); | ||
| } | ||
|
|
||
| // Get PostgreSQL documents not in MongoDB | ||
| const pgDocs = await pgClient.query( | ||
| `SELECT id FROM ${pgTableName} WHERE "deletedAt" IS NULL` | ||
| ); | ||
|
Comment on lines
+94
to
+96
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve PostgreSQL query efficiency Retrieving all rows at once may cause performance issues with large tables. Use pagination or streaming to handle large datasets without overwhelming memory resources. |
||
|
|
||
| const pgIds = new Set(pgDocs.rows.map(row => row.id)); | ||
| const mongoIds = new Set(mongoDocs.map(doc => doc.id)); | ||
|
|
||
| const missingInMongo = [...pgIds].filter(id => !mongoIds.has(id)); | ||
|
|
||
| if (missingInMongo.length > 0 || missingInPostgres.length > 0 || fieldDiscrepancies.length > 0) { | ||
| verificationResults.push({ | ||
| collectionName, | ||
| mongoCount: totalCount, | ||
| pgCount: pgIds.size, | ||
| missingInPostgres, | ||
| missingInMongo, | ||
| fieldDiscrepancies | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // Print verification summary | ||
| if (verificationResults.length > 0) { | ||
| console.error('\n❌ Data verification failed: Discrepancies found'); | ||
|
|
||
| verificationResults.forEach(result => { | ||
| console.error(`\nCollection: ${result.collectionName}`); | ||
| console.error(`MongoDB count: ${result.mongoCount}, PostgreSQL count: ${result.pgCount}`); | ||
|
|
||
| if (result.missingInPostgres.length > 0) { | ||
| console.error('\nDocuments missing in PostgreSQL:'); | ||
| result.missingInPostgres.forEach(id => console.error(`- ${id}`)); | ||
| } | ||
|
|
||
| if (result.missingInMongo.length > 0) { | ||
| console.error('\nDocuments missing in MongoDB:'); | ||
| result.missingInMongo.forEach(id => console.error(`- ${id}`)); | ||
| } | ||
|
|
||
| if (result.fieldDiscrepancies.length > 0) { | ||
| console.error('\nDocuments with field discrepancies:'); | ||
| result.fieldDiscrepancies.forEach(({ id, differences }) => { | ||
| console.error(`\nDocument ID: ${id}`); | ||
| differences.forEach(diff => console.error(`- ${diff}`)); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| process.exit(1); | ||
| } else { | ||
| console.log('\n✓ Data verification successful: All records match'); | ||
| } | ||
|
|
||
| } catch (error) { | ||
| console.error('Error during verification:', error); | ||
| process.exit(1); | ||
| } finally { | ||
| await mongoClient.close(); | ||
| await pgClient.end(); | ||
| } | ||
| } | ||
|
|
||
| // Add helper function to compare documents | ||
| function compareDocuments(mongoDoc, pgDoc) { | ||
| const differences = []; | ||
|
|
||
| // Compare each field in MongoDB document | ||
| for (const [key, mongoValue] of Object.entries(mongoDoc)) { | ||
| // Skip internal MongoDB fields | ||
| if (key === '_id') continue; | ||
|
|
||
| const pgValue = pgDoc[key.toLowerCase()]; // PostgreSQL fields are lowercase | ||
|
|
||
| if (!isEquivalent(mongoValue, pgValue)) { | ||
| differences.push(`Field '${key}' mismatch - Mongo: ${mongoValue}, Postgres: ${pgValue}`); | ||
| } | ||
| } | ||
|
|
||
| // Check for extra fields in PostgreSQL | ||
| for (const [key, pgValue] of Object.entries(pgDoc)) { | ||
| const mongoKey = key.toLowerCase(); | ||
| if (!mongoDoc.hasOwnProperty(mongoKey) && pgValue !== null) { | ||
AnaghHegde marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| differences.push(`Extra field in Postgres: '${key}' = ${pgValue}`); | ||
| } | ||
| } | ||
|
|
||
| return differences; | ||
| } | ||
|
|
||
| // Helper function to compare values | ||
| function isEquivalent(value1, value2) { | ||
| if (value1 === value2) return true; | ||
|
|
||
| // Handle null/undefined | ||
| if (!value1 && !value2) return true; | ||
| if (!value1 || !value2) return false; | ||
|
|
||
| // Handle arrays | ||
| if (Array.isArray(value1) && Array.isArray(value2)) { | ||
| return JSON.stringify(value1) === JSON.stringify(value2); | ||
| } | ||
|
|
||
| // Handle objects | ||
| if (typeof value1 === 'object' && typeof value2 === 'object') { | ||
| return JSON.stringify(value1) === JSON.stringify(value2); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| // Parse command line arguments | ||
| const args = process.argv.slice(2); | ||
| let mongoUrl, pgUrl; | ||
|
|
||
| for (const arg of args) { | ||
| if (arg.startsWith('--mongodb-url=')) { | ||
| mongoUrl = arg.split('=')[1]; | ||
| } else if (arg.startsWith('--postgres-url=')) { | ||
| pgUrl = arg.split('=')[1]; | ||
| } | ||
| } | ||
|
|
||
| if (!mongoUrl || !pgUrl) { | ||
| console.error('Usage: node verify-migration.mjs --mongodb-url=<url> --postgres-url=<url>'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| verifyMigration(mongoUrl, pgUrl).catch(console.error); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.