stock-bot/run-price-migration.js
2025-07-09 23:49:08 -04:00

78 lines
No EOL
2.2 KiB
JavaScript

// This script performs the migration of lastPriceUpdate and lastPriceDate fields
// to the operations tracking system
// Since authentication is required, you'll need to run this script with proper MongoDB credentials
// Update the connection string below with your authentication details
const MONGODB_URI = 'mongodb://username:password@localhost:27017/stock?authSource=admin';
// Or use environment variable: process.env.MONGODB_URI
async function runMigration() {
console.log(`
========================================
PRICE TRACKING MIGRATION SCRIPT
========================================
This script will migrate lastPriceUpdate and lastPriceDate fields
from eodSymbols collection to the operations tracking system.
To run this migration:
1. Update the MONGODB_URI in this file with your MongoDB credentials
OR set the MONGODB_URI environment variable
2. Run the script:
node run-price-migration.js
The script will:
- Find all documents with lastPriceUpdate field
- Convert dates to normalized format (00:00:00 UTC)
- Create operations.price_update structure
- Process in batches of 1000 documents
Total documents to migrate: ~199,175
Migration query that will be executed:
`);
console.log(`
db.eodSymbols.find({
lastPriceUpdate: { $exists: true },
'operations.price_update': { $exists: false }
}).forEach(doc => {
const normalizedDate = new Date(doc.lastPriceUpdate);
normalizedDate.setUTCHours(0, 0, 0, 0);
let lastRecordDate = null;
if (doc.lastPriceDate) {
try {
lastRecordDate = new Date(doc.lastPriceDate);
} catch (e) {}
}
db.eodSymbols.updateOne(
{ _id: doc._id },
{
$set: {
'operations.price_update': {
lastRunAt: normalizedDate,
lastSuccessAt: normalizedDate,
status: 'success',
...(lastRecordDate && { lastRecordDate })
}
}
}
);
});
`);
console.log(`
After migration, you can optionally remove old fields:
db.eodSymbols.updateMany({}, { $unset: { lastPriceUpdate: '', lastPriceDate: '' } })
To verify migration:
db.eodSymbols.findOne({ 'operations.price_update': { $exists: true } })
`);
}
runMigration();