86 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-09-15 15:19:33 +03:00
import { PrismaClient } from "./client";
import { z } from "zod";
const PageSchema = z.object({
id: z.string(),
name: z.string(),
path: z.string(),
title: z.string(),
meta: z.record(z.string(), z.string()),
treeId: z.string(),
});
const PagesSchema = z.object({
homePage: PageSchema,
pages: z.array(PageSchema),
});
export default () => {
const client = new PrismaClient({
// Uncomment to see the queries in console as the migration runs
2022-09-15 15:59:53 +03:00
// log: ["query", "info", "warn", "error"],
2022-09-15 15:19:33 +03:00
});
2022-09-15 17:56:38 +03:00
return client.$transaction(
async (prisma) => {
const builds = await prisma.build.findMany();
const breakpoints = await prisma.breakpoints.findMany();
2022-09-15 15:19:33 +03:00
2022-09-15 17:56:38 +03:00
const buildsParsed = builds.map((build) => ({
id: build.id,
pages: PagesSchema.parse(JSON.parse(build.pages)),
}));
2022-09-15 15:19:33 +03:00
2022-10-19 22:55:33 +04:00
// await Promise.all(
// breakpoints.map((breakpoint) => {
// const build = buildsParsed.find(
// (build) => build.pages.homePage.treeId === breakpoint.treeId
// );
// if (build === undefined) {
//
2022-10-19 22:55:33 +04:00
// console.warn(
// `Build not found for breakpoint ${breakpoint.treeId}. Deleting!`
// );
2022-09-15 15:19:33 +03:00
2022-10-19 22:55:33 +04:00
// return prisma.breakpoints.delete({
// where: { treeId: breakpoint.treeId },
// });
// }
2022-10-19 21:56:14 +04:00
2022-10-19 22:55:33 +04:00
// return prisma.breakpoints.update({
// where: { treeId: breakpoint.treeId },
// data: {
// buildId: build.id,
// },
// });
// })
// );
2022-09-15 17:56:38 +03:00
2022-10-19 22:55:33 +04:00
for (const breakpoint of breakpoints) {
const build = buildsParsed.find(
(build) => build.pages.homePage.treeId === breakpoint.treeId
);
if (build === undefined) {
console.warn(
`Build not found for breakpoint ${breakpoint.treeId}. Deleting!`
);
await prisma.breakpoints.delete({
where: { treeId: breakpoint.treeId },
});
} else {
console.info(`Updating breakpoint ${breakpoint.treeId}`);
2022-10-19 22:55:33 +04:00
await prisma.breakpoints.update({
2022-09-15 17:56:38 +03:00
where: { treeId: breakpoint.treeId },
data: {
buildId: build.id,
},
});
2022-10-19 22:55:33 +04:00
}
}
2022-09-15 17:56:38 +03:00
},
2022-10-19 22:55:33 +04:00
{ timeout: 1000 * 60 * 15 }
2022-09-15 17:56:38 +03:00
);
2022-09-15 15:19:33 +03:00
};