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.

Migrate InstanceProps into Tree table as props column (#819)

Bogdan Chadkin78af54b0175b
diff --git a/packages/prisma-client/prisma/migrations/20230119181836_tree_props/migration.sql b/packages/prisma-client/prisma/migrations/20230119181836_tree_props/migration.sql
new file mode 100644
index 0000000000..21bc5e3680
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20230119181836_tree_props/migration.sql
@@ -0,0 +1,2 @@
+-- AlterTable
+ALTER TABLE "Tree" ADD COLUMN "props" TEXT NOT NULL DEFAULT '[]';
diff --git a/packages/prisma-client/prisma/migrations/20230119181858_tree_props_data/migration.ts b/packages/prisma-client/prisma/migrations/20230119181858_tree_props_data/migration.ts
new file mode 100644
index 0000000000..749551db4c
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20230119181858_tree_props_data/migration.ts
@@ -0,0 +1,82 @@
+import { PrismaClient } from "./client";
+
+type PropsItem = {
+ instanceId: string;
+ id: string;
+ name: string;
+ required?: boolean;
+ type: string;
+ value: unknown;
+};
+
+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 cursor: undefined | string = undefined;
+ let hasNext = true;
+
+ while (hasNext) {
+ const chunkSize = 1000;
+ const trees: any[] = 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;
+
+ const treesProps = await prisma.instanceProps.findMany({
+ where: {
+ treeId: {
+ in: trees.map((tree) => tree.id),
+ },
+ },
+ });
+
+ for (const tree of trees) {
+ const instanceProps = treesProps.filter(
+ (props) => props.treeId === tree.id
+ );
+ try {
+ const list: PropsItem[] = [];
+ for (const { instanceId, props } of instanceProps) {
+ const parsed = JSON.parse(props);
+ for (const prop of parsed) {
+ list.push({
+ instanceId,
+ id: prop.id,
+ name: prop.prop,
+ required: prop.required,
+ type: prop.type,
+ value: prop.value,
+ });
+ }
+ }
+
+ tree.props = JSON.stringify(list);
+ } catch {
+ console.info(`Tree ${tree.id} cannot be converted`);
+ }
+ }
+ await Promise.all(
+ trees.map(({ id, props }) =>
+ prisma.tree.update({ where: { id }, data: { props } })
+ )
+ );
+ }
+ },
+ { timeout: 1000 * 60 * 5 }
+ );
+};
diff --git a/packages/prisma-client/prisma/migrations/20230119181858_tree_props_data/schema.prisma b/packages/prisma-client/prisma/migrations/20230119181858_tree_props_data/schema.prisma
new file mode 100644
index 0000000000..0e6e0de952
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20230119181858_tree_props_data/schema.prisma
@@ -0,0 +1,112 @@
+// 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 = ["interactiveTransactions"]
+ 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[]
+}
+
+model Build {
+ id String @id @default(uuid())
+ createdAt DateTime @default(now())
+ pages String
+
+ project Project @relation(fields: [projectId], references: [id])
+ projectId String
+
+ isDev Boolean // exctly one is true per project
+ isProd Boolean // at most one is true per project (none if not published)
+
+ breakpoints Breakpoints?
+ designTokens DesignTokens?
+}
+
+model Tree {
+ id String @id @default(uuid())
+ root String
+ instances String @default("[]")
+ props String @default("[]")
+ presetStyles String @default("[]")
+ styles String @default("[]")
+}
+
+model InstanceProps {
+ id String @id @default(uuid())
+ instanceId String
+ treeId String
+ props String @default("[]")
+
+ @@unique([instanceId, treeId])
+}
+
+model Breakpoints {
+ build Build @relation(fields: [buildId], references: [id])
+ buildId String @id
+ values String @default("[]")
+}
+
+model DesignTokens {
+ build Build @relation(fields: [buildId], references: [id])
+ buildId String @id
+ value String @default("[]")
+}
diff --git a/packages/prisma-client/prisma/schema.prisma b/packages/prisma-client/prisma/schema.prisma
index 0fb09682cb..fcdad67396 100644
--- a/packages/prisma-client/prisma/schema.prisma
+++ b/packages/prisma-client/prisma/schema.prisma
@@ -80,6 +80,7 @@ model Tree {
id String @id @default(uuid())
root String
instances String @default("[]")
+ props String @default("[]")
presetStyles String @default("[]")
styles String @default("[]")
}
diff --git a/packages/project/src/db/build.ts b/packages/project/src/db/build.ts
index 9887c18dca..38105c9c68 100644
--- a/packages/project/src/db/build.ts
+++ b/packages/project/src/db/build.ts
@@ -143,7 +143,6 @@ export const deletePage = async (buildId: Build["id"], pageId: Page["id"]) => {
throw new Error(`Page with id "${pageId}" not found`);
}
- await db.props.deleteByTreeId(page.treeId);
await db.tree.deleteById(page.treeId);
return {
@@ -174,7 +173,6 @@ const clonePage = async (
) => {
const treeId = source.treeId;
const tree = await db.tree.clone(treeId, client);
- await db.props.clone({ previousTreeId: treeId, nextTreeId: tree.id }, client);
return { ...source, id: uuid(), treeId: tree.id };
};
diff --git a/packages/project/src/db/props.ts b/packages/project/src/db/props.ts
index 9d1102909f..212451485a 100644
--- a/packages/project/src/db/props.ts
+++ b/packages/project/src/db/props.ts
@@ -1,69 +1,35 @@
-import { v4 as uuid } from "uuid";
-import { z } from "zod";
-
import warnOnce from "warn-once";
import {
type Tree,
type AllUserProps,
- type UserProp,
- UserProps,
+ type InstanceProps,
+ type PropsItem,
+ Props,
} from "@webstudio-is/react-sdk";
import { applyPatches, type Patch } from "immer";
-import { prisma, Prisma } from "@webstudio-is/prisma-client";
+import { prisma } from "@webstudio-is/prisma-client";
import { formatAsset } from "@webstudio-is/asset-uploader/server";
-const baseUserProps = {
- id: z.string(),
- prop: z.string(),
- required: z.optional(z.boolean()),
-};
-
-export const UserDbProp = z.discriminatedUnion("type", [
- z.object({
- ...baseUserProps,
- type: z.literal("number"),
- value: z.number(),
- }),
- z.object({
- ...baseUserProps,
- type: z.literal("string"),
- value: z.string(),
- }),
- z.object({
- ...baseUserProps,
- type: z.literal("boolean"),
- value: z.boolean(),
- }),
- z.object({
- ...baseUserProps,
- type: z.literal("asset"),
- // In database we hold asset.id
- value: z.string(),
- }),
-]);
-
-const UserDbProps = z.array(UserDbProp);
-
-type UserDbProp = z.infer<typeof UserDbProp>;
-
export const loadByTreeId = async (treeId: Tree["id"]) => {
- const instancePropsEntries = await prisma.instanceProps.findMany({
- where: { treeId },
+ const tree = await prisma.tree.findUnique({
+ where: { id: treeId },
});
+ if (tree === null) {
+ return [];
+ }
+
+ const props = Props.parse(JSON.parse(tree.props));
+
// Fin all assetId in all props
const assetIds: string[] = [];
- for (const instanceProps of instancePropsEntries) {
- const props = JSON.parse(instanceProps.props);
- const dbProps = UserDbProps.parse(props);
-
- for (const dbProp of dbProps) {
- if (dbProp.type === "asset") {
- assetIds.push(dbProp.value);
- }
+ for (const propsItem of props) {
+ if (propsItem.type === "asset") {
+ assetIds.push(propsItem.value);
}
}
+
// Load all assets
const assets = await prisma.asset.findMany({
where: {
@@ -77,69 +43,50 @@ export const loadByTreeId = async (treeId: Tree["id"]) => {
assets.map((asset) => [asset.id, formatAsset(asset)])
);
- return instancePropsEntries.map((instanceProps) => {
- const props = JSON.parse(instanceProps.props);
- const userProps: UserProp[] = [];
-
- // Can be changed onto `props as ` having that above is already checked, no optmizations now
- const dpProps = UserDbProps.parse(props);
+ const instancePropsMap = new Map<string, InstanceProps>();
+
+ for (const propsItem of props) {
+ let instanceProps = instancePropsMap.get(propsItem.instanceId);
+ if (instanceProps === undefined) {
+ instanceProps = {
+ id: "",
+ instanceId: propsItem.instanceId,
+ treeId,
+ props: [],
+ };
+ instancePropsMap.set(propsItem.instanceId, instanceProps);
+ }
- for (const dbProp of dpProps) {
- if (dbProp.type === "asset") {
- const assetId = dbProp.value;
- const asset = assetsMap.get(assetId);
+ if (propsItem.type === "asset") {
+ const assetId = propsItem.value;
+ const asset = assetsMap.get(assetId);
- if (asset) {
- userProps.push({
- ...dbProp,
- value: asset,
- });
- continue;
- }
+ if (asset) {
+ instanceProps.props.push({
+ id: propsItem.id,
+ prop: propsItem.name,
+ required: propsItem.required,
+ type: propsItem.type,
+ value: asset,
+ });
- warnOnce(true, `Asset with assetId "${assetId}" not found`);
continue;
}
- userProps.push(dbProp);
+ warnOnce(true, `Asset with assetId "${assetId}" not found`);
+ continue;
}
- return {
- ...instanceProps,
- props: userProps,
- };
- });
-};
-
-export const deleteByTreeId = async (treeId: Tree["id"]) => {
- await prisma.instanceProps.deleteMany({ where: { treeId } });
-};
-
-export const clone = async (
- {
- previousTreeId,
- nextTreeId,
- }: {
- previousTreeId: string;
- nextTreeId: string;
- },
- client: Prisma.TransactionClient | typeof prisma = prisma
-) => {
- const props = await client.instanceProps.findMany({
- where: { treeId: previousTreeId },
- });
-
- if (props.length === 0) {
- return;
+ instanceProps.props.push({
+ id: propsItem.id,
+ prop: propsItem.name,
+ required: propsItem.required,
+ type: propsItem.type,
+ value: propsItem.value,
+ } as InstanceProps["props"][number]);
}
- const data = props.map(({ id: _id, treeId: _treeId, ...rest }) => ({
- ...rest,
- treeId: nextTreeId,
- }));
- await client.instanceProps.createMany({
- data,
- });
+ return Array.from(instancePropsMap.values());
};
export const patch = async (
@@ -160,34 +107,35 @@ export const patch = async (
patches
);
- await Promise.all(
- Object.entries(nextProps).map(async ([instanceId, props]) => {
- const propsDb: UserDbProp[] = UserProps.parse(props).map((prop) => {
- if (prop.type === "asset") {
- return {
- ...prop,
- value: prop.value.id,
- };
- }
-
- return prop;
- });
-
- const propsString = JSON.stringify(propsDb);
-
- await prisma.instanceProps.upsert({
- // eslint-disable-next-line camelcase
- where: { instanceId_treeId: { instanceId, treeId } },
- create: {
- id: uuid(),
+ const props: Props = [];
+ for (const [instanceId, instanceProps] of Object.entries(nextProps)) {
+ for (const prop of instanceProps) {
+ if (prop.type === "asset") {
+ props.push({
+ id: prop.id,
instanceId,
- treeId,
- props: propsString,
- },
- update: {
- props: propsString,
- },
- });
- })
- );
+ name: prop.prop,
+ required: prop.required,
+ type: prop.type,
+ value: prop.value.id,
+ });
+ continue;
+ }
+ props.push({
+ id: prop.id,
+ instanceId,
+ name: prop.prop,
+ required: prop.required,
+ type: prop.type,
+ value: prop.value,
+ } as PropsItem);
+ }
+ }
+
+ await prisma.tree.update({
+ data: {
+ props: JSON.stringify(props),
+ },
+ where: { id: treeId },
+ });
};
diff --git a/packages/project/src/db/tree.ts b/packages/project/src/db/tree.ts
index c35ce5e7f6..6b8286b0d0 100644
--- a/packages/project/src/db/tree.ts
+++ b/packages/project/src/db/tree.ts
@@ -9,6 +9,7 @@ import {
findMissingPresetStyles,
Styles,
Instances,
+ Props,
} from "@webstudio-is/react-sdk";
import {
prisma,
@@ -44,9 +45,11 @@ export const createTree = (): TreeData => {
const styles: Styles = [];
const instances: Instances = [];
normalizeTree(root, instances);
+ const props: Props = [];
return {
root,
+ props,
presetStyles,
styles,
};
@@ -64,6 +67,7 @@ export const create = async (
data: {
root: "",
instances: JSON.stringify(instances),
+ props: JSON.stringify(treeData.props),
presetStyles: JSON.stringify(treeData.presetStyles),
styles: JSON.stringify(await StylesDbIn.parseAsync(treeData.styles)),
},
@@ -113,6 +117,7 @@ export const loadById = async (
const instances = Instances.parse(JSON.parse(tree.instances));
const root = Instance.parse(denormalizeTree(instances));
+ const props = Props.parse(JSON.parse(tree.props));
const presetStyles = PresetStyles.parse(JSON.parse(tree.presetStyles));
const styles = Styles.parse(
await StylesDbOut.parseAsync(JSON.parse(tree.styles))
@@ -121,6 +126,7 @@ export const loadById = async (
return {
...tree,
root,
+ props,
presetStyles,
styles,
};
diff --git a/packages/react-sdk/src/db/index.ts b/packages/react-sdk/src/db/index.ts
index 481cfdd2eb..0d944b9f6d 100644
--- a/packages/react-sdk/src/db/index.ts
+++ b/packages/react-sdk/src/db/index.ts
@@ -1,3 +1,4 @@
export * from "./types";
export * from "./instance";
+export * from "./props";
export * from "./style";
diff --git a/packages/react-sdk/src/db/props.ts b/packages/react-sdk/src/db/props.ts
new file mode 100644
index 0000000000..a6cfe00523
--- /dev/null
+++ b/packages/react-sdk/src/db/props.ts
@@ -0,0 +1,38 @@
+import { z } from "zod";
+
+const basePropsItem = {
+ id: z.string(),
+ instanceId: z.string(),
+ name: z.string(),
+ required: z.optional(z.boolean()),
+};
+
+export const PropsItem = z.discriminatedUnion("type", [
+ z.object({
+ ...basePropsItem,
+ type: z.literal("number"),
+ value: z.number(),
+ }),
+ z.object({
+ ...basePropsItem,
+ type: z.literal("string"),
+ value: z.string(),
+ }),
+ z.object({
+ ...basePropsItem,
+ type: z.literal("boolean"),
+ value: z.boolean(),
+ }),
+ z.object({
+ ...basePropsItem,
+ type: z.literal("asset"),
+ // In database we hold asset.id
+ value: z.string(),
+ }),
+]);
+
+export type PropsItem = z.infer<typeof PropsItem>;
+
+export const Props = z.array(PropsItem);
+
+export type Props = z.infer<typeof Props>;
diff --git a/packages/react-sdk/src/db/types.ts b/packages/react-sdk/src/db/types.ts
index 07ff61721b..c20872087d 100644
--- a/packages/react-sdk/src/db/types.ts
+++ b/packages/react-sdk/src/db/types.ts
@@ -1,22 +1,17 @@
import type { InstanceProps as DbInstanceProps } from "@webstudio-is/prisma-client";
import type { UserProp } from "../user-props";
import type { Instance } from "./instance";
+import type { Props } from "./props";
import type { PresetStyles, Styles } from "./style";
export type Tree = {
id: string;
root: Instance;
+ props: Props;
presetStyles: PresetStyles;
styles: Styles;
};
-export type Props = {
- props: string;
- id: string;
- instanceId: string;
- treeId: string;
-};
-
export type InstanceProps = Omit<DbInstanceProps, "props"> & {
props: Array<UserProp>;
};