Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

fix: Clone timeouts (#3257)

Ivan Starkov52553be0afd7
diff --git a/packages/asset-uploader/src/db/clone.ts b/packages/asset-uploader/src/db/clone.ts
index a1deda9ae6..3e65cc15ee 100644
--- a/packages/asset-uploader/src/db/clone.ts
+++ b/packages/asset-uploader/src/db/clone.ts
@@ -1,4 +1,4 @@
-import { prisma, type Prisma, type Project } from "@webstudio-is/prisma-client";
+import { Prisma, prisma, type Project } from "@webstudio-is/prisma-client";
import {
authorizeProject,
type AppContext,
@@ -9,7 +9,7 @@ export const cloneAssets = async (
{
fromProjectId,
toProjectId,
- dontCheckEditPermission = false,
+ checkPermissions = true,
}: {
fromProjectId: Project["id"];
toProjectId: Project["id"];
@@ -18,23 +18,23 @@ export const cloneAssets = async (
* Don't use unless absolutely have to (e.g. because of transactions)
* and unless it's obvious on the call project that permission is checked
*/
- dontCheckEditPermission?: boolean;
+ checkPermissions?: boolean;
},
context: AppContext,
client: Prisma.TransactionClient = prisma
) => {
- const canView = await authorizeProject.hasProjectPermit(
- { projectId: fromProjectId, permit: "view" },
- context
- );
-
- if (canView === false) {
- throw new AuthorizationError(
- "You don't have access to this project assets"
+ if (checkPermissions) {
+ const canView = await authorizeProject.hasProjectPermit(
+ { projectId: fromProjectId, permit: "view" },
+ context
);
- }
- if (dontCheckEditPermission === false) {
+ if (canView === false) {
+ throw new AuthorizationError(
+ "You don't have access to this project assets"
+ );
+ }
+
const canEdit = await authorizeProject.hasProjectPermit(
{ projectId: toProjectId, permit: "edit" },
context
@@ -47,15 +47,28 @@ export const cloneAssets = async (
}
}
- const assets = await client.asset.findMany({
- where: {
- projectId: fromProjectId,
- file: { status: "UPLOADED" },
- },
- });
-
- await client.asset.createMany({
- // we intentionally keep old ids in order for all references in styles/props to work
- data: assets.map((asset) => ({ ...asset, projectId: toProjectId })),
- });
+ const assetFields = Object.keys(client.asset.fields);
+
+ const assetFieldsFiltered = assetFields.filter(
+ (field) => field !== "projectId"
+ );
+
+ const selectQuery = Prisma.sql`
+ SELECT
+ ${Prisma.raw(
+ assetFieldsFiltered.map((field) => `a."${field}"`).join(", ")
+ )}, ${toProjectId} as "projectId"
+ FROM "Asset" a, "File" f
+ WHERE
+ a.name = f.name AND
+ f.status = 'UPLOADED' AND
+ a."projectId" = ${fromProjectId}`;
+
+ const insertQuery = Prisma.sql`
+ INSERT INTO "Asset"(${Prisma.raw(
+ assetFieldsFiltered.map((field) => `"${field}"`).join(", ")
+ )}, "projectId") ${selectQuery}
+ `;
+
+ await client.$executeRaw(insertQuery);
};
diff --git a/packages/project-build/src/db/build.ts b/packages/project-build/src/db/build.ts
index 383a9d14f4..9e6e1ccffe 100644
--- a/packages/project-build/src/db/build.ts
+++ b/packages/project-build/src/db/build.ts
@@ -31,6 +31,7 @@ import { parseDeployment, serializeDeployment } from "./deployment";
import { parsePages, serializePages } from "./pages";
import { createDefaultPages } from "../shared/pages-utils";
import type { MarketplaceProduct } from "..";
+import { z } from "zod";
export const parseData = <Type extends { id: string }>(
string: string
@@ -226,6 +227,11 @@ export const createBuild = async (
});
};
+const zBuildCloneResult = z
+ .array(z.object({ id: z.string() }))
+ .length(1)
+ .transform((result) => result[0]);
+
export const cloneBuild = async (
props: {
fromProjectId: Build["projectId"];
@@ -234,32 +240,42 @@ export const cloneBuild = async (
},
_context: AppContext,
client: Prisma.TransactionClient
-) => {
- const build = await prisma.build.findFirst({
- where: { projectId: props.fromProjectId, deployment: null },
- });
+): Promise<{ id: string }> => {
+ const deployment = props.deployment
+ ? serializeDeployment(props.deployment)
+ : null;
- if (build === null) {
- throw new Error("Dev build not found");
- }
+ const notCopiedFields = [
+ "id",
+ "createdAt",
+ "updatedAt",
+ "deployment",
+ "projectId",
+ ];
+ const fieldsToCopy = Object.keys(client.build.fields)
+ .filter((field) => notCopiedFields.includes(field) === false)
+ .map((field) => `"${field}"`)
+ .join(", ");
- const data = {
- ...build,
- id: undefined,
- createdAt: undefined,
- updatedAt: undefined,
- deployment: props.deployment
- ? serializeDeployment(props.deployment)
- : undefined,
- projectId: props.toProjectId,
- };
+ const selectQuery = Prisma.sql`
+ SELECT ${Prisma.raw(
+ fieldsToCopy
+ )}, uuid_generate_v4() as id, ${deployment} as deployment, ${
+ props.toProjectId
+ } as "projectId"
+ FROM "Build"
+ WHERE "projectId" = ${props.fromProjectId} AND "deployment" IS NULL`;
- const result = await client.build.create({
- data,
- select: {
- id: true,
- },
- });
+ const insertQuery = Prisma.sql`
+ INSERT INTO "Build"(${Prisma.raw(
+ fieldsToCopy
+ )}, "id", "deployment", "projectId")
+ ${selectQuery}
+ RETURNING "id"`;
+
+ const rawResult = await client.$queryRaw(insertQuery);
+
+ const result = zBuildCloneResult.parse(rawResult);
return result;
};
@@ -280,15 +296,13 @@ export const createProductionBuild = async (
throw new AuthorizationError("You don't have access to build this project");
}
- return await prisma.$transaction(async (client) => {
- return await cloneBuild(
- {
- fromProjectId: props.projectId,
- toProjectId: props.projectId,
- deployment: props.deployment,
- },
- context,
- client
- );
- });
+ return await cloneBuild(
+ {
+ fromProjectId: props.projectId,
+ toProjectId: props.projectId,
+ deployment: props.deployment,
+ },
+ context,
+ prisma
+ );
};
diff --git a/packages/project/src/db/project.ts b/packages/project/src/db/project.ts
index c682f462d0..05efad4405 100644
--- a/packages/project/src/db/project.ts
+++ b/packages/project/src/db/project.ts
@@ -178,9 +178,10 @@ export const clone = async (
// Permission check on newProjectId will fail until this transaction is committed.
// We have to skip it, but it's ok because registerProjectOwner is right above
- dontCheckEditPermission: true,
+ checkPermissions: false,
},
- context
+ context,
+ client
);
const clonedProject = await client.project.create({