From 24f79d47969273b33e69b96ec223819190188fc6 Mon Sep 17 00:00:00 2001 From: naskya Date: Fri, 12 Apr 2024 02:26:00 +0900 Subject: [PATCH] fix (backend): prevent chat messages from being deleted when attached file is deleted --- docs/downgrade.sql | 5 +++++ .../src/model/entity/messaging_message.rs | 2 +- .../1712855579316-fix-chat-file-constraint.ts | 21 +++++++++++++++++++ .../src/models/entities/messaging-message.ts | 2 +- 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 packages/backend/src/migration/1712855579316-fix-chat-file-constraint.ts diff --git a/docs/downgrade.sql b/docs/downgrade.sql index af95163abc..0b6f25e08c 100644 --- a/docs/downgrade.sql +++ b/docs/downgrade.sql @@ -1,6 +1,7 @@ BEGIN; DELETE FROM "migrations" WHERE name IN ( + 'FixChatFileConstraint1712855579316', 'DropTimeZone1712425488543', 'ExpandNoteEdit1711936358554', 'markLocalFilesNsfwByDefault1709305200000', @@ -21,6 +22,10 @@ DELETE FROM "migrations" WHERE name IN ( 'RemoveNativeUtilsMigration1705877093218' ); +-- fix-chat-file-constraint +ALTER TABLE "messaging_message" DROP CONSTRAINT "FK_535def119223ac05ad3fa9ef64b"; +ALTER TABLE "messaging_message" ADD CONSTRAINT "FK_535def119223ac05ad3fa9ef64b" FOREIGN KEY ("fileId") REFERENCES "drive_file"("id") ON DELETE CASCADE ON UPDATE NO ACTION; + -- drop-time-zone ALTER TABLE "abuse_user_report" ALTER "createdAt" TYPE timestamp with time zone; ALTER TABLE "access_token" ALTER "createdAt" TYPE timestamp with time zone; diff --git a/packages/backend-rs/src/model/entity/messaging_message.rs b/packages/backend-rs/src/model/entity/messaging_message.rs index be7c0632bc..304f876d1a 100644 --- a/packages/backend-rs/src/model/entity/messaging_message.rs +++ b/packages/backend-rs/src/model/entity/messaging_message.rs @@ -35,7 +35,7 @@ pub enum Relation { from = "Column::FileId", to = "super::drive_file::Column::Id", on_update = "NoAction", - on_delete = "Cascade" + on_delete = "SetNull" )] DriveFile, #[sea_orm( diff --git a/packages/backend/src/migration/1712855579316-fix-chat-file-constraint.ts b/packages/backend/src/migration/1712855579316-fix-chat-file-constraint.ts new file mode 100644 index 0000000000..98f10d2bb4 --- /dev/null +++ b/packages/backend/src/migration/1712855579316-fix-chat-file-constraint.ts @@ -0,0 +1,21 @@ +import type { MigrationInterface, QueryRunner } from "typeorm"; + +export class FixChatFileConstraint1712855579316 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "messaging_message" DROP CONSTRAINT "FK_535def119223ac05ad3fa9ef64b"`, + ); + await queryRunner.query( + `ALTER TABLE "messaging_message" ADD CONSTRAINT "FK_535def119223ac05ad3fa9ef64b" FOREIGN KEY ("fileId") REFERENCES "drive_file"("id") ON DELETE SET NULL ON UPDATE NO ACTION`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "messaging_message" DROP CONSTRAINT "FK_535def119223ac05ad3fa9ef64b"`, + ); + await queryRunner.query( + `ALTER TABLE "messaging_message" ADD CONSTRAINT "FK_535def119223ac05ad3fa9ef64b" FOREIGN KEY ("fileId") REFERENCES "drive_file"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + } +} diff --git a/packages/backend/src/models/entities/messaging-message.ts b/packages/backend/src/models/entities/messaging-message.ts index 1e87a0dc5c..5c9c17a027 100644 --- a/packages/backend/src/models/entities/messaging-message.ts +++ b/packages/backend/src/models/entities/messaging-message.ts @@ -97,7 +97,7 @@ export class MessagingMessage { public group: Relation; @ManyToOne(() => DriveFile, { - onDelete: "CASCADE", // TODO: change this to SET NULL + onDelete: "SET NULL", nullable: true, }) @JoinColumn()