mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-08-30 23:50:37 +02:00
fix: fix migration script
This commit is contained in:
+35
-46
@@ -31,17 +31,14 @@ async function connect(db) {
|
|||||||
const oldDB = await connect("anonymous_github");
|
const oldDB = await connect("anonymous_github");
|
||||||
|
|
||||||
console.log("Import Users");
|
console.log("Import Users");
|
||||||
await new Promise(async (resolve) => {
|
let index = 0;
|
||||||
const promises = [];
|
const userQuery = oldDB.collection("users").find();
|
||||||
await oldDB
|
const totalUser = await userQuery.count();
|
||||||
.collection("users")
|
|
||||||
.find()
|
while (await userQuery.hasNext()) {
|
||||||
.batchSize(1)
|
const r = await userQuery.next();
|
||||||
.forEach(async (r) => {
|
index++;
|
||||||
let localResolve = null;
|
console.log(`Import User [${index}/${totalUser}]: ${r.username}`);
|
||||||
const p = new Promise((r) => (localResolve = r));
|
|
||||||
promises.push(p);
|
|
||||||
console.log("Import User: " + r.username);
|
|
||||||
|
|
||||||
const newRepos = [];
|
const newRepos = [];
|
||||||
const allRepoIds = [];
|
const allRepoIds = [];
|
||||||
@@ -98,28 +95,24 @@ async function connect(db) {
|
|||||||
if (user.emails?.length) user.emails[0].default = true;
|
if (user.emails?.length) user.emails[0].default = true;
|
||||||
|
|
||||||
await user.save();
|
await user.save();
|
||||||
|
}
|
||||||
localResolve(user);
|
|
||||||
});
|
|
||||||
Promise.all(promises).then(resolve);
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log("Import Repositories");
|
console.log("Import Repositories");
|
||||||
let promises = [];
|
const repoQuery = oldDB.collection("repositories").find();
|
||||||
await oldDB
|
const totalRepository = await repoQuery.count();
|
||||||
.collection("repositories")
|
index = 0;
|
||||||
.find({})
|
while (await repoQuery.hasNext()) {
|
||||||
.batchSize(1)
|
const r = await repoQuery.next();
|
||||||
.forEach(async (r) => {
|
if (!r.id) continue;
|
||||||
if (!r.id) return;
|
index++;
|
||||||
let localResolve = null;
|
console.log(
|
||||||
const p = new Promise((r) => (localResolve = r));
|
`Import Repository [${index}/${totalRepository}]: ${r.fullName}`
|
||||||
promises.push(p);
|
);
|
||||||
|
|
||||||
let find = await RepositoryModel.findOne({
|
let find = await RepositoryModel.findOne({
|
||||||
externalId: "gh_" + r.id,
|
externalId: "gh_" + r.id,
|
||||||
});
|
});
|
||||||
// console.log("gh_" + r.id, find != null);
|
|
||||||
if (find == null) {
|
if (find == null) {
|
||||||
find = new RepositoryModel({
|
find = new RepositoryModel({
|
||||||
externalId: "gh_" + r.id,
|
externalId: "gh_" + r.id,
|
||||||
@@ -140,18 +133,19 @@ async function connect(db) {
|
|||||||
find.branches = branches;
|
find.branches = branches;
|
||||||
}
|
}
|
||||||
await find.save();
|
await find.save();
|
||||||
localResolve();
|
}
|
||||||
});
|
|
||||||
await Promise.all(promises);
|
|
||||||
console.log("Import Anonymized Repositories");
|
console.log("Import Anonymized Repositories");
|
||||||
promises = [];
|
const anoQuery = oldDB.collection("anonymized_repositories").find();
|
||||||
await oldDB
|
const totalAno = await anoQuery.count();
|
||||||
.collection("anonymized_repositories")
|
index = 0;
|
||||||
.find({})
|
while (await anoQuery.hasNext()) {
|
||||||
.forEach(async (r) => {
|
const r = await anoQuery.next();
|
||||||
let localResolve = null;
|
|
||||||
const p = new Promise((r) => (localResolve = r));
|
index++;
|
||||||
promises.push(p);
|
console.log(
|
||||||
|
`Import Anonymized Repository [${index}/${totalAno}]: ${r.repoId}`
|
||||||
|
);
|
||||||
|
|
||||||
let repo = await RepositoryModel.findOne({ name: r.fullName });
|
let repo = await RepositoryModel.findOne({ name: r.fullName });
|
||||||
if (repo == null) {
|
if (repo == null) {
|
||||||
@@ -184,9 +178,7 @@ async function connect(db) {
|
|||||||
if (r.originalFiles) {
|
if (r.originalFiles) {
|
||||||
size = recursiveCount(r.originalFiles);
|
size = recursiveCount(r.originalFiles);
|
||||||
}
|
}
|
||||||
const owner = await UserModel.findOne({ username: r.owner }).select(
|
const owner = await UserModel.findOne({ username: r.owner }).select("_id");
|
||||||
"_id"
|
|
||||||
);
|
|
||||||
await new AnonymizedRepositoryModel({
|
await new AnonymizedRepositoryModel({
|
||||||
repoId: r.repoId,
|
repoId: r.repoId,
|
||||||
status: r.status,
|
status: r.status,
|
||||||
@@ -197,8 +189,7 @@ async function connect(db) {
|
|||||||
size,
|
size,
|
||||||
source: {
|
source: {
|
||||||
accessToken: r.token,
|
accessToken: r.token,
|
||||||
type:
|
type: r.options.mode == "download" ? "GitHubDownload" : "GitHubStream",
|
||||||
r.options.mode == "download" ? "GitHubDownload" : "GitHubStream",
|
|
||||||
branch: r.branch,
|
branch: r.branch,
|
||||||
commit: r.commit,
|
commit: r.commit,
|
||||||
repositoryId: repo?.id,
|
repositoryId: repo?.id,
|
||||||
@@ -220,9 +211,7 @@ async function connect(db) {
|
|||||||
pageSource: r.options.pageSource,
|
pageSource: r.options.pageSource,
|
||||||
},
|
},
|
||||||
}).save();
|
}).save();
|
||||||
localResolve();
|
}
|
||||||
});
|
|
||||||
await Promise.all(promises);
|
|
||||||
console.log("Import finished!");
|
console.log("Import finished!");
|
||||||
setTimeout(() => process.exit(), 5000);
|
setTimeout(() => process.exit(), 5000);
|
||||||
})();
|
})();
|
||||||
|
|||||||
Reference in New Issue
Block a user