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.

Move assets data into File table (#1573)

Bogdan Chadkin74da71b7c702
diff --git a/packages/asset-uploader/src/db/clone.ts b/packages/asset-uploader/src/db/clone.ts
index 2499716d5c..5a98a9c273 100644
--- a/packages/asset-uploader/src/db/clone.ts
+++ b/packages/asset-uploader/src/db/clone.ts
@@ -48,7 +48,10 @@ export const cloneAssets = async (
}
const assets = await client.asset.findMany({
- where: { projectId: fromProjectId, status: "UPLOADED" },
+ where: {
+ projectId: fromProjectId,
+ file: { status: "UPLOADED" },
+ },
});
await client.asset.createMany({
diff --git a/packages/asset-uploader/src/db/delete.ts b/packages/asset-uploader/src/db/delete.ts
deleted file mode 100644
index 99aa823369..0000000000
--- a/packages/asset-uploader/src/db/delete.ts
+++ /dev/null
@@ -1,34 +0,0 @@
-import { prisma, type Project } from "@webstudio-is/prisma-client";
-import {
- authorizeProject,
- type AppContext,
- AuthorizationError,
-} from "@webstudio-is/trpc-interface/index.server";
-import type { Asset } from "../schema";
-
-export const deleteFromDb = async (
- props: {
- ids: Array<Asset["id"]>;
- projectId: Project["id"];
- },
- context: AppContext
-) => {
- if (props.ids.length === 0) {
- throw new Error("Asset IDs required");
- }
-
- const canDelete = await authorizeProject.hasProjectPermit(
- { projectId: props.projectId, permit: "edit" },
- context
- );
-
- if (canDelete === false) {
- throw new AuthorizationError(
- "You don't have access to create this project assets"
- );
- }
-
- return await prisma.asset.deleteMany({
- where: { id: { in: props.ids }, projectId: props.projectId },
- });
-};
diff --git a/packages/asset-uploader/src/db/index.ts b/packages/asset-uploader/src/db/index.ts
index 0119e4f719..d405a57654 100644
--- a/packages/asset-uploader/src/db/index.ts
+++ b/packages/asset-uploader/src/db/index.ts
@@ -1,3 +1,2 @@
export * from "./load";
-export * from "./delete";
export * from "./clone";
diff --git a/packages/asset-uploader/src/db/load.ts b/packages/asset-uploader/src/db/load.ts
index ad8b004601..75714a54b1 100644
--- a/packages/asset-uploader/src/db/load.ts
+++ b/packages/asset-uploader/src/db/load.ts
@@ -23,11 +23,21 @@ export const loadAssetsByProject = async (
}
const assets = await prisma.asset.findMany({
- where: { projectId, status: "UPLOADED" },
+ select: {
+ file: true,
+ id: true,
+ projectId: true,
+ name: true,
+ location: true,
+ },
+ where: {
+ projectId,
+ file: { status: "UPLOADED" },
+ },
orderBy: {
- createdAt: "desc",
+ file: { createdAt: "desc" },
},
});
- return assets.map(formatAsset);
+ return assets.map((asset) => formatAsset(asset, asset.file));
};
diff --git a/packages/asset-uploader/src/delete.ts b/packages/asset-uploader/src/delete.ts
index 3de70daf5b..3af986ee42 100644
--- a/packages/asset-uploader/src/delete.ts
+++ b/packages/asset-uploader/src/delete.ts
@@ -5,7 +5,6 @@ import {
AuthorizationError,
} from "@webstudio-is/trpc-interface/index.server";
import type { AssetClient } from "./client";
-import { deleteFromDb } from "./db";
import type { Asset } from "./schema";
import { formatAsset } from "./utils/format-asset";
@@ -29,6 +28,13 @@ export const deleteAssets = async (
}
const assets = await prisma.asset.findMany({
+ select: {
+ file: true,
+ id: true,
+ projectId: true,
+ name: true,
+ location: true,
+ },
where: { id: { in: props.ids }, projectId: props.projectId },
});
@@ -36,20 +42,26 @@ export const deleteAssets = async (
throw new Error("Assets not found");
}
- await deleteFromDb(props, context);
-
- const assetsByName = await prisma.asset.findMany({
- where: { name: { in: assets.map((asset) => asset.name) } },
+ await prisma.asset.deleteMany({
+ where: { id: { in: props.ids }, projectId: props.projectId },
});
- const stillUsedNames = new Set(assetsByName.map((asset) => asset.name));
- for (const asset of assets) {
- if (stillUsedNames.has(asset.name)) {
- continue;
- }
+ // find unused files
+ const unusedFileNames = new Set(assets.map((asset) => asset.name));
+ const assetsByStillUsedFileName = await prisma.asset.findMany({
+ where: { name: { in: Array.from(unusedFileNames) } },
+ });
+ for (const asset of assetsByStillUsedFileName) {
+ unusedFileNames.delete(asset.name);
+ }
- await client.deleteFile(asset.name);
+ // delete unused files
+ await prisma.file.deleteMany({
+ where: { name: { in: Array.from(unusedFileNames) } },
+ });
+ for (const name of unusedFileNames) {
+ await client.deleteFile(name);
}
- return assets.map(formatAsset);
+ return assets.map((asset) => formatAsset(asset, asset.file));
};
diff --git a/packages/asset-uploader/src/index.server.ts b/packages/asset-uploader/src/index.server.ts
index 36b3f709e5..90a84bf654 100644
--- a/packages/asset-uploader/src/index.server.ts
+++ b/packages/asset-uploader/src/index.server.ts
@@ -2,6 +2,5 @@ export * from "./db";
export * from "./upload";
export * from "./delete";
export * from "./patch";
-export * from "./utils/format-asset";
export * from "./clients/fs/fs";
export * from "./clients/s3/s3";
diff --git a/packages/asset-uploader/src/upload.ts b/packages/asset-uploader/src/upload.ts
index 01e75bce81..f809796406 100644
--- a/packages/asset-uploader/src/upload.ts
+++ b/packages/asset-uploader/src/upload.ts
@@ -42,11 +42,16 @@ export const createUploadName = async (
const count = await prisma.asset.count({
where: {
OR: [
- { projectId, status: "UPLOADED" },
{
projectId,
- status: "UPLOADING",
- createdAt: { gt: new Date(Date.now() - UPLOADING_STALE_TIMEOUT) },
+ file: { status: "UPLOADED" },
+ },
+ {
+ projectId,
+ file: {
+ status: "UPLOADING",
+ createdAt: { gt: new Date(Date.now() - UPLOADING_STALE_TIMEOUT) },
+ },
},
],
},
@@ -74,11 +79,19 @@ export const createUploadName = async (
await prisma.asset.create({
data: {
id: assetId,
- name,
projectId,
+ location: "REMOTE",
+ file: {
+ create: {
+ name,
+ status: "UPLOADING",
+ format: "unknown",
+ size: 0,
+ },
+ },
+ // @todo remove once legacy fields are removed from schema
status: "UPLOADING",
format: "unknown",
- location: "REMOTE",
size: 0,
},
});
@@ -91,9 +104,11 @@ export const uploadFile = async (
) => {
const asset = await prisma.asset.findFirst({
where: {
- name: { equals: name },
- status: { equals: "UPLOADING" },
- createdAt: { gt: new Date(Date.now() - UPLOADING_STALE_TIMEOUT) },
+ name,
+ file: {
+ status: "UPLOADING",
+ createdAt: { gt: new Date(Date.now() - UPLOADING_STALE_TIMEOUT) },
+ },
},
});
if (asset === null) {
@@ -104,18 +119,29 @@ export const uploadFile = async (
const assetData = await client.uploadFile(request);
const { meta, format, location, size } = assetData;
const dbAsset = await prisma.asset.update({
+ select: {
+ file: true,
+ id: true,
+ projectId: true,
+ name: true,
+ location: true,
+ },
where: {
id_projectId: { id: asset.id, projectId: asset.projectId },
},
data: {
location,
- size,
- format,
- meta: JSON.stringify(meta),
- status: "UPLOADED",
+ file: {
+ update: {
+ size,
+ format,
+ meta: JSON.stringify(meta),
+ status: "UPLOADED",
+ },
+ },
},
});
- return formatAsset(dbAsset);
+ return formatAsset(dbAsset, dbAsset.file);
} catch (error) {
await prisma.asset.delete({
where: {
diff --git a/packages/asset-uploader/src/utils/format-asset.ts b/packages/asset-uploader/src/utils/format-asset.ts
index 577c13d205..1ecb8b3292 100644
--- a/packages/asset-uploader/src/utils/format-asset.ts
+++ b/packages/asset-uploader/src/utils/format-asset.ts
@@ -1,36 +1,48 @@
-import type { Asset as DbAsset } from "@webstudio-is/prisma-client";
+import type {
+ Asset as DbAsset,
+ File as DbFile,
+} from "@webstudio-is/prisma-client";
import { type FontFormat, FONT_FORMATS } from "@webstudio-is/fonts";
import { FontMeta } from "@webstudio-is/fonts";
import { type Asset, ImageMeta } from "../schema";
-export const formatAsset = (asset: DbAsset): Asset => {
- const isFont = FONT_FORMATS.has(asset.format as FontFormat);
+// @todo remove once legacy fields are removed from schema
+type DbAssetWithoutOldFields = Omit<
+ DbAsset,
+ "size" | "format" | "meta" | "status" | "description" | "createdAt"
+>;
+
+export const formatAsset = (
+ asset: DbAssetWithoutOldFields,
+ file: DbFile
+): Asset => {
+ const isFont = FONT_FORMATS.has(file.format as FontFormat);
if (isFont) {
return {
id: asset.id,
- name: asset.name,
- description: asset.description,
+ name: file.name,
+ description: file.description,
location: asset.location,
projectId: asset.projectId,
- size: asset.size,
+ size: file.size,
type: "font",
- createdAt: asset.createdAt.toISOString(),
- format: asset.format as FontFormat,
- meta: FontMeta.parse(JSON.parse(asset.meta)),
+ createdAt: file.createdAt.toISOString(),
+ format: file.format as FontFormat,
+ meta: FontMeta.parse(JSON.parse(file.meta)),
};
}
return {
id: asset.id,
- name: asset.name,
- description: asset.description,
+ name: file.name,
+ description: file.description,
location: asset.location,
projectId: asset.projectId,
- size: asset.size,
+ size: file.size,
type: "image",
- format: asset.format,
- createdAt: asset.createdAt.toISOString(),
- meta: ImageMeta.parse(JSON.parse(asset.meta)),
+ format: file.format,
+ createdAt: file.createdAt.toISOString(),
+ meta: ImageMeta.parse(JSON.parse(file.meta)),
};
};
diff --git a/packages/prisma-client/prisma/migrations/20230501151815_file/migration.sql b/packages/prisma-client/prisma/migrations/20230501151815_file/migration.sql
new file mode 100644
index 0000000000..99e84404be
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20230501151815_file/migration.sql
@@ -0,0 +1,12 @@
+-- CreateTable
+CREATE TABLE "File" (
+ "name" TEXT NOT NULL,
+ "format" TEXT NOT NULL,
+ "size" INTEGER NOT NULL,
+ "description" TEXT,
+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ "meta" TEXT NOT NULL DEFAULT '{}',
+ "status" "UploadStatus" NOT NULL DEFAULT 'UPLOADING',
+
+ CONSTRAINT "File_pkey" PRIMARY KEY ("name")
+);
diff --git a/packages/prisma-client/prisma/migrations/20230501151941_asset-to-file/migration.ts b/packages/prisma-client/prisma/migrations/20230501151941_asset-to-file/migration.ts
new file mode 100644
index 0000000000..b3277e9b8e
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20230501151941_asset-to-file/migration.ts
@@ -0,0 +1,43 @@
+import { PrismaClient } from "./client";
+
+export default () => {
+ const client = new PrismaClient({
+ // Uncomment to see the queries in console as the migration runs
+ // log: ["query", "info", "warn", "error"],
+ });
+ return client.$transaction(
+ async (prisma) => {
+ const result = await prisma.$executeRaw`
+ INSERT INTO "File"
+ (
+ "name",
+ "format",
+ "size",
+ "description",
+ "createdAt",
+ "meta",
+ "status"
+ )
+ SELECT
+ "name",
+ "format",
+ "size",
+ "description",
+ "createdAt",
+ "meta",
+ "status"
+ FROM "Asset"
+ GROUP BY
+ "name",
+ "format",
+ "size",
+ "description",
+ "createdAt",
+ "meta",
+ "status";
+ `;
+ console.info(`${result} files migrated from assets`);
+ },
+ { timeout: 1000 * 60 }
+ );
+};
diff --git a/packages/prisma-client/prisma/migrations/20230501151941_asset-to-file/schema.prisma b/packages/prisma-client/prisma/migrations/20230501151941_asset-to-file/schema.prisma
new file mode 100644
index 0000000000..855fd61989
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20230501151941_asset-to-file/schema.prisma
@@ -0,0 +1,142 @@
+// 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"]
+ // see commands.ts
+ 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 File {
+ name String @id
+ user User? @relation(fields: [userId], references: [id])
+ userId String?
+ format String
+ size Int
+ description String?
+ createdAt DateTime @default(now())
+ meta String @default("{}")
+ status UploadStatus @default(UPLOADING)
+}
+
+model Asset {
+ id String @default(uuid()) // not unique!
+ projectId String
+ format String
+ size Int
+ name String
+ description String?
+ location Location
+ createdAt DateTime @default(now())
+ meta String @default("{}")
+ status UploadStatus @default(UPLOADED)
+
+ @@id([id, projectId])
+}
+
+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[]
+ files File[]
+}
+
+model Project {
+ id String @id @default(uuid())
+ createdAt DateTime @default(now())
+ title String
+ domain String @unique
+ user User? @relation(fields: [userId], references: [id])
+ userId String?
+ build Build[]
+ isDeleted Boolean @default(false)
+
+ @@unique([id, isDeleted])
+ @@unique([domain, isDeleted])
+}
+
+model Build {
+ id String @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 String @default("[]")
+ styles String @default("[]")
+ styleSources String @default("[]")
+ styleSourceSelections String @default("[]")
+ props String @default("[]")
+ instances String @default("[]")
+
+ @@id([id, projectId])
+}
+
+model Breakpoints {
+ buildId String @id
+ values String @default("[]")
+}
+
+enum AuthorizationRelation {
+ viewers
+ editors
+ builders
+}
+
+model AuthorizationToken {
+ token String @default(uuid())
+ // No relation to Project, as the Authorization system is not tied to a project
+ projectId String
+ name String @default("")
+ relation AuthorizationRelation @default(viewers)
+ createdAt DateTime @default(now())
+
+ @@id([token, projectId])
+}
+
+// Dashboard
+view DashboardProject {
+ id String @id @default(uuid())
+ createdAt DateTime @default(now())
+ title String
+ domain String
+ userId String?
+ isDeleted Boolean @default(false)
+ isPublished Boolean
+}
diff --git a/packages/prisma-client/prisma/migrations/20230501153024_asset-file-relation/migration.sql b/packages/prisma-client/prisma/migrations/20230501153024_asset-file-relation/migration.sql
new file mode 100644
index 0000000000..cc980f05f7
--- /dev/null
+++ b/packages/prisma-client/prisma/migrations/20230501153024_asset-file-relation/migration.sql
@@ -0,0 +1,2 @@
+-- AddForeignKey
+ALTER TABLE "Asset" ADD CONSTRAINT "Asset_name_fkey" FOREIGN KEY ("name") REFERENCES "File"("name") ON DELETE RESTRICT ON UPDATE CASCADE;
diff --git a/packages/prisma-client/prisma/schema.prisma b/packages/prisma-client/prisma/schema.prisma
index e4af886017..3dc08d3cf3 100644
--- a/packages/prisma-client/prisma/schema.prisma
+++ b/packages/prisma-client/prisma/schema.prisma
@@ -30,14 +30,28 @@ enum UploadStatus {
UPLOADED
}
+model File {
+ name String @id
+ format String
+ size Int
+ description String?
+ createdAt DateTime @default(now())
+ meta String @default("{}")
+ status UploadStatus @default(UPLOADING)
+ assets Asset[]
+}
+
model Asset {
id String @default(uuid()) // not unique!
projectId String
+ file File @relation(fields: [name], references: [name])
+ name String
+ location Location
+
+ // @todo remove these fields from schema and db after release
format String
size Int
- name String
description String?
- location Location
createdAt DateTime @default(now())
meta String @default("{}")
status UploadStatus @default(UPLOADED)
diff --git a/packages/prisma-client/src/types.ts b/packages/prisma-client/src/types.ts
index 133ce56e99..e42ce23ea8 100644
--- a/packages/prisma-client/src/types.ts
+++ b/packages/prisma-client/src/types.ts
@@ -5,6 +5,7 @@ export type {
Build,
Project,
Asset,
+ File,
DashboardProject,
AuthorizationRelation,
AuthorizationToken,