Last active
April 18, 2024 01:55
-
-
Save cffnpwr/8f3c07b711b55f45abe7ddeac4f233ab to your computer and use it in GitHub Desktop.
This file contains 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
import { loadConfig } from "./built/config.js"; | |
import { MiUser } from "./built/models/_.js"; | |
import { appendQuery, query } from "./built/misc/prelude/url.js"; | |
import dataSource from "./ormconfig.js"; | |
const concurrentPromise = async (concurrency, promises) => { | |
let index = 0; | |
console.log(`Processing ${promises.length} promises...`); 1; | |
while (true) { | |
const chunks = promises.slice(index, index + concurrency); | |
if (chunks.length === 0) { | |
break; | |
} | |
console.log(`Processing ${index} to ${index + concurrency}...`); | |
await Promise.all(chunks); | |
index += concurrency; | |
console.log(`Processed ${index} promises...`); | |
} | |
console.log("Done processing all promises"); | |
}; | |
// 一度に処理する数 | |
// エラーを吐いたり、heap out of memory になる場合はこの数を減らす | |
const concurrency = 500; | |
const config = loadConfig(); | |
const ds = await dataSource.initialize(); | |
console.log("Migrating users..."); | |
const users = await ds.manager.find(MiUser, { | |
relations: ["avatar", "banner"], | |
}); | |
console.log(`Found ${users.length} users`); | |
console.log("--------------------------------------------------"); | |
console.log("Migrating users..."); | |
const promises = users.map(async (user) => { | |
console.log(`Migrating user ${user.id}...`); | |
const avatar = user.avatar; | |
if (avatar) { | |
user.avatarUrl = appendQuery( | |
`${config.mediaProxy}/avatar.webp`, | |
query({ | |
url: avatar.url, | |
avatar: 1, | |
}), | |
); | |
user.avatarBlurhash = avatar.blurhash; | |
} | |
const banner = user.banner; | |
if (banner) { | |
user.bannerUrl = banner.url; | |
user.bannerBlurhash = banner.blurhash; | |
} | |
await ds.manager.update(MiUser, user.id, user); | |
console.log(`Migrated user ${user.id}`); | |
}); | |
await concurrentPromise(concurrency, promises); | |
console.log("Done migrating users"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment