-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid duplicate slack message, cleanup script and batch delete (#9331)
- Loading branch information
Showing
2 changed files
with
119 additions
and
11 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
connectors/migrations/20241212_clean_slack_messages_duplicates.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { makeScript } from "scripts/helpers"; | ||
import { QueryTypes, Sequelize } from "sequelize"; | ||
|
||
const { CONNECTORS_DATABASE_URI } = process.env; | ||
|
||
makeScript({}, async ({ execute }) => { | ||
const sequelize = new Sequelize(CONNECTORS_DATABASE_URI as string, { | ||
logging: false, | ||
}); | ||
|
||
// Select distinct connectorId on slack_messages | ||
const connectorIds = ( | ||
await sequelize.query<{ connectorId: number }>( | ||
'SELECT DISTINCT "connectorId" FROM slack_messages', | ||
{ | ||
type: QueryTypes.SELECT, | ||
} | ||
) | ||
).map((c) => c.connectorId); | ||
|
||
for (const connectorId of connectorIds) { | ||
const duplicates = await sequelize.query<{ | ||
min_id: string; | ||
documentId: string; | ||
total: number; | ||
}>( | ||
'SELECT min(id) as min_id, "documentId", count(*) as total FROM slack_messages WHERE "connectorId" = $1 GROUP BY "documentId" HAVING count(*) > 1', | ||
{ | ||
type: QueryTypes.SELECT, | ||
bind: [connectorId], | ||
} | ||
); | ||
|
||
if (duplicates.length > 0) { | ||
console.log( | ||
`${duplicates.length} duplicates slack messages for connector ${connectorId}` | ||
); | ||
|
||
for (const { min_id, documentId, total } of duplicates) { | ||
const deleteQuery = `DELETE FROM slack_messages WHERE id > $1 AND "documentId" = $2`; | ||
if (execute) { | ||
await sequelize.query(deleteQuery, { | ||
bind: [Number(min_id), documentId], | ||
type: QueryTypes.DELETE, | ||
}); | ||
} else { | ||
const countQuery = `SELECT count(*) as count FROM slack_messages WHERE id > $1 AND "documentId" = $2`; | ||
const counts = await sequelize.query<{ count: number }>(countQuery, { | ||
bind: [Number(min_id), documentId], | ||
type: QueryTypes.SELECT, | ||
}); | ||
if (!counts[0]) { | ||
throw new Error(`No results for ${countQuery}`); | ||
} | ||
if (counts[0].count != total - 1) { | ||
throw new Error( | ||
`Expected to delete ${total - 1} but would deleted ${counts[0].count}` | ||
); | ||
} else { | ||
console.log(`OK: Would delete ${counts[0].count} slack messages.`); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters