fix (backend): prevent chat messages from being deleted when attached file is deleted

This commit is contained in:
naskya 2024-04-12 02:26:00 +09:00
parent 6eee5c651c
commit 24f79d4796
No known key found for this signature in database
GPG Key ID: 712D413B3A9FED5C
4 changed files with 28 additions and 2 deletions

View File

@ -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;

View File

@ -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(

View File

@ -0,0 +1,21 @@
import type { MigrationInterface, QueryRunner } from "typeorm";
export class FixChatFileConstraint1712855579316 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
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`,
);
}
}

View File

@ -97,7 +97,7 @@ export class MessagingMessage {
public group: Relation<UserGroup | null>;
@ManyToOne(() => DriveFile, {
onDelete: "CASCADE", // TODO: change this to SET NULL
onDelete: "SET NULL",
nullable: true,
})
@JoinColumn()