Files
outline/server/migrations/20230314013103-move-notification-settings.js
Tom Moor 45831e9469 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
2023-03-18 06:32:41 -07:00

67 lines
1.9 KiB
JavaScript

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