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
Migrate styles from Tree to Build (#904)
Bogdan Chadkin59e164fb8137
diff --git a/packages/prisma-client/prisma/migrations/20230130160827_build_styles/migration.ts b/packages/prisma-client/prisma/migrations/20230130160827_build_styles/migration.tsnew file mode 100644index 0000000000..f4ee4b213a--- /dev/null+++ b/packages/prisma-client/prisma/migrations/20230130160827_build_styles/migration.ts@@ -0,0 +1,160 @@+import ObjectId from "bson-objectid";+import { PrismaClient } from "./client";++type InstanceId = string;+type StyleSourceId = string;+type BuildId = string;+type TreeId = string;++type TreeStyleDecl = {+ breakpointId: string;+ instanceId: InstanceId;+ property: string;+ value: unknown;+};++type TreeStyles = TreeStyleDecl[];++type StyleDecl = {+ styleSourceId: StyleSourceId;+ breakpointId: string;+ property: string;+ value: unknown;+};++type Styles = StyleDecl[];++type StyleSource = {+ type: "local";+ id: StyleSourceId;+ treeId: TreeId;+};++type StyleSources = StyleSource[];++type StyleSourceSelection = {+ instanceId: InstanceId;+ values: StyleSourceId[];+};++type StyleSourceSelections = StyleSourceSelection[];++export default async () => {+ const client = new PrismaClient({+ // Uncomment to see the queries in console as the migration runs+ // log: ["query", "info", "warn", "error"],+ });++ await client.$transaction(+ async (prisma) => {+ let stylesPerBuild = new Map<BuildId, Styles>();+ let styleSourcesPerBuild = new Map<BuildId, StyleSources>();++ const chunkSize = 1000;+ let cursor: undefined | string = undefined;+ let hasNext = true;+ while (hasNext) {+ const trees = await prisma.tree.findMany({+ take: chunkSize,+ orderBy: {+ id: "asc",+ },+ ...(cursor+ ? {+ skip: 1, // Skip the cursor+ cursor: { id: cursor },+ }+ : null),+ });+ cursor = trees.at(-1)?.id;+ hasNext = trees.length === chunkSize;++ for (const tree of trees) {+ const treeId = tree.id;+ try {+ const treeStyles: TreeStyles = JSON.parse(tree.styles);+ const styles: Styles = [];+ const styleSources: StyleSources = [];+ const styleSelections: StyleSourceSelections = [];+ const styleSourceIdPerInstanceId = new Map<+ InstanceId,+ StyleSourceId+ >();++ for (const styleDecl of treeStyles) {+ const { instanceId, breakpointId, property, value } = styleDecl;+ let styleSourceId = styleSourceIdPerInstanceId.get(instanceId);+ if (styleSourceId === undefined) {+ styleSourceId = ObjectId().toString();+ styleSourceIdPerInstanceId.set(instanceId, styleSourceId);+ }++ styles.push({+ styleSourceId,+ breakpointId,+ property,+ value,+ });+ styleSources.push({+ id: styleSourceId,+ treeId,+ type: "local",+ });+ styleSelections.push({+ instanceId,+ values: [styleSourceId],+ });+ }++ stylesPerBuild.set(tree.buildId, styles);+ styleSourcesPerBuild.set(tree.buildId, styleSources);+ tree.styleSelections = JSON.stringify(styleSelections);+ } catch {+ console.info(`Tree ${treeId} cannot be converted`);+ }+ }+ await Promise.all(+ trees.map(({ id, styleSelections }) =>+ prisma.tree.update({ where: { id }, data: { styleSelections } })+ )+ );+ }++ cursor = undefined;+ hasNext = true;+ while (hasNext) {+ const builds = await prisma.build.findMany({+ take: chunkSize,+ orderBy: {+ id: "asc",+ },+ ...(cursor+ ? {+ skip: 1, // Skip the cursor+ cursor: { id: cursor },+ }+ : null),+ });+ cursor = builds.at(-1)?.id;+ hasNext = builds.length === chunkSize;++ for (const build of builds) {+ const buildId = build.id;+ const styles = stylesPerBuild.get(buildId) ?? [];+ const styleSources = styleSourcesPerBuild.get(buildId) ?? [];+ build.styles = JSON.stringify(styles);+ build.styleSources = JSON.stringify(styleSources);+ }+ await Promise.all(+ builds.map(({ id, styles, styleSources }) =>+ prisma.build.update({+ where: { id },+ data: { styles, styleSources },+ })+ )+ );+ }+ },+ { timeout: 1000 * 60 * 8 }+ );+};diff --git a/packages/prisma-client/prisma/migrations/20230130160827_build_styles/schema.prisma b/packages/prisma-client/prisma/migrations/20230130160827_build_styles/schema.prismanew file mode 100644index 0000000000..d942af2378--- /dev/null+++ b/packages/prisma-client/prisma/migrations/20230130160827_build_styles/schema.prisma@@ -0,0 +1,130 @@+// DO NOT EDIT THIS FILE!+// This is a copy of your schema.prisma that corresponds to the state of the database+// when all migrations up until this one are applied.+// It's used to generate a Prisma Client for the migration.++// This is your Prisma schema file,+// learn more about it in the docs: https://pris.ly/d/prisma-schema++generator client {+ provider = "prisma-client-js"+ previewFeatures = ["views", "clientExtensions"]+ output = "client"+}++datasource db {+ provider = "postgres"+ url = env("DATABASE_URL")+}++model Team {+ id String @id @default(uuid())+ users User[]+}++enum Location {+ FS+ REMOTE+}++enum UploadStatus {+ UPLOADING+ UPLOADED+}++model Asset {+ id String @id @default(uuid())+ projectId String+ format String+ size Int+ name String+ description String?+ location Location+ createdAt DateTime @default(now())+ meta String @default("{}")+ status UploadStatus @default(UPLOADED)+}++model User {+ id String @id @default(uuid())+ email String? @unique+ provider String?+ image String?+ username String?+ createdAt DateTime @default(now())+ team Team? @relation(fields: [teamId], references: [id])+ teamId String?+ projects Project[]+}++model Project {+ id String @id @default(uuid())+ title String+ domain String @unique+ user User? @relation(fields: [userId], references: [id])+ userId String?+ build Build[]+ trees Tree[]+ isDeleted Boolean @default(false)+}++model Build {+ id String @id @default(uuid())+ createdAt DateTime @default(now())+ pages String++ project Project @relation(fields: [projectId], references: [id])+ projectId String+ trees Tree[]++ isDev Boolean // exctly one is true per project+ isProd Boolean // at most one is true per project (none if not published)++ breakpoints Breakpoints?++ styles String @default("[]")+ styleSources String @default("[]")+}++model Tree {+ id String @id @default(uuid())+ project Project @relation(fields: [projectId], references: [id])+ projectId String+ build Build @relation(fields: [buildId], references: [id])+ buildId String+ root String+ instances String @default("[]")+ props String @default("[]")+ styles String @default("[]")+ styleSelections String @default("[]")+}++model Breakpoints {+ build Build @relation(fields: [buildId], references: [id])+ buildId String @id+ values String @default("[]")+}++enum AuthorizationPermit {+ VIEW+ EDIT+}++model AuthorizationTokens {+ id String @id @default(uuid())+ // No relation to Project, as the Authorization system is not tied to a project+ projectId String+ token String+ permit AuthorizationPermit+ createdAt DateTime @default(now())+}++// Dashboard+view DashboardProject {+ id String @id @default(uuid())+ title String+ domain String+ userId String?+ isDeleted Boolean @default(false)+ isPublished Boolean+}diff --git a/packages/project-build/src/schema/style-sources.ts b/packages/project-build/src/schema/style-sources.tsindex 89af6d1d7a..bf6bfc6660 100644--- a/packages/project-build/src/schema/style-sources.ts+++ b/packages/project-build/src/schema/style-sources.ts@@ -13,7 +13,6 @@ const StyleSourceLocal = z.object({type: z.literal("local"),id: StyleSourceId,treeId: z.string(),- name: z.string(),});export const StyleSource = z.union([StyleSourceToken, StyleSourceLocal]);diff --git a/packages/project-build/src/schema/styles.ts b/packages/project-build/src/schema/styles.tsindex 711bcc2e42..2e3177ba33 100644--- a/packages/project-build/src/schema/styles.ts+++ b/packages/project-build/src/schema/styles.ts@@ -11,8 +11,8 @@ const StoredImageValue = z.object({});export const StoredStylesItem = z.object({+ styleSourceId: z.string(),breakpointId: z.string(),- instanceId: z.string(),// @todo can't figure out how to make property to be enumproperty: z.string() as z.ZodType<StyleProperty>,value: z.union([StoredImageValue, SharedStyleValue]),diff --git a/packages/project/src/db/styles.ts b/packages/project/src/db/styles.tsindex 2519d73b0e..b838db5f7b 100644--- a/packages/project/src/db/styles.ts+++ b/packages/project/src/db/styles.ts@@ -1,9 +1,18 @@import warnOnce from "warn-once";+import ObjectId from "bson-objectid";import { type Patch, applyPatches } from "immer";import { prisma } from "@webstudio-is/prisma-client";import type { Asset } from "@webstudio-is/asset-uploader";import { formatAsset } from "@webstudio-is/asset-uploader/server";-import { StoredStyles, Styles, type Tree } from "@webstudio-is/project-build";+import {+ type Instance,+ StoredStyles,+ Styles,+ type StyleSource,+ StyleSources,+ StyleSourceSelections,+ type Tree,+} from "@webstudio-is/project-build";import type { Project } from "./schema";import {authorizeProject,@@ -38,9 +47,39 @@ const parseValue = (return styleValue;};-export const parseStyles = async (stylesString: string) => {+export const parseStyles = async (+ treeId: string,+ stylesString: string,+ styleSourcesString: string,+ styleSourceSelectionsString: string+) => {+ const styleSources = StyleSources.parse(JSON.parse(styleSourcesString));+ const styleSourceSelections = StyleSourceSelections.parse(+ JSON.parse(styleSourceSelectionsString)+ );const storedStyles = StoredStyles.parse(JSON.parse(stylesString));+ const instanceIdByStyleSourceId = new Map<+ StyleSource["id"],+ Instance["id"]+ >();+ const currentTreeStyleSources = new Set<StyleSource["id"]>();++ for (const styleSource of styleSources) {+ if (styleSource.type === "local" && styleSource.treeId === treeId) {+ currentTreeStyleSources.add(styleSource.id);+ }+ }++ for (const stylesSourceSelection of styleSourceSelections) {+ for (const styleSourceId of stylesSourceSelection.values) {+ instanceIdByStyleSourceId.set(+ styleSourceId,+ stylesSourceSelection.instanceId+ );+ }+ }+const assetIds: string[] = [];for (const { value: styleValue } of storedStyles) {if (styleValue.type === "image") {@@ -65,13 +104,21 @@ export const parseStyles = async (stylesString: string) => {assetsMap.set(asset.id, formatAsset(asset));}- const styles: Styles = storedStyles.map((stylesItem) => {- return {- breakpointId: stylesItem.breakpointId,- instanceId: stylesItem.instanceId,- property: stylesItem.property,- value: parseValue(stylesItem.value, assetsMap),+ const styles: Styles = storedStyles.flatMap((styleDecl) => {+ if (currentTreeStyleSources.has(styleDecl.styleSourceId) === false) {+ return [];+ }+ const instanceId = instanceIdByStyleSourceId.get(styleDecl.styleSourceId);+ if (instanceId === undefined) {+ return [];+ }+ const clientStyle = {+ breakpointId: styleDecl.breakpointId,+ instanceId,+ property: styleDecl.property,+ value: parseValue(styleDecl.value, assetsMap),};+ return [clientStyle];});return styles;@@ -94,13 +141,91 @@ const serializeValue = (styleValue: Styles[number]["value"]) => {return styleValue;};-export const serializeStyles = (styles: Styles) => {- const storedStyles: StoredStyles = styles.map((stylesItem) => {+export const convertToStoredStyles = (+ treeId: string,+ styles: Styles,+ styleSources: StyleSources,+ styleSourceSelections: StyleSourceSelections+): StoredStyles => {+ const styleSourceIdByInstanceId = new Map<+ Instance["id"],+ StyleSource["id"]+ >();++ for (const stylesSourceSelection of styleSourceSelections) {+ for (const styleSourceId of stylesSourceSelection.values) {+ styleSourceIdByInstanceId.set(+ stylesSourceSelection.instanceId,+ styleSourceId+ );+ }+ }++ const storedStyles: StoredStyles = styles.map((styleDecl) => {+ const { instanceId } = styleDecl;+ let styleSourceId = styleSourceIdByInstanceId.get(instanceId);+ if (styleSourceId === undefined) {+ styleSourceId = ObjectId().toString();+ styleSourceSelections.push({+ instanceId,+ values: [styleSourceId],+ });+ styleSources.push({+ type: "local",+ id: styleSourceId,+ treeId,+ });+ }return {- breakpointId: stylesItem.breakpointId,- instanceId: stylesItem.instanceId,- property: stylesItem.property,- value: serializeValue(stylesItem.value),+ breakpointId: styleDecl.breakpointId,+ styleSourceId,+ property: styleDecl.property,+ value: serializeValue(styleDecl.value),+ };+ });+ return storedStyles;+};++export const serializeStyles = (+ treeId: string,+ styles: Styles,+ styleSources: StyleSources,+ styleSourceSelections: StyleSourceSelections+) => {+ const styleSourceIdByInstanceId = new Map<+ Instance["id"],+ StyleSource["id"]+ >();++ for (const stylesSourceSelection of styleSourceSelections) {+ for (const styleSourceId of stylesSourceSelection.values) {+ styleSourceIdByInstanceId.set(+ stylesSourceSelection.instanceId,+ styleSourceId+ );+ }+ }++ const storedStyles: StoredStyles = styles.map((styleDecl) => {+ const { instanceId } = styleDecl;+ let styleSourceId = styleSourceIdByInstanceId.get(instanceId);+ if (styleSourceId === undefined) {+ styleSourceId = ObjectId().toString();+ styleSourceSelections.push({+ instanceId,+ values: [styleSourceId],+ });+ styleSources.push({+ type: "local",+ id: styleSourceId,+ treeId,+ });+ }+ return {+ breakpointId: styleDecl.breakpointId,+ styleSourceId,+ property: styleDecl.property,+ value: serializeValue(styleDecl.value),};});return JSON.stringify(storedStyles);@@ -126,12 +251,62 @@ export const patch = async (if (tree === null) {return;}- const styles = await parseStyles(tree.styles);- const patchedStyles = Styles.parse(applyPatches(styles, patches));++ const build = await prisma.build.findUnique({+ where: { id: tree.buildId },+ });+ if (build === null) {+ return null;+ }++ const storedStyles = StoredStyles.parse(JSON.parse(build.styles));+ const styleSources = StyleSources.parse(JSON.parse(build.styleSources));+ const styleSourceSelections = StyleSourceSelections.parse(+ JSON.parse(tree.styleSelections)+ );++ // these styles are filtered by treeId+ const treeStyles = await parseStyles(+ treeId,+ build.styles,+ build.styleSources,+ tree.styleSelections+ );++ const patchedTreeStyles = Styles.parse(applyPatches(treeStyles, patches));++ const currentTreeStyleSources = new Set<StyleSource["id"]>();+ for (const styleSource of styleSources) {+ if (styleSource.type === "local" && styleSource.treeId === treeId) {+ currentTreeStyleSources.add(styleSource.id);+ }+ }++ // exclude all current tree styles and append patched styles to the end+ const patchedStoredStyles = storedStyles.filter(+ (styleDecl) =>+ currentTreeStyleSources.has(styleDecl.styleSourceId) === false+ );+ patchedStoredStyles.push(+ ...convertToStoredStyles(+ treeId,+ patchedTreeStyles,+ styleSources,+ styleSourceSelections+ )+ );++ await prisma.build.update({+ data: {+ styles: JSON.stringify(patchedStoredStyles),+ styleSources: JSON.stringify(styleSources),+ },+ where: { id: build.id },+ });await prisma.tree.update({data: {- styles: serializeStyles(patchedStyles),+ styleSelections: JSON.stringify(styleSourceSelections),},where: { id: treeId },});diff --git a/packages/project/src/db/tree.ts b/packages/project/src/db/tree.tsindex 125e2072be..2573292952 100644--- a/packages/project/src/db/tree.ts+++ b/packages/project/src/db/tree.ts@@ -1,4 +1,5 @@import { applyPatches, type Patch } from "immer";+import { v4 as uuid } from "uuid";import { z } from "zod";import {type Tree,@@ -7,14 +8,13 @@ import {Instances,type Styles,type Props,+ StyleSourceSelections,+ StyleSources,+ StoredStyles,} from "@webstudio-is/project-build";-import {- prisma,- type Prisma,- type Tree as DbTree,-} from "@webstudio-is/prisma-client";+import { prisma, type Prisma } from "@webstudio-is/prisma-client";import { utils } from "../index";-import { parseStyles, serializeStyles } from "./styles";+import { convertToStoredStyles, parseStyles } from "./styles";import { parseProps, serializeProps } from "./props";import type { Project } from "./schema";import {@@ -67,19 +67,55 @@ export const createTree = ({export const create = async (treeData: TreeData,client: Prisma.TransactionClient = prisma-): Promise<DbTree> => {+) => {const root = Instance.parse(treeData.root);const instances: Instances = [];normalizeTree(root, instances);+ const newTreeId = uuid();++ const build = await client.build.findUnique({+ where: {+ id: treeData.buildId,+ },+ });++ if (build === null) {+ throw Error(`Build ${treeData.buildId} not found`);+ }++ const storedStyles = StoredStyles.parse(JSON.parse(build.styles));+ const styleSources = StyleSources.parse(JSON.parse(build.styleSources));+ const styleSourceSelections: StyleSourceSelections = [];+ storedStyles.push(+ ...convertToStoredStyles(+ newTreeId,+ treeData.styles,+ styleSources,+ styleSourceSelections+ )+ );++ await client.build.update({+ where: {+ id: treeData.buildId,+ },+ data: {+ styles: JSON.stringify(storedStyles),+ styleSources: JSON.stringify(styleSources),+ },+ });+return await client.tree.create({data: {+ id: newTreeId,projectId: treeData.projectId,buildId: treeData.buildId,root: "",+ styles: "",instances: JSON.stringify(instances),props: serializeProps(treeData.props),- styles: serializeStyles(treeData.styles),+ styleSelections: JSON.stringify(styleSourceSelections),},});};@@ -119,16 +155,27 @@ export const loadById = async (const tree = await client.tree.findUnique({where: { id: treeId },});-if (tree === null) {return null;}+ const build = await client.build.findUnique({+ where: { id: tree.buildId },+ });+ if (build === null) {+ return null;+ }+const instances = Instances.parse(JSON.parse(tree.instances));const root = Instance.parse(denormalizeTree(instances));const props = await parseProps(tree.props);- const styles = await parseStyles(tree.styles);+ const styles = await parseStyles(+ treeId,+ build.styles,+ build.styleSources,+ tree.styleSelections+ );return {...tree,