Remove NotificationSettings table (#5036
* helper * Add script to move notification settings * wip, removal of NotificationSettings * event name * iteration * test * test * Remove last of NotificationSettings model * refactor * More fixes * snapshots * Change emails to class instances for type safety * test * docs * Update migration for self-hosted * tsc
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
await queryInterface.sequelize.transaction(async (transaction) => {
|
||||
await queryInterface.addColumn("users", "notificationSettings", {
|
||||
type: Sequelize.JSONB,
|
||||
allowNull: false,
|
||||
defaultValue: {},
|
||||
transaction,
|
||||
});
|
||||
|
||||
if (process.env.DEPLOYMENT === "hosted") {
|
||||
return;
|
||||
}
|
||||
|
||||
// In cloud hosted Outline this migration is done in a script instead due
|
||||
// to scale considerations.
|
||||
const users = await queryInterface.sequelize.query(
|
||||
"SELECT id FROM users",
|
||||
{
|
||||
type: queryInterface.sequelize.QueryTypes.SELECT,
|
||||
transaction
|
||||
}
|
||||
);
|
||||
|
||||
for (const user of users) {
|
||||
const settings = await queryInterface.sequelize.query(
|
||||
`SELECT * FROM notification_settings WHERE "userId" = :userId`,
|
||||
{
|
||||
type: queryInterface.sequelize.QueryTypes.SELECT,
|
||||
replacements: { userId: user.id },
|
||||
transaction
|
||||
}
|
||||
);
|
||||
|
||||
const eventTypes = settings.map((setting) => setting.event);
|
||||
|
||||
|
||||
if (eventTypes.length > 0) {
|
||||
const notificationSettings = {};
|
||||
|
||||
for (const eventType of eventTypes) {
|
||||
notificationSettings[eventType] = true;
|
||||
}
|
||||
|
||||
await queryInterface.sequelize.query(
|
||||
`UPDATE users SET "notificationSettings" = :notificationSettings WHERE id = :userId`,
|
||||
{
|
||||
type: queryInterface.sequelize.QueryTypes.UPDATE,
|
||||
replacements: {
|
||||
userId: user.id,
|
||||
notificationSettings: JSON.stringify(notificationSettings),
|
||||
},
|
||||
transaction
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
async down (queryInterface) {
|
||||
return queryInterface.removeColumn("users", "notificationSettings");
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user