Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

Commit

Permalink
chore: 移行用スクリプトの改善
Browse files Browse the repository at this point in the history
  • Loading branch information
yupix committed Sep 26, 2023
1 parent e4c80b7 commit 588fcbe
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
26 changes: 17 additions & 9 deletions src/migration/drive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import { createUser } from "./user";
import { LRUCache } from "lru-cache";

const driveFileCache = new LRUCache<string, v13DriveFile>({
max: 1000
})
max: 1000,
});

export async function migrateDriveFile(
driveFileId: string,
useFile?: DriveFile,
useFile?: DriveFile
) {
const cacheKey = `migrateDriveFile${driveFileId}`
const cacheResult = driveFileCache.get(cacheKey)
const cacheKey = `migrateDriveFile${driveFileId}`;
const cacheResult = driveFileCache.get(cacheKey);
if (cacheResult) return cacheResult;

const originalDb = getConnection();
Expand All @@ -46,8 +46,9 @@ export async function migrateDriveFile(
file = result;
}

if (file.folderId) await migrateDriveFolder(originalDb, nextDb, file.folderId);
if (file.userId) await createUser({userId: file.userId}) // ユーザーその物しか作成しない。migrateUsersであとでその他の情報は作成される
if (file.folderId)
await migrateDriveFolder(originalDb, nextDb, file.folderId);
if (file.userId) await createUser({ userId: file.userId }); // ユーザーその物しか作成しない。migrateUsersであとでその他の情報は作成される

const createdDriveFile = await driveFileRepository.save({
id: file.id,
Expand Down Expand Up @@ -75,7 +76,7 @@ export async function migrateDriveFile(
isLink: file.isLink,
});
driveFileCache.set(cacheKey, createdDriveFile);
return createdDriveFile
return createdDriveFile;
}

export async function migrateDriveFiles(
Expand All @@ -86,9 +87,16 @@ export async function migrateDriveFiles(
const pagination = createPagination(originalDb, DriveFile, {
where: { userId },
});
const driveFileRepository = nextDb.getRepository(v13DriveFile);
while (true) {
const files = await pagination.next();
for (const file of files) {
const checkExists = await driveFileRepository.findOne({
where: { id: file.id },
});
if (checkExists) {
continue;
}
driveFileQueue.add({ driveFileId: file.id, useFile: file });
}
if (files.length === 0) break; // 100以下になったら止める
Expand All @@ -105,7 +113,7 @@ export async function migrateDriveFolder(
const driveFolderRepository = nextDb.getRepository(v13DriveFolder);

async function save(folder: DriveFolder) {
if (folder.userId) await createUser({userId: folder.userId})
if (folder.userId) await createUser({ userId: folder.userId });
return await driveFolderRepository.save({
createdAt: folder.createdAt,
id: folder.id,
Expand Down
2 changes: 1 addition & 1 deletion src/migration/hashtag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export async function migrateHashtags(originalDb: Connection, nextDb: Connection
count++
const checkExists = await hashtagRepository.findOne({where: {id: hashtag.id}});
if (checkExists) {
logger.info(`Hashtag: ${hashtag.id} は移行済みです ${count}`)
// logger.info(`Hashtag: ${hashtag.id} は移行済みです ${count}`)
continue
}
hashtagQueue.add({hashtagId: hashtag.id, useHashtag: hashtag})
Expand Down
9 changes: 6 additions & 3 deletions src/migration/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export async function migrateNote(noteId: string, useNote?: Note) {
await migrateNoteReactions(originalDb, nextDb, note.id);
await migrateNoteUnreads(originalDb, nextDb, note.id);
await migrateNoteFavorites(note.id);
if (await noteQueue.getCompletedCount() > 1000) {
await noteQueue.clean(0, 'completed')
}
logger.succ(`Note: ${note.id} の移行が完了しました`);
}

Expand Down Expand Up @@ -91,8 +94,8 @@ export async function migrateNote(noteId: string, useNote?: Note) {
}


const checkExists = await noteRepository.findOne(note.id); // 既にノートが移行済みか確認
if (checkExists) return; // 移行済みならスキップする
// const checkExists = await noteRepository.findOne(note.id); // 既にノートが移行済みか確認
// if (checkExists) return; // 移行済みならスキップする

if (note.replyId) await checkReply(note.replyId); // リプライが既に登録されてるか確認し、無いなら再帰的に作成する
if (note.renoteId) await checkRenoteId(note.renoteId); // renoteが既に登録されてるか確認し、無いなら再帰的に作成する
Expand All @@ -113,7 +116,7 @@ export async function migrateNotes(
for (const note of notes) {
const checkExists = await noteRepository.findOne({where: {id: note.id}})
if (checkExists) {
logger.info(`Note: ${note.id} は移行済みです`)
// logger.info(`Note: ${note.id} は移行済みです`)
continue
}
noteQueue.add({ noteId: note.id, note });
Expand Down
2 changes: 1 addition & 1 deletion src/migration/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function createUser(options: {userId: string, useUser?: User}) {

if (checkExists) {
resultUser = checkExists;
logger.info(`User: ${options.userId} は既に移行済みです`);
// logger.info(`User: ${options.userId} は既に移行済みです`);
} else {
resultUser = await userRepository.save({
id: user.id,
Expand Down

0 comments on commit 588fcbe

Please sign in to comment.