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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
refactor: create production build with sql function (#3843)
Bogdan Chadkinf1fef772bfc6
diff --git a/packages/domain/src/trpc/domain.ts b/packages/domain/src/trpc/domain.tsindex 4bbebbe176..98c9d90cdf 100644--- a/packages/domain/src/trpc/domain.ts+++ b/packages/domain/src/trpc/domain.ts@@ -129,6 +129,7 @@ export const domainRouter = router({return result;} catch (error) {+ console.error(error);return {success: false,error: error instanceof Error ? error.message : "Unknown error",diff --git a/packages/postgrest/src/__generated__/db-types.ts b/packages/postgrest/src/__generated__/db-types.tsindex 313b97c45e..4c7f46b14a 100644--- a/packages/postgrest/src/__generated__/db-types.ts+++ b/packages/postgrest/src/__generated__/db-types.ts@@ -781,6 +781,13 @@ export type Database = {userId: string | null;};};+ create_production_build: {+ Args: {+ project_id: string;+ deployment: string;+ };+ Returns: string;+ };};Enums: {AuthorizationRelation:diff --git a/packages/prisma-client/prisma/migrations/20240731170412_create_production_build/migration.sql b/packages/prisma-client/prisma/migrations/20240731170412_create_production_build/migration.sqlnew file mode 100644index 0000000000..330e1c02d8--- /dev/null+++ b/packages/prisma-client/prisma/migrations/20240731170412_create_production_build/migration.sql@@ -0,0 +1,50 @@+DROP FUNCTION IF EXISTS create_production_build;+CREATE FUNCTION create_production_build(+ project_id text,+ deployment text+) RETURNS text AS $$+DECLARE+ new_build_id text;+BEGIN+ INSERT INTO "Build" (+ version,+ "lastTransactionId",+ pages,+ breakpoints,+ styles,+ "styleSources",+ "styleSourceSelections",+ props,+ "dataSources",+ resources,+ instances,+ "marketplaceProduct",+ "publishStatus",+ "projectId",+ id,+ deployment+ )+ SELECT+ version,+ "lastTransactionId",+ pages,+ breakpoints,+ styles,+ "styleSources",+ "styleSourceSelections",+ props,+ "dataSources",+ resources,+ instances,+ "marketplaceProduct",+ "publishStatus",+ "projectId",+ uuid_generate_v4() as id,+ create_production_build.deployment as deployment+ FROM "Build"+ WHERE "projectId" = project_id AND "Build"."deployment" IS NULL+ RETURNING "id" INTO new_build_id;++ RETURN new_build_id;+END;+$$ LANGUAGE plpgsql;diff --git a/packages/project-build/src/db/build.ts b/packages/project-build/src/db/build.tsindex e619d1f9c6..a27d1300dc 100644--- a/packages/project-build/src/db/build.ts+++ b/packages/project-build/src/db/build.ts@@ -2,6 +2,7 @@import { nanoid } from "nanoid";import { prisma, Prisma } from "@webstudio-is/prisma-client";+import type { Database } from "@webstudio-is/postrest/index.server";import {AuthorizationError,authorizeProject,@@ -22,12 +23,10 @@ import type { Data } from "@webstudio-is/http-client";import type { Build } from "../types";import { parseStyles } from "./styles";import { parseStyleSourceSelections } from "./style-source-selections";-import { parseDeployment, serializeDeployment } from "./deployment";+import { parseDeployment } from "./deployment";import { parsePages, serializePages } from "./pages";import { createDefaultPages } from "../shared/pages-utils";-import type { MarketplaceProduct } from "..";-import { z } from "zod";-import type { Database } from "@webstudio-is/postrest/index.server";+import type { MarketplaceProduct } from "../shared//marketplace";export const parseData = <Type extends { id: string }>(string: string@@ -260,59 +259,6 @@ export const createBuild = async (});};-const zBuildCloneResult = z- .array(z.object({ id: z.string() }))- .length(1)- .transform((result) => result[0]);--const cloneBuild = async (- props: {- fromProjectId: Build["projectId"];- toProjectId: Build["projectId"];- deployment: Deployment | undefined;- },- _context: AppContext,- client: Prisma.TransactionClient-): Promise<{ id: string }> => {- const deployment = props.deployment- ? serializeDeployment(props.deployment)- : null;-- 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 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 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;-};-export const createProductionBuild = async (props: {projectId: Build["projectId"];@@ -329,13 +275,19 @@ export const createProductionBuild = async (throw new AuthorizationError("You don't have access to build this project");}- return await cloneBuild(- {- fromProjectId: props.projectId,- toProjectId: props.projectId,- deployment: props.deployment,- },- context,- prisma- );+ const build = await context.postgrest.client.rpc("create_production_build", {+ project_id: props.projectId,+ deployment: JSON.stringify(props.deployment),+ });+ const buildId = build.data;+ if (build.error) {+ throw build.error;+ }+ if (buildId === null) {+ throw Error(`Project ${props.projectId} not found`);+ }++ return {+ id: build.data,+ };};diff --git a/packages/project-build/src/db/deployment.ts b/packages/project-build/src/db/deployment.tsindex 5f3bed6c71..3402e35fe8 100644--- a/packages/project-build/src/db/deployment.ts+++ b/packages/project-build/src/db/deployment.ts@@ -7,7 +7,3 @@ export const parseDeployment = (deployment: string | null) => {return JSON.parse(deployment) as Deployment;};--export const serializeDeployment = (deployment: Deployment) => {- return JSON.stringify(deployment);-};