fix: Migrations should account for old rows

This commit is contained in:
Tom Moor
2022-04-03 20:16:09 -07:00
parent 84d6bf8ddf
commit b50c7beba3
2 changed files with 23 additions and 3 deletions

View File

@@ -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")
}
};

View File

@@ -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",