diff --git a/server/models/NotificationSetting.js b/server/models/NotificationSetting.js index 69971d64..6e5be653 100644 --- a/server/models/NotificationSetting.js +++ b/server/models/NotificationSetting.js @@ -52,6 +52,7 @@ NotificationSetting.associate = models => { NotificationSetting.belongsTo(models.User, { as: 'user', foreignKey: 'userId', + onDelete: 'cascade', }); NotificationSetting.belongsTo(models.Team, { as: 'team', diff --git a/server/models/Revision.js b/server/models/Revision.js index c138cfec..fafcc61a 100644 --- a/server/models/Revision.js +++ b/server/models/Revision.js @@ -9,29 +9,13 @@ const Revision = sequelize.define('revision', { }, title: DataTypes.STRING, text: DataTypes.TEXT, - - userId: { - type: 'UUID', - allowNull: false, - references: { - model: 'users', - }, - }, - - documentId: { - type: 'UUID', - allowNull: false, - references: { - model: 'documents', - onDelete: 'CASCADE', - }, - }, }); Revision.associate = models => { Revision.belongsTo(models.Document, { as: 'document', foreignKey: 'documentId', + onDelete: 'cascade', }); Revision.belongsTo(models.User, { as: 'user', diff --git a/server/models/User.js b/server/models/User.js index d1f7acad..24231310 100644 --- a/server/models/User.js +++ b/server/models/User.js @@ -39,17 +39,16 @@ const User = sequelize.define( return !!this.suspendedAt; }, }, - indexes: [ - { - fields: ['email'], - }, - ], } ); // Class methods User.associate = models => { - User.hasMany(models.ApiKey, { as: 'apiKeys' }); + User.hasMany(models.ApiKey, { as: 'apiKeys', onDelete: 'cascade' }); + User.hasMany(models.NotificationSetting, { + as: 'notificationSettings', + onDelete: 'cascade', + }); User.hasMany(models.Document, { as: 'documents' }); User.hasMany(models.View, { as: 'views' }); }; @@ -93,9 +92,19 @@ const setRandomJwtSecret = model => { model.jwtSecret = crypto.randomBytes(64).toString('hex'); }; -const removeIdentifyingInfo = async model => { - await ApiKey.destroy({ where: { userId: model.id } }); - await Star.destroy({ where: { userId: model.id } }); +const removeIdentifyingInfo = async (model, options) => { + await NotificationSetting.destroy({ + where: { userId: model.id }, + transaction: options.transaction, + }); + await ApiKey.destroy({ + where: { userId: model.id }, + transaction: options.transaction, + }); + await Star.destroy({ + where: { userId: model.id }, + transaction: options.transaction, + }); model.email = ''; model.name = 'Unknown'; @@ -108,7 +117,7 @@ const removeIdentifyingInfo = async model => { // this shouldn't be needed once this issue is resolved: // https://github.com/sequelize/sequelize/issues/9318 - await model.save({ hooks: false }); + await model.save({ hooks: false, transaction: options.transaction }); }; const checkLastAdmin = async model => {