From 2a52bb94ad8ae65b8e3f0778370d6cb72ed1665b Mon Sep 17 00:00:00 2001 From: Lars Yencken Date: Wed, 31 Jul 2024 10:54:23 +0100 Subject: [PATCH 1/2] :hammer: Make more db relationships cascade on delete for charts Previously, if you tried to delete a chart in the DB, it would fail if there were any linked rows in `suggested_chart_revisions` (some charts) or `chart_dimensions` (all charts). With this migration, both of those relationships cascade on delete, meaning that deleting a chart will succeed even if there are associated chart dimension rows and suggested revisions. However, other relationships, such as usage in explorers, will still prevent deletion. --- ...22418726999-SuggesteRevisionsFkRelaxing.ts | 77 +++++++++++++++++++ ...19367802-ChartDimensionsOnDeleteCascade.ts | 31 ++++++++ 2 files changed, 108 insertions(+) create mode 100644 db/migration/1722418726999-SuggesteRevisionsFkRelaxing.ts create mode 100644 db/migration/1722419367802-ChartDimensionsOnDeleteCascade.ts diff --git a/db/migration/1722418726999-SuggesteRevisionsFkRelaxing.ts b/db/migration/1722418726999-SuggesteRevisionsFkRelaxing.ts new file mode 100644 index 00000000000..a277357c47f --- /dev/null +++ b/db/migration/1722418726999-SuggesteRevisionsFkRelaxing.ts @@ -0,0 +1,77 @@ +import { MigrationInterface, QueryRunner } from "typeorm" + +export class SuggestedRevisionsFkRelaxing1722418726999 + implements MigrationInterface +{ + public async up(queryRunner: QueryRunner): Promise { + // Change: ON DELETE RESTRICT -> ON DELETE CASCADE + // + // This means that if a chart or user is deleted, then any + // linked suggested revisions are deleted too. + await queryRunner.query(`-- sql + ALTER TABLE suggested_chart_revisions + DROP FOREIGN KEY suggested_chart_revisions_ibfk_1 + `) + await queryRunner.query(`-- sql + ALTER TABLE suggested_chart_revisions + ADD CONSTRAINT suggested_chart_revisions_ibfk_1 + FOREIGN KEY (chartId) REFERENCES charts(id) + ON DELETE CASCADE ON UPDATE RESTRICT; + `) + await queryRunner.query(`-- sql + ALTER TABLE suggested_chart_revisions + DROP FOREIGN KEY suggested_chart_revisions_ibfk_2 + `) + await queryRunner.query(`-- sql + ALTER TABLE suggested_chart_revisions + ADD CONSTRAINT suggested_chart_revisions_ibfk_2 + FOREIGN KEY (createdBy) REFERENCES users(id) + ON DELETE CASCADE ON UPDATE RESTRICT; + `) + await queryRunner.query(`-- sql + ALTER TABLE suggested_chart_revisions + DROP FOREIGN KEY suggested_chart_revisions_ibfk_3 + `) + await queryRunner.query(`-- sql + ALTER TABLE suggested_chart_revisions + ADD CONSTRAINT suggested_chart_revisions_ibfk_3 + FOREIGN KEY (updatedBy) + REFERENCES users(id) + ON DELETE CASCADE ON UPDATE RESTRICT; + `) + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`-- sql + ALTER TABLE suggested_chart_revisions + DROP FOREIGN KEY suggested_chart_revisions_ibfk_1 + `) + await queryRunner.query(`-- sql + ALTER TABLE suggested_chart_revisions + ADD CONSTRAINT suggested_chart_revisions_ibfk_1 + FOREIGN KEY (chartId) REFERENCES charts(id) + ON DELETE RESTRICT ON UPDATE RESTRICT; + `) + await queryRunner.query(`-- sql + ALTER TABLE suggested_chart_revisions + DROP FOREIGN KEY suggested_chart_revisions_ibfk_2 + `) + await queryRunner.query(`-- sql + ALTER TABLE suggested_chart_revisions + ADD CONSTRAINT suggested_chart_revisions_ibfk_2 + FOREIGN KEY (createdBy) REFERENCES users(id) + ON DELETE RESTRICT ON UPDATE RESTRICT; + `) + await queryRunner.query(`-- sql + ALTER TABLE suggested_chart_revisions + DROP FOREIGN KEY suggested_chart_revisions_ibfk_3 + `) + await queryRunner.query(`-- sql + ALTER TABLE suggested_chart_revisions + ADD CONSTRAINT suggested_chart_revisions_ibfk_3 + FOREIGN KEY (updatedBy) + REFERENCES users(id) + ON DELETE RESTRICT ON UPDATE RESTRICT; + `) + } +} diff --git a/db/migration/1722419367802-ChartDimensionsOnDeleteCascade.ts b/db/migration/1722419367802-ChartDimensionsOnDeleteCascade.ts new file mode 100644 index 00000000000..71cd7369ffc --- /dev/null +++ b/db/migration/1722419367802-ChartDimensionsOnDeleteCascade.ts @@ -0,0 +1,31 @@ +import { MigrationInterface, QueryRunner } from "typeorm" + +export class ChartDimensionsOnDeleteCascade1722419367802 + implements MigrationInterface +{ + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`-- sql + ALTER TABLE chart_dimensions + DROP FOREIGN KEY chart_dimensions_chartId_78d6a092_fk_charts_id + `) + await queryRunner.query(`-- sql + ALTER TABLE chart_dimensions + ADD CONSTRAINT chart_dimensions_chartId_78d6a092_fk_charts_id + FOREIGN KEY (chartId) REFERENCES charts(id) + ON DELETE CASCADE ON UPDATE RESTRICT; + `) + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`-- sql + ALTER TABLE chart_dimensions + DROP FOREIGN KEY chart_dimensions_chartId_78d6a092_fk_charts_id + `) + await queryRunner.query(`-- sql + ALTER TABLE chart_dimensions + ADD CONSTRAINT chart_dimensions_chartId_78d6a092_fk_charts_id + FOREIGN KEY (chartId) REFERENCES charts(id) + ON DELETE RESTRICT ON UPDATE RESTRICT; + `) + } +} From 42499110d254a2555c112ecb93e9dc5577c5793a Mon Sep 17 00:00:00 2001 From: Lars Yencken Date: Wed, 31 Jul 2024 16:23:30 +0100 Subject: [PATCH 2/2] :bug: Fix typo in migration name --- ...kRelaxing.ts => 1722418726999-SuggestedRevisionsFkRelaxing.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename db/migration/{1722418726999-SuggesteRevisionsFkRelaxing.ts => 1722418726999-SuggestedRevisionsFkRelaxing.ts} (100%) diff --git a/db/migration/1722418726999-SuggesteRevisionsFkRelaxing.ts b/db/migration/1722418726999-SuggestedRevisionsFkRelaxing.ts similarity index 100% rename from db/migration/1722418726999-SuggesteRevisionsFkRelaxing.ts rename to db/migration/1722418726999-SuggestedRevisionsFkRelaxing.ts