From 966136dab6a1ab7c27f1532d6a01d481ccf92fe9 Mon Sep 17 00:00:00 2001 From: daniel31x13 Date: Wed, 25 Oct 2023 15:42:36 -0400 Subject: [PATCH] created migration script [WIP] --- .../users/userId/deleteUserById.ts | 2 +- lib/api/migration/migrateToV2.js | 83 +++++++++++++++++++ .../migration.sql | 13 +++ prisma/schema.prisma | 5 +- 4 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 lib/api/migration/migrateToV2.js create mode 100644 prisma/migrations/20231025123038_added_pathname_to_files/migration.sql diff --git a/lib/api/controllers/users/userId/deleteUserById.ts b/lib/api/controllers/users/userId/deleteUserById.ts index 731e877..0641525 100644 --- a/lib/api/controllers/users/userId/deleteUserById.ts +++ b/lib/api/controllers/users/userId/deleteUserById.ts @@ -59,7 +59,7 @@ export default async function deleteUserById( where: { collectionId: collection.id }, }); - // Delete archive folders associated with collections + // Delete archive folders removeFolder({ filePath: `archives/${collection.id}` }); } diff --git a/lib/api/migration/migrateToV2.js b/lib/api/migration/migrateToV2.js new file mode 100644 index 0000000..aeb07fb --- /dev/null +++ b/lib/api/migration/migrateToV2.js @@ -0,0 +1,83 @@ +const { PrismaClient } = require("@prisma/client"); +const axios = require("axios"); +const { existsSync } = require("fs"); + +const prisma = new PrismaClient(); + +const STORAGE_FOLDER = process.env.STORAGE_FOLDER || "data"; + +async function checkFileExistence(path) { + try { + if (existsSync(path)) { + return true; + } else return false; + } catch (err) { + console.log(err); + } +} + +// Avatars +async function migrateAvatars() { + const users = await prisma.user.findMany(); + + for (let user of users) { + const path = STORAGE_FOLDER + `/uploads/avatar/${user.id}.jpg`; + + const res = await checkFileExistence(path); + + if (res) { + // await prisma.user.update({ + // where: { id: user.id }, + // data: { image: "avatar/" + user.id }, + // }); + console.log(`Updated avatar for user ${user.id}`); + } else { + console.log(`No avatar found for user ${user.id}`); + } + } + + const links = await prisma.link.findMany(); + + // Screenshots + for (let link of links) { + const path = + STORAGE_FOLDER + `/archives/${link.collectionId}/${link.id}.pdf`; + + const res = await checkFileExistence(path); + + if (res) { + // await prisma.user.update({ + // where: { id: user.id }, + // data: { image: "avatar/" + user.id }, + // }); + console.log(`Updated capture for link ${link.id}`); + } else { + console.log(`No capture found for link ${link.id}`); + } + } + + // PDFs + for (let link of links) { + const path = + STORAGE_FOLDER + `/archives/${link.collectionId}/${link.id}.png`; + + const res = await checkFileExistence(path); + + if (res) { + // await prisma.user.update({ + // where: { id: user.id }, + // data: { image: "avatar/" + user.id }, + // }); + console.log(`Updated capture for link ${link.id}`); + } else { + console.log(`No capture found for link ${link.id}`); + } + } + + await prisma.$disconnect(); +} + +migrateAvatars().catch((e) => { + console.error(e); + process.exit(1); +}); diff --git a/prisma/migrations/20231025123038_added_pathname_to_files/migration.sql b/prisma/migrations/20231025123038_added_pathname_to_files/migration.sql new file mode 100644 index 0000000..65fa41a --- /dev/null +++ b/prisma/migrations/20231025123038_added_pathname_to_files/migration.sql @@ -0,0 +1,13 @@ +/* + Warnings: + + - You are about to drop the column `image` on the `User` table. All the data in the column will be lost. + +*/ +-- AlterTable +ALTER TABLE "Link" ADD COLUMN "pdfPath" TEXT, +ADD COLUMN "screenshotPath" TEXT; + +-- AlterTable +ALTER TABLE "User" DROP COLUMN "image", +ADD COLUMN "imagePath" TEXT; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index ab67eb6..e417a4f 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -42,7 +42,7 @@ model User { email String? @unique emailVerified DateTime? - image String? + imagePath String? accounts Account[] sessions Session[] @@ -122,6 +122,9 @@ model Link { collectionId Int tags Tag[] + screenshotPath String? + pdfPath String? + createdAt DateTime @default(now()) }