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.

migration to asset.meta

Oleg Isonenccd77ed37275
diff --git a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-info-tigger.tsx b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-info-tigger.tsx
index 50c9181e16..090d9ff201 100644
--- a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-info-tigger.tsx
+++ b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-info-tigger.tsx
@@ -11,7 +11,7 @@ import { GearIcon, gearIconCssVars } from "@webstudio-is/icons";
import { AssetInfo } from "./asset-info";
import { cssVars } from "@webstudio-is/css-vars";
-import { BaseAsset } from "./types";
+import { BaseAsset } from "~/designer/shared/assets";
const triggerVisibilityVar = cssVars.define("trigger-visibility");
diff --git a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-info.tsx b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-info.tsx
index 81ac6ff527..94ce843b40 100644
--- a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-info.tsx
+++ b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-info.tsx
@@ -8,7 +8,7 @@ import {
} from "@webstudio-is/icons";
import { useSubmit } from "@remix-run/react";
import prettyBytes from "pretty-bytes";
-import { BaseAsset } from "./types";
+import type { BaseAsset } from "~/designer/shared/assets";
const Filename = ({ name }: { name: string }) => {
const splitName = name.split(".");
@@ -52,8 +52,7 @@ type AssetInfoProps = BaseAsset & {
export const AssetInfo = ({
size,
- width,
- height,
+ meta,
id,
name,
onClose,
@@ -87,14 +86,12 @@ export const AssetInfo = ({
<Flex align="center" css={{ gap: "$1" }}>
<SizeIcon />
<Text variant="label">
- {width} x {height}
+ {meta.width} x {meta.height}
</Text>
</Flex>{" "}
<Flex align="center" css={{ gap: "$1" }}>
<AspectRatioIcon />
- <Text variant="label">
- {getFormattedAspectRatio({ width, height })}
- </Text>
+ <Text variant="label">{getFormattedAspectRatio(meta)}</Text>
</Flex>
</Grid>
</Box>
diff --git a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-thumbnail.tsx b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-thumbnail.tsx
index 58eb9f6a14..3abc279822 100644
--- a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-thumbnail.tsx
+++ b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-thumbnail.tsx
@@ -4,7 +4,7 @@ import placeholderImage from "~/shared/images/image-placeholder.svg";
import brokenImage from "~/shared/images/broken-image-placeholder.svg";
import { UploadingAnimation } from "./uploading-animation";
import { AssetInfoTrigger, assetInfoTriggerCssVars } from "./asset-info-tigger";
-import { BaseAsset } from "./types";
+import type { BaseAsset } from "~/designer/shared/assets";
const useImageWithFallback = ({
path = placeholderImage,
@@ -52,14 +52,14 @@ export const Thumbnail = ({ path, status }: ThumbnailProps) => {
};
export const AssetThumbnail = (asset: BaseAsset) => {
- const { path, alt, status, name } = asset;
+ const { path, description, status, name } = asset;
const [isDeleting, setIsDeleting] = useState(false);
const isUploading = status === "uploading";
const isUploadedAsset = isUploading === false && "size" in asset;
return (
<Box
- title={alt || name}
+ title={description || name}
css={{
aspectRatio: "1/1",
display: "flex",
diff --git a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/types.ts b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/types.ts
deleted file mode 100644
index 94d5f7bb43..0000000000
--- a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/types.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-import { Asset } from "@webstudio-is/asset-uploader";
-
-export type BaseAsset = Pick<
- Asset,
- "id" | "status" | "name" | "path" | "alt" | "size" | "width" | "height"
->;
diff --git a/apps/designer/app/designer/shared/assets/asset-upload.tsx b/apps/designer/app/designer/shared/assets/asset-upload.tsx
index cdae2c5ec6..5e5fc55923 100644
--- a/apps/designer/app/designer/shared/assets/asset-upload.tsx
+++ b/apps/designer/app/designer/shared/assets/asset-upload.tsx
@@ -2,8 +2,8 @@ import { Form, useSubmit } from "@remix-run/react";
import ObjectID from "bson-objectid";
import { ChangeEvent, useRef } from "react";
import { Button, Flex, Text } from "@webstudio-is/design-system";
-import { BaseAsset } from "../../features/sidebar-left/panels/asset-manager/types";
import { UploadIcon } from "@webstudio-is/icons";
+import type { BaseAsset } from "./types";
const readAssets = (fileList: FileList): Promise<BaseAsset[]> => {
const assets: Array<Promise<BaseAsset>> = Array.from(fileList).map(
@@ -18,8 +18,9 @@ const readAssets = (fileList: FileList): Promise<BaseAsset[]> => {
name: file.name,
id: ObjectID().toString(),
status: "uploading",
- alt: file.name,
+ description: file.name,
size: file.size,
+ meta: {},
});
});
reader.readAsDataURL(file);
diff --git a/apps/designer/app/designer/shared/assets/types.ts b/apps/designer/app/designer/shared/assets/types.ts
index 3b4ee1b828..a5cf5c3fee 100644
--- a/apps/designer/app/designer/shared/assets/types.ts
+++ b/apps/designer/app/designer/shared/assets/types.ts
@@ -2,7 +2,7 @@ import { Asset } from "@webstudio-is/asset-uploader";
export type BaseAsset = Pick<
Asset,
- "id" | "status" | "name" | "path" | "alt" | "size" | "width" | "height"
+ "id" | "status" | "name" | "path" | "description" | "size" | "meta"
>;
export type ActionData = {
diff --git a/packages/asset-uploader/src/db/create.ts b/packages/asset-uploader/src/db/create.ts
index 9c248ab890..1cbd92e4f3 100644
--- a/packages/asset-uploader/src/db/create.ts
+++ b/packages/asset-uploader/src/db/create.ts
@@ -18,8 +18,7 @@ const create = (projectId: Project["id"], options: Options) => {
name,
size,
format: metadata.format,
- ...(metadata.width ? { width: metadata.width } : {}),
- ...(metadata.height ? { height: metadata.height } : {}),
+ meta: JSON.stringify({ width: metadata.width, height: metadata.height }),
projectId,
},
});
@@ -32,6 +31,5 @@ export const createMany = async (
) => {
const promisedData = values.map((options) => create(projectId, options));
const data = await Promise.all(promisedData);
- console.log(111, data);
return data.map(formatAsset);
};
diff --git a/packages/asset-uploader/src/types.ts b/packages/asset-uploader/src/types.ts
index c692aa1f2c..5928b82cde 100644
--- a/packages/asset-uploader/src/types.ts
+++ b/packages/asset-uploader/src/types.ts
@@ -1,8 +1,12 @@
import { Asset as DbAsset } from "@webstudio-is/prisma-client";
-export type Asset = Omit<DbAsset, "width" | "height"> & {
- path?: string;
- status?: "uploading" | "uploaded";
+type ImageMeta = {
width?: number;
height?: number;
};
+
+export type Asset = Omit<DbAsset, "meta"> & {
+ path?: string;
+ status?: "uploading" | "uploaded";
+ meta: ImageMeta;
+};
diff --git a/packages/asset-uploader/src/utils/format-asset.ts b/packages/asset-uploader/src/utils/format-asset.ts
index 3e443bb136..310c41cfad 100644
--- a/packages/asset-uploader/src/utils/format-asset.ts
+++ b/packages/asset-uploader/src/utils/format-asset.ts
@@ -1,12 +1,27 @@
+import { z } from "zod";
import { Asset as DbAsset } from "@webstudio-is/prisma-client";
import { Asset } from "../types";
import { getAssetPath } from "./get-asset-path";
+// @todo to use to distinguish between images and fonts
+//const formatTypeMap = {
+// woff: "font",
+// woff2: "font",
+// ttf: "font",
+// otf: "font",
+// eot: "font",
+//} as const;
+
+const ImageMeta = z.object({
+ width: z.number().optional(),
+ height: z.number().optional(),
+});
+
export const formatAsset = (asset: DbAsset): Asset => {
+ const meta = ImageMeta.parse(JSON.parse(asset.meta));
return {
...asset,
- width: asset.width ? Number(asset.width) : undefined,
- height: asset.height ? Number(asset.height) : undefined,
+ meta,
path: getAssetPath(asset),
};
};
diff --git a/packages/prisma-client/migrations-cli/README.md b/packages/prisma-client/migrations-cli/README.md
index b18890457c..7eef385a1a 100644
--- a/packages/prisma-client/migrations-cli/README.md
+++ b/packages/prisma-client/migrations-cli/README.md
@@ -50,19 +50,26 @@ NOTE: If this becomes a library there should be more istructions here.
- Run `migrations migrate --dev`
-### I've changed models in `schema.prisma`. How do I update the database?
+### I've changed schema only, no data changes. How do I migrate database schema?
- Create a schema migration by running `migrations create-schema <name>`.
- Apply the migration by running `migrations migrate --dev`.
-### I need to change schema in a way that involves moving data to a new location.
+### I want to change the data, without schema changes. How do I migrate data only?
-- Make changes to `schema.prisma` in a way that both the old and the new locations for the data are defined.
-- Create a schema migration by running `migrations create-schema <name>`, and apply it by running `migrations migrate --dev`.
+TODO
+
+### I need to change the schema and migrate the data.
+
+- Make changes to `schema.prisma` in a way that **both** the old and the new locations for the data are defined.
+- Create a schema migration by running `migrations create-schema <name>`.
+- And the new schema by running `migrations migrate --dev`.
- Create a data migration by running `migrations create-data <name>`.
-- Edit the data migration file to move the data to the new location, and apply the migration.
+- Edit the data migration file to move the data to the new location
+- Apply the migration for data using the same command `migrations migrate --dev`.
- Make changes to `schema.prisma` to remove the old models or fields that are no longer needed.
-- Create a schema migration by running `migrations create-schema <name>`, and apply it.
+- Create a schema migration by running `migrations create-schema <name>`.
+- Apply schema migration `migrations migrate --dev`.
### A migration has failed. What do I do?
diff --git a/packages/prisma-client/prisma/migrations/20220912141854_assets-meta/migration.sql b/packages/prisma-client/prisma/migrations/20220912141854_assets-meta/migration.sql
new file mode 100644
index 0000000000..4fbb8708c1
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20220912141854_assets-meta/migration.sql
@@ -0,0 +1,3 @@
+-- AlterTable
+ALTER TABLE "Asset" ADD COLUMN "description" TEXT,
+ADD COLUMN "meta" TEXT NOT NULL DEFAULT '{}';
diff --git a/packages/prisma-client/prisma/migrations/20220912142938_assets-meta-data/migration.ts b/packages/prisma-client/prisma/migrations/20220912142938_assets-meta-data/migration.ts
new file mode 100644
index 0000000000..15ad7112f7
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20220912142938_assets-meta-data/migration.ts
@@ -0,0 +1,33 @@
+import { assert } from "console";
+import { PrismaClient } from "./client";
+
+// NOTE ON IMPORTS:
+//
+// We want to be able to run old migrations at any point.
+// For example, when we setting up a fresh database or making a reset.
+//
+// You shouldn't import code that may change later
+// and become incompatible with the migration.
+// It's better to copy it to the migration directory.
+
+export default () => {
+ const client = new PrismaClient();
+ return client.$transaction(async (prisma) => {
+ const previousAssets = await prisma.asset.findMany();
+ const update = previousAssets.map((asset) => {
+ return {
+ id: asset.id,
+ description: asset.alt,
+ meta: JSON.stringify({
+ width: Number(asset.width),
+ height: Number(asset.height),
+ }),
+ };
+ });
+ await Promise.all(
+ update.map(({ id, ...data }) =>
+ prisma.asset.update({ where: { id }, data })
+ )
+ );
+ });
+};
diff --git a/packages/prisma-client/prisma/migrations/20220912142938_assets-meta-data/schema.prisma b/packages/prisma-client/prisma/migrations/20220912142938_assets-meta-data/schema.prisma
new file mode 100644
index 0000000000..88cc57402d
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20220912142938_assets-meta-data/schema.prisma
@@ -0,0 +1,87 @@
+// 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
+}
+
+model Asset {
+ id String @id @default(uuid())
+ project Project @relation(fields: [projectId], references: [id])
+ projectId String
+ format String?
+ size Int
+ name String
+ description String?
+ location Location
+ createdAt DateTime @default(now())
+ meta String @default("{}")
+
+ // Migrating
+ width Decimal?
+ height Decimal?
+ alt String?
+}
+
+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
+ prodTreeId String? // exists when published
+ devTreeId String
+ prodTreeIdHistory String @default("[]")
+ user User? @relation(fields: [userId], references: [id])
+ userId String?
+ assets Asset[]
+}
+
+model Tree {
+ id String @id @default(uuid())
+ root String
+}
+
+model InstanceProps {
+ id String @id @default(uuid())
+ instanceId String
+ treeId String
+ props String @default("[]")
+}
+
+model Breakpoints {
+ treeId String @id @default(uuid())
+ values String @default("[]")
+}
diff --git a/packages/prisma-client/prisma/migrations/20220912150542_assets-meta-cleanup/migration.sql b/packages/prisma-client/prisma/migrations/20220912150542_assets-meta-cleanup/migration.sql
new file mode 100644
index 0000000000..a309b57f73
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20220912150542_assets-meta-cleanup/migration.sql
@@ -0,0 +1,4 @@
+-- AlterTable
+ALTER TABLE "Asset" DROP COLUMN "alt",
+DROP COLUMN "height",
+DROP COLUMN "width";
diff --git a/packages/prisma-client/prisma/schema.prisma b/packages/prisma-client/prisma/schema.prisma
index d78a8b9251..c8acb2a27a 100644
--- a/packages/prisma-client/prisma/schema.prisma
+++ b/packages/prisma-client/prisma/schema.prisma
@@ -23,17 +23,16 @@ enum Location {
}
model Asset {
- id String @id @default(uuid())
- project Project @relation(fields: [projectId], references: [id])
- projectId String
- format String?
- size Int
- width Decimal?
- height Decimal?
- name String
- alt String?
- location Location
- createdAt DateTime @default(now())
+ id String @id @default(uuid())
+ project Project @relation(fields: [projectId], references: [id])
+ projectId String
+ format String?
+ size Int
+ name String
+ description String?
+ location Location
+ createdAt DateTime @default(now())
+ meta String @default("{}")
}
model User {