diff --git a/server/migrations/20220402032204-starred-collections.js b/server/migrations/20220402032204-starred-collections.js index 31bab6e4e..451e5fa77 100644 --- a/server/migrations/20220402032204-starred-collections.js +++ b/server/migrations/20220402032204-starred-collections.js @@ -2,6 +2,18 @@ module.exports = { up: async (queryInterface, Sequelize) => { + // Previously there was no onDelete cascade so there may be existing stars + // in the db that reference documents that no longer exist. We must clean + // these up first before applying the new constraint. + await queryInterface.sequelize.query(` + DELETE FROM stars + WHERE NOT EXISTS ( + SELECT NULL + FROM documents doc + WHERE doc.id = "documentId" + ) + `); + await queryInterface.addColumn("stars", "collectionId", { type: Sequelize.UUID, allowNull: true, @@ -15,6 +27,7 @@ module.exports = { }); await queryInterface.changeColumn("stars", "documentId", { type: Sequelize.UUID, + onDelete: "cascade", references: { model: "documents", }, @@ -22,13 +35,20 @@ module.exports = { await queryInterface.changeColumn("stars", "userId", { type: Sequelize.UUID, allowNull: false, + onDelete: "cascade", references: { model: "users", }, }); }, - down: async (queryInterface) => { + down: async (queryInterface, Sequelize) => { await queryInterface.removeColumn("stars", "collectionId"); + await queryInterface.changeColumn("stars", "documentId", { + type: Sequelize.UUID, + allowNull: false + }); + await queryInterface.removeConstraint("stars", "stars_documentId_fkey") + await queryInterface.removeConstraint("stars", "stars_userId_fkey") } }; diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index a248f7af1..4b297114d 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -138,13 +138,13 @@ "Documents": "Documents", "Logo": "Logo", "Document archived": "Document archived", - "Empty": "Empty", "Move document": "Move document", - "Collections": "Collections", "You can't reorder documents in an alphabetically sorted collection": "You can't reorder documents in an alphabetically sorted collection", + "Collections": "Collections", "Untitled": "Untitled", "New nested document": "New nested document", "Document not supported – try Markdown, Plain text, HTML, or Word": "Document not supported – try Markdown, Plain text, HTML, or Word", + "Empty": "Empty", "Starred documents could not be loaded": "Starred documents could not be loaded", "Starred": "Starred", "Show more": "Show more",