Skip to content

Commit b686830

Browse files
author
Pedro Aim
committed
removing duplicated keys
1 parent cba5d1d commit b686830

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

electron_app/src/database/DBEmigrations/20200512111423_addBlockAllColumn.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,28 @@ const handleDuplicatedMessageIds = async (queryInterface, transaction) => {
4848
});
4949
await Email().destroy({
5050
where: {
51-
id: idsToDelete
51+
id: [].concat.apply([], idsToDelete)
52+
},
53+
transaction
54+
});
55+
};
56+
57+
const handleDuplicatedKeys = async (queryInterface, transaction) => {
58+
const query = `SELECT key, messageId, COUNT(*), GROUP_CONCAT(id) as 'ids' FROM ${
59+
Table.EMAIL
60+
} GROUP BY key HAVING COUNT(*) > 1 `;
61+
const [results] = await queryInterface.sequelize.query(query, {
62+
transaction
63+
});
64+
const idsToDelete = [];
65+
results.forEach(row => {
66+
const ids = row.ids.split(',');
67+
ids.shift();
68+
idsToDelete.push(ids);
69+
});
70+
await Email().destroy({
71+
where: {
72+
id: [].concat.apply([], idsToDelete)
5273
},
5374
transaction
5475
});
@@ -76,6 +97,7 @@ module.exports = {
7697

7798
try {
7899
await migrateBlockRemoteContent(queryInterface, Sequelize, transaction);
100+
await handleDuplicatedKeys(queryInterface, transaction);
79101
await handleDuplicatedMessageIds(queryInterface, transaction);
80102
await addUniqueIndexes(queryInterface, transaction);
81103
await transaction.commit();

0 commit comments

Comments
 (0)