mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
测试:从vj4的自动迁移脚本(未完成)
This commit is contained in:
1 parent
cedb427dc9
commit
41125c6929
11 files changed
+185
-24
No files matched your search
@@ -0,0 +1,87 @@
|
||||
/* eslint-disable no-await-in-loop */
|
||||
const Mongo = global.Hydro.nodeModules.mongodb;
|
||||
const { ObjectID } = global.Hydro.nodeModules.bson;
|
||||
const dst = global.Hydro.service.db;
|
||||
|
||||
function parseSolution(doc) {
|
||||
return {
|
||||
_id: doc._id,
|
||||
title: doc.title,
|
||||
content: doc.content,
|
||||
vote: doc.vote,
|
||||
};
|
||||
}
|
||||
|
||||
async function problem(src, report) {
|
||||
const count = await src.collection('document').find({ doc_type: 10 }).count();
|
||||
report({ progress: 1, message: `Found ${count} problems.` });
|
||||
for (let i = 0; i <= Math.floor(count / 50); i++) {
|
||||
const docs = await src.collection('document').find({ doc_type: 10 })
|
||||
.skip(i * 50).limit(50)
|
||||
.toArray();
|
||||
const problems = [];
|
||||
for (const doc of docs) {
|
||||
problems.push({
|
||||
_id: doc._id,
|
||||
pid: doc.doc_id || new ObjectID(),
|
||||
title: doc.title,
|
||||
content: doc.content,
|
||||
owner: doc.owner_uid,
|
||||
data: doc.data,
|
||||
category: doc.category,
|
||||
tag: doc.tag,
|
||||
nSubmit: doc.num_submit,
|
||||
nAccept: doc.num_accept,
|
||||
});
|
||||
}
|
||||
await dst.collection('problem').insertMany(problems);
|
||||
}
|
||||
}
|
||||
|
||||
async function contest(src, report) {
|
||||
const RULES = ['', 'oi', 'acm'];
|
||||
const count = await src.collection('document').find({ doc_type: 30 }).count();
|
||||
report({ progress: 1, message: `Found ${count} contests.` });
|
||||
for (let i = 0; i <= Math.floor(count / 50); i++) {
|
||||
const docs = await src.collection('document').find({ doc_type: 30 })
|
||||
.skip(i * 50).limit(50)
|
||||
.toArray();
|
||||
const contests = [];
|
||||
for (const doc of docs) {
|
||||
contests.push({
|
||||
_id: doc._id,
|
||||
title: doc.title,
|
||||
content: doc.content,
|
||||
owner: doc.owner_uid,
|
||||
rule: RULES[doc.rule],
|
||||
beginAt: doc.begin_at,
|
||||
endAt: doc.end_at,
|
||||
pids: doc.pids,
|
||||
attend: doc.attend,
|
||||
});
|
||||
}
|
||||
await dst.collection('contests').insertMany(contests);
|
||||
}
|
||||
}
|
||||
|
||||
async function migrateVijos({
|
||||
host, port, name, username, password,
|
||||
}, report = () => { }) {
|
||||
let mongourl = 'mongodb://';
|
||||
if (username) mongourl += `${username}:${password}@`;
|
||||
mongourl += `${host}:${port}/${name}`;
|
||||
const Database = await Mongo.MongoClient.connect(mongourl, {
|
||||
useNewUrlParser: true, useUnifiedTopology: true,
|
||||
});
|
||||
const src = Database.db(name);
|
||||
report({ progress: 0, message: 'Database connected.' });
|
||||
dst.collection('system').insertOne({
|
||||
_id: 'user',
|
||||
value: (await src.collection('system').findOne({ _id: 'user_counter' })).value,
|
||||
});
|
||||
report({ progress: 1, message: 'Collection:system done.' });
|
||||
await problem(src, report);
|
||||
await contest(src, report);
|
||||
}
|
||||
|
||||
global.Hydro.script.migrateVijos = module.exports = migrateVijos;
|
||||
Reference in new issue
Block a user