From 8e6f88d29fb46d8bf096cfb23857e61fa4c10abd Mon Sep 17 00:00:00 2001 From: daniel31x13 Date: Thu, 25 Jul 2024 23:43:26 -0400 Subject: [PATCH] merged the two migration scripts for v2.6.1 --- scripts/migration/descriptionToName.js | 70 ------------------- .../index.js} | 59 +++++++++++----- 2 files changed, 41 insertions(+), 88 deletions(-) delete mode 100644 scripts/migration/descriptionToName.js rename scripts/migration/{pdfScreenshotIndexing.js => v2.6.1/index.js} (68%) diff --git a/scripts/migration/descriptionToName.js b/scripts/migration/descriptionToName.js deleted file mode 100644 index 435e59f..0000000 --- a/scripts/migration/descriptionToName.js +++ /dev/null @@ -1,70 +0,0 @@ -// [Optional, but recommended] - -// We decided that the "name" field should be the auto-generated field instead of the "description" field, so we need to -// move the data from the "description" field to the "name" field for links that have an empty name. - -// This script is meant to be run only once. - -// Run the script with `node scripts/migration/descriptionToName.js` - -const { PrismaClient } = require("@prisma/client"); - -const prisma = new PrismaClient(); - -async function main() { - console.log("Starting..."); - - const count = await prisma.link.count({ - where: { - name: "", - description: { - not: "", - }, - }, - }); - - console.log( - `Applying the changes to ${count} ${ - count == 1 ? "link" : "links" - } in 10 seconds...` - ); - - await new Promise((resolve) => setTimeout(resolve, 10000)); - - console.log("Applying the changes..."); - - const links = await prisma.link.findMany({ - where: { - name: "", - description: { - not: "", - }, - }, - select: { - id: true, - description: true, - }, - }); - - for (const link of links) { - await prisma.link.update({ - where: { - id: link.id, - }, - data: { - name: link.description, - description: "", - }, - }); - } - - console.log("Done!"); -} - -main() - .catch((e) => { - throw e; - }) - .finally(async () => { - await prisma.$disconnect(); - }); diff --git a/scripts/migration/pdfScreenshotIndexing.js b/scripts/migration/v2.6.1/index.js similarity index 68% rename from scripts/migration/pdfScreenshotIndexing.js rename to scripts/migration/v2.6.1/index.js index 422085b..0c537e0 100644 --- a/scripts/migration/pdfScreenshotIndexing.js +++ b/scripts/migration/v2.6.1/index.js @@ -1,4 +1,12 @@ -// This is a script that looks for every link and checks if the pdf/screenshot exist in the filesystem. +// Run the script with `node scripts/migration/v2.6.1/index.js` +// Docker users can run the script with `docker exec -it CONTAINER_ID /bin/bash -c 'node scripts/migration/v2.6.1/index.js'` + +// There are two parts to this script: + +// Firstly we decided that the "name" field should be the auto-generated field instead of the "description" field, so we need to +// move the data from the "description" field to the "name" field for links that have an empty name. + +// Secondly it looks for every link and checks if the pdf/screenshot exist in the filesystem. // If they do, it updates the link with the path in the db. // If they don't, it passes. @@ -64,7 +72,34 @@ async function checkFileExistence(path) { } } -async function pdfScreenshotIndexing() { +async function main() { + console.log("Starting... Please do not interrupt the process."); + + const linksWithoutName = await prisma.link.findMany({ + where: { + name: "", + description: { + not: "", + }, + }, + select: { + id: true, + description: true, + }, + }); + + for (const link of linksWithoutName) { + await prisma.link.update({ + where: { + id: link.id, + }, + data: { + name: link.description, + description: "", + }, + }); + } + const links = await prisma.link.findMany({ select: { id: true, @@ -77,8 +112,6 @@ async function pdfScreenshotIndexing() { orderBy: { id: "asc" }, }); - let counter = 0; - // PDFs for (let link of links) { const path = `archives/${link.collectionId}/${link.id}.pdf`; @@ -92,13 +125,9 @@ async function pdfScreenshotIndexing() { }); } - console.log("count:", counter, "id:", link.id, "PDF"); - - counter++; + console.log("Indexing the PDF for link:", link.id); } - counter = 0; - // Screenshots (PNGs) for (let link of links) { const path = `archives/${link.collectionId}/${link.id}.png`; @@ -112,13 +141,9 @@ async function pdfScreenshotIndexing() { }); } - console.log("count:", counter, "id:", link.id, "PNG"); - - counter++; + console.log("Indexing the PNG for link:", link.id); } - counter = 0; - // Screenshots (JPEGs) for (let link of links) { const path = `archives/${link.collectionId}/${link.id}.jpeg`; @@ -132,15 +157,13 @@ async function pdfScreenshotIndexing() { }); } - console.log("count:", counter, "id:", link.id, "JPEG"); - - counter++; + console.log("Indexing the JPEG for link:", link.id); } await prisma.$disconnect(); } -pdfScreenshotIndexing().catch((e) => { +main().catch((e) => { console.error(e); process.exit(1); });