created migration script [WIP]
This commit is contained in:
parent
2be2a83c62
commit
966136dab6
|
@ -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}` });
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
});
|
|
@ -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;
|
|
@ -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())
|
||||
}
|
||||
|
||||
|
|
Ŝarĝante…
Reference in New Issue