Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- migrate-data.js +70 -0
migrate-data.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env node
|
| 2 |
+
// One-time migration: copy SourceText and Slide from source cluster to target (APG5604)
|
| 3 |
+
|
| 4 |
+
const mongoose = require('mongoose');
|
| 5 |
+
const SourceText = require('./models/SourceText');
|
| 6 |
+
const Slide = require('./models/Slide');
|
| 7 |
+
|
| 8 |
+
const SOURCE_URI = process.env.SOURCE_MONGODB_URI || 'mongodb+srv://nothingyu:wSg3lbO1PkHiRMq9@sandbox.ecysggv.mongodb.net/test?retryWrites=true&w=majority&appName=sandbox';
|
| 9 |
+
const TARGET_URI = process.env.MONGODB_URI || process.env.TARGET_MONGODB_URI || 'mongodb+srv://nothingyu_db_user:zjxVXtaIfUVBesdt@apg5604.tdavbvz.mongodb.net/?retryWrites=true&w=majority&appName=APG5604';
|
| 10 |
+
|
| 11 |
+
async function fetchAll(conn, modelName) {
|
| 12 |
+
const docs = await conn.connection.db.collection(modelName.toLowerCase() + 's').find({}).toArray();
|
| 13 |
+
return docs;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
async function upsertAll(conn, collectionName, docs) {
|
| 17 |
+
if (!Array.isArray(docs) || docs.length === 0) return { inserted: 0, updated: 0 };
|
| 18 |
+
const col = conn.connection.db.collection(collectionName);
|
| 19 |
+
let inserted = 0; let updated = 0;
|
| 20 |
+
for (const doc of docs) {
|
| 21 |
+
const _id = doc._id;
|
| 22 |
+
// Remove _id to allow upsert by _id
|
| 23 |
+
await col.updateOne({ _id }, { $set: { ...doc } }, { upsert: true });
|
| 24 |
+
}
|
| 25 |
+
// Rough counts
|
| 26 |
+
inserted = docs.length;
|
| 27 |
+
return { inserted, updated };
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
async function main() {
|
| 31 |
+
console.log('Starting migration...');
|
| 32 |
+
console.log('Source:', SOURCE_URI.replace(/:[^@]+@/, ':***@'));
|
| 33 |
+
console.log('Target:', TARGET_URI.replace(/:[^@]+@/, ':***@'));
|
| 34 |
+
|
| 35 |
+
const source = await mongoose.createConnection(SOURCE_URI, { maxPoolSize: 5 }).asPromise();
|
| 36 |
+
const target = await mongoose.createConnection(TARGET_URI, { maxPoolSize: 5 }).asPromise();
|
| 37 |
+
try {
|
| 38 |
+
// Slides
|
| 39 |
+
const slideDocs = await source.db.collection('slides').find({}).toArray();
|
| 40 |
+
console.log('Slides to migrate:', slideDocs.length);
|
| 41 |
+
const slideResult = await upsertAll({ connection: { db: target.db } }, 'slides', slideDocs);
|
| 42 |
+
console.log('Slides migrated:', slideResult.inserted);
|
| 43 |
+
|
| 44 |
+
// SourceText (tutorial + weekly-practice)
|
| 45 |
+
const stDocs = await source.db.collection('sourcetexts').find({ category: { $in: ['tutorial', 'weekly-practice'] } }).toArray();
|
| 46 |
+
console.log('SourceTexts to migrate:', stDocs.length);
|
| 47 |
+
const stResult = await upsertAll({ connection: { db: target.db } }, 'sourcetexts', stDocs);
|
| 48 |
+
console.log('SourceTexts migrated:', stResult.inserted);
|
| 49 |
+
|
| 50 |
+
// Optional: Week briefs if used
|
| 51 |
+
const briefsExists = await source.db.listCollections({ name: 'weekbriefs' }).hasNext();
|
| 52 |
+
if (briefsExists) {
|
| 53 |
+
const wbDocs = await source.db.collection('weekbriefs').find({}).toArray();
|
| 54 |
+
console.log('WeekBriefs to migrate:', wbDocs.length);
|
| 55 |
+
await upsertAll({ connection: { db: target.db } }, 'weekbriefs', wbDocs);
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
console.log('Migration completed successfully.');
|
| 59 |
+
} catch (e) {
|
| 60 |
+
console.error('Migration failed:', e);
|
| 61 |
+
process.exitCode = 1;
|
| 62 |
+
} finally {
|
| 63 |
+
await source.close();
|
| 64 |
+
await target.close();
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
main();
|
| 69 |
+
|
| 70 |
+
|