###### tags: `ELK-ROOM` # Forum item data ## sql ```sql= CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; /* copyPostsToFourmItems */ INSERT INTO "ForumItem" ( "uuid", "createAt", "updatedAt", "type", "userId", "gameId", "lang", "country", "postId" ) SELECT uuid_generate_v4(), "createAt", "updatedAt", 'POST', "userId", "gameId", "lang", "country", "id" FROM "Post" WHERE NOT EXISTS ( SELECT 1 FROM "ForumItem" WHERE "postId" = "Post"."id" AND "type" = 'POST' ); /* copyVideosToForumItems */ INSERT INTO "ForumItem" ( "uuid", "createAt", "updatedAt", "type", "userId", "gameId", "lang", "country", "ytVideoId" ) SELECT uuid_generate_v4(), "YtVideo"."createAt", "YtVideo"."updatedAt", 'YTVIDEO', "YtChannel"."userId", "YtVideo"."taggedGameId", "YtVideo"."lang", "YtVideo"."country", "YtVideo"."id" FROM "YtVideo" LEFT JOIN "YtChannel" ON "YtVideo"."ytChannelId" = "YtChannel"."id" WHERE NOT EXISTS ( SELECT 1 FROM "ForumItem" WHERE "ytVideoId" = "YtVideo"."id" AND "type" = 'YTVIDEO' ); /* connectForumItemIdToComments */ UPDATE "Comment" SET "forumItemId" = "ForumItem"."id", "targetId" = "ForumItem"."id", "targetType" = 'FORUM_ITEM' FROM "ForumItem" WHERE "Comment"."postId" = "ForumItem"."postId" AND "ForumItem"."id" IS NOT NULL; /* connectForumItemIdToLikes */ UPDATE "Like" SET "forumItemId" = "ForumItem"."id" FROM "ForumItem" WHERE "Like"."postId" = "ForumItem"."postId" AND "ForumItem"."id" IS NOT NULL; /* connectForumItemIdToNotifications */ UPDATE "NotificationEntity" SET "forumItemId" = "ForumItem"."id" FROM "ForumItem" WHERE "NotificationEntity"."postId" = "ForumItem"."postId" AND "ForumItem"."id" IS NOT NULL; ``` ## script ```typescript= await ForumService.copyPostsToFourmItems() await ForumService.copyVideosToForumItems() await ForumService.connectForumItemIdToComments() await ForumService.connectForumItemIdToLikes() await ForumService.connectForumItemIdToNotifications() ``` ```typescript= static async copyPostsToFourmItems() { const arr: Prisma.Prisma__ForumItemClient<ForumItem>[] = [] const posts = await prisma.post.findMany() posts.forEach((post) => arr.push( prisma.forumItem.create({ data: { createAt: post.createAt, updatedAt: post.updatedAt, type: ForumItemType.POST, userId: post.userId, gameId: post.gameId, lang: post.lang, country: post.country, postId: post.id, }, }), )) const result = await prisma.$transaction(arr) console.log('--->', result.length) } static async copyVideosToForumItems() { const arr: Prisma.Prisma__ForumItemClient<ForumItem>[] = [] const videos = await prisma.ytVideo.findMany({ include: { ytChannel: { select: { user: { select: { id: true } } } } }, }) videos.forEach((video) => arr.push( prisma.forumItem.create({ data: { createAt: video.createAt, updatedAt: video.updatedAt, type: ForumItemType.YTVIDEO, userId: video.ytChannel.user.id, gameId: video.taggedGameId, lang: video.lang, country: video.country, ytVideoId: video.id, }, }), )) const result = await prisma.$transaction(arr) console.log('--->', result.length) } static async connectForumItemIdToComments() { const arr: Prisma.Prisma__CommentClient<Comment>[] = [] const comments = await prisma.comment.findMany() comments.forEach((comment) => arr.push( prisma.comment.update({ where: { id: comment.id }, data: { forumItem: { connect: comment.postId ? { postId: comment.postId, } : undefined, }, }, }), )) const result = await prisma.$transaction(arr) console.log('--->', result.length) } static async connectForumItemIdToLikes() { const arr: Prisma.Prisma__LikeClient<Like>[] = [] const likes = await prisma.like.findMany() likes.forEach((like) => arr.push( prisma.like.update({ where: { id: like.id }, data: { forumItem: { connect: like.postId ? { postId: like.postId, } : undefined, }, }, }), )) const result = await prisma.$transaction(arr) console.log('--->', result.length) } static async connectForumItemIdToNotifications() { const arr: Prisma.Prisma__NotificationEntityClient<NotificationEntity>[] = [] const notifications = await prisma.notificationEntity.findMany({ where: { NOT: { postId: null } }, }) notifications.forEach((notification) => arr.push( prisma.notificationEntity.update({ where: { id: notification.id }, data: { forumItem: { connect: notification.postId ? { postId: notification.postId, } : undefined, }, }, }), )) const result = await prisma.$transaction(arr) console.log('--->', result.length) } ```