-
Notifications
You must be signed in to change notification settings - Fork 714
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move project cloning into sql function
Our project cloning does a lot of roundtrips to database and got fragmented across 3 packages. The logic is too complicated to represent with postgrest. Here written single sql function which is invoked safely with postgrest rpc.
- Loading branch information
Showing
7 changed files
with
95 additions
and
133 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export * from "./load"; | ||
export * from "./clone"; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
packages/prisma-client/prisma/migrations/20240725003228_clone_project/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
DROP FUNCTION IF EXISTS clone_project; | ||
CREATE FUNCTION clone_project( | ||
project_id text, | ||
user_id text, | ||
title text, | ||
domain text | ||
) RETURNS "Project" AS $$ | ||
DECLARE | ||
old_project "Project"; | ||
new_project "Project"; | ||
BEGIN | ||
SELECT * FROM "Project" WHERE id=project_id INTO old_project; | ||
|
||
INSERT INTO "Project" ( | ||
id, | ||
"userId", | ||
title, | ||
domain, | ||
"previewImageAssetId" | ||
) | ||
VALUES ( | ||
extensions.uuid_generate_v4(), | ||
user_id, | ||
title, | ||
domain, | ||
old_project."previewImageAssetId" | ||
) | ||
RETURNING * INTO new_project; | ||
|
||
INSERT INTO "Asset" (id, name, "projectId") | ||
SELECT asset.id, asset.name, new_project.id AS "projectId" | ||
FROM "Asset" AS asset, "File" AS file | ||
WHERE | ||
asset.name = file.name AND | ||
file.status = 'UPLOADED' AND | ||
asset."projectId" = old_project.id; | ||
|
||
INSERT INTO "Build" ( | ||
id, | ||
"projectId", | ||
pages, | ||
"styleSources", | ||
"styleSourceSelections", | ||
styles, | ||
breakpoints, | ||
props, | ||
instances, | ||
"dataSources", | ||
resources | ||
) | ||
SELECT | ||
extensions.uuid_generate_v4() AS id, | ||
new_project.id AS "projectId", | ||
pages, | ||
"styleSources", | ||
"styleSourceSelections", | ||
styles, | ||
breakpoints, | ||
props, | ||
instances, | ||
"dataSources", | ||
resources | ||
FROM "Build" | ||
WHERE "projectId" = old_project.id AND deployment IS NULL; | ||
|
||
RETURN new_project; | ||
END; | ||
$$ LANGUAGE plpgsql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters