-
-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Better handle associations deletions (#10622)
- Loading branch information
Showing
6 changed files
with
105 additions
and
13 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
migrations/20250110102114-fix-deleted-expense-relationships.ts
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,54 @@ | ||
'use strict'; | ||
|
||
import logger from '../server/lib/logger'; | ||
|
||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
async up(queryInterface) { | ||
const [deletedComments] = await queryInterface.sequelize.query(` | ||
UPDATE "Comments" | ||
SET "deletedAt" = e."deletedAt" | ||
FROM "Expenses" e | ||
WHERE "Comments"."ExpenseId" IS NOT NULL | ||
AND "Comments"."ExpenseId" = e."id" | ||
AND e."deletedAt" IS NOT NULL | ||
AND "Comments"."deletedAt" IS NULL | ||
RETURNING e."id" | ||
`); | ||
|
||
const [deletedItems] = await queryInterface.sequelize.query(` | ||
UPDATE "ExpenseItems" | ||
SET "deletedAt" = e."deletedAt" | ||
FROM "Expenses" e | ||
WHERE "ExpenseItems"."ExpenseId" = e."id" | ||
AND e."deletedAt" IS NOT NULL | ||
AND "ExpenseItems"."deletedAt" IS NULL | ||
RETURNING e."id" | ||
`); | ||
|
||
logger.info(`Expenses: Fixed ${deletedComments.length} deleted comments, ${deletedItems.length} deleted items`); | ||
|
||
if (deletedComments.length === 0 || deletedItems.length === 0) { | ||
await queryInterface.sequelize.query( | ||
` | ||
INSERT INTO "MigrationLogs" | ||
("createdAt", "type", "description", "CreatedByUserId", "data") | ||
VALUES (NOW(), 'MIGRATION', 'Fix deleted expense relationships', NULL, :data) | ||
`, | ||
{ | ||
type: queryInterface.sequelize.QueryTypes.INSERT, | ||
replacements: { | ||
data: JSON.stringify({ | ||
deletedComments, | ||
deletedItems, | ||
}), | ||
}, | ||
}, | ||
); | ||
} | ||
}, | ||
|
||
async down() { | ||
console.log(`No rollback, check the MigrationLogs table for the data`); | ||
}, | ||
}; |
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
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
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