From 69d85eadfdf65d74eedb51cd81e2f4911d7377fc Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sat, 1 Jun 2024 11:08:36 +0200 Subject: [PATCH] Feature/setup cascading on delete for various relations in database schema (#3445) * Setup cascading on delete * Update changelog --- CHANGELOG.md | 2 + .../migration.sql | 53 +++++++++++++++++++ prisma/schema.prisma | 18 +++---- 3 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 prisma/migrations/20240601083002_added_cascade_on_delete_for_various_relations/migration.sql diff --git a/CHANGELOG.md b/CHANGELOG.md index 274071d21..9a09ee3dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,11 +10,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Added the data provider information to the asset profile details dialog of the admin control +- Added the cascading on delete for various relations in the database schema ### Fixed - Fixed an issue with the initial annual interest rate in the _FIRE_ calculator - Fixed the state handling in the currency selector +- Fixed the deletion of an asset profile with symbol profile overrides in the asset profile details dialog of the admin control ## 2.83.0 - 2024-05-30 diff --git a/prisma/migrations/20240601083002_added_cascade_on_delete_for_various_relations/migration.sql b/prisma/migrations/20240601083002_added_cascade_on_delete_for_various_relations/migration.sql new file mode 100644 index 000000000..46b08f606 --- /dev/null +++ b/prisma/migrations/20240601083002_added_cascade_on_delete_for_various_relations/migration.sql @@ -0,0 +1,53 @@ +-- DropForeignKey +ALTER TABLE "Access" DROP CONSTRAINT "Access_granteeUserId_fkey"; + +-- DropForeignKey +ALTER TABLE "Access" DROP CONSTRAINT "Access_userId_fkey"; + +-- DropForeignKey +ALTER TABLE "Account" DROP CONSTRAINT "Account_userId_fkey"; + +-- DropForeignKey +ALTER TABLE "Analytics" DROP CONSTRAINT "Analytics_userId_fkey"; + +-- DropForeignKey +ALTER TABLE "AuthDevice" DROP CONSTRAINT "AuthDevice_userId_fkey"; + +-- DropForeignKey +ALTER TABLE "Order" DROP CONSTRAINT "Order_userId_fkey"; + +-- DropForeignKey +ALTER TABLE "Settings" DROP CONSTRAINT "Settings_userId_fkey"; + +-- DropForeignKey +ALTER TABLE "Subscription" DROP CONSTRAINT "Subscription_userId_fkey"; + +-- DropForeignKey +ALTER TABLE "SymbolProfileOverrides" DROP CONSTRAINT "SymbolProfileOverrides_symbolProfileId_fkey"; + +-- AddForeignKey +ALTER TABLE "Access" ADD CONSTRAINT "Access_granteeUserId_fkey" FOREIGN KEY ("granteeUserId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Access" ADD CONSTRAINT "Access_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Account" ADD CONSTRAINT "Account_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Analytics" ADD CONSTRAINT "Analytics_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "AuthDevice" ADD CONSTRAINT "AuthDevice_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Order" ADD CONSTRAINT "Order_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Settings" ADD CONSTRAINT "Settings_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "SymbolProfileOverrides" ADD CONSTRAINT "SymbolProfileOverrides_symbolProfileId_fkey" FOREIGN KEY ("symbolProfileId") REFERENCES "SymbolProfile"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Subscription" ADD CONSTRAINT "Subscription_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 43052de11..6293cfc18 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -17,8 +17,8 @@ model Access { permissions AccessPermission[] @default([READ_RESTRICTED]) updatedAt DateTime @updatedAt userId String - GranteeUser User? @relation("accessGet", fields: [granteeUserId], references: [id]) - User User @relation("accessGive", fields: [userId], references: [id]) + GranteeUser User? @relation("accessGet", fields: [granteeUserId], onDelete: Cascade, references: [id]) + User User @relation("accessGive", fields: [userId], onDelete: Cascade, references: [id]) @@index([alias]) @@index([granteeUserId]) @@ -38,7 +38,7 @@ model Account { updatedAt DateTime @updatedAt userId String Platform Platform? @relation(fields: [platformId], references: [id]) - User User @relation(fields: [userId], references: [id]) + User User @relation(fields: [userId], onDelete: Cascade, references: [id]) Order Order[] @@id([id, userId]) @@ -69,7 +69,7 @@ model Analytics { country String? updatedAt DateTime @updatedAt userId String @id - User User @relation(fields: [userId], references: [id]) + User User @relation(fields: [userId], onDelete: Cascade, references: [id]) @@index([updatedAt]) } @@ -82,7 +82,7 @@ model AuthDevice { id String @id @default(uuid()) updatedAt DateTime @updatedAt userId String - User User @relation(fields: [userId], references: [id]) + User User @relation(fields: [userId], onDelete: Cascade, references: [id]) @@index([userId]) } @@ -123,7 +123,7 @@ model Order { userId String Account Account? @relation(fields: [accountId, accountUserId], references: [id, userId]) SymbolProfile SymbolProfile @relation(fields: [symbolProfileId], references: [id]) - User User @relation(fields: [userId], references: [id]) + User User @relation(fields: [userId], onDelete: Cascade, references: [id]) tags Tag[] @@index([accountId]) @@ -150,7 +150,7 @@ model Settings { settings Json? updatedAt DateTime @updatedAt userId String @id - User User @relation(fields: [userId], references: [id]) + User User @relation(fields: [userId], onDelete: Cascade, references: [id]) } model SymbolProfile { @@ -194,7 +194,7 @@ model SymbolProfileOverrides { url String? symbolProfileId String @id updatedAt DateTime @updatedAt - SymbolProfile SymbolProfile @relation(fields: [symbolProfileId], references: [id]) + SymbolProfile SymbolProfile @relation(fields: [symbolProfileId], onDelete: Cascade, references: [id]) } model Subscription { @@ -204,7 +204,7 @@ model Subscription { price Float? updatedAt DateTime @updatedAt userId String - User User @relation(fields: [userId], references: [id]) + User User @relation(fields: [userId], onDelete: Cascade, references: [id]) @@index([userId]) }