-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a11eb2
commit 0c073e4
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
connectors/migrations/20230906_2_slack_fill_parents_field.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,61 @@ | ||
import { updateDocumentParentsField } from "@connectors/lib/data_sources"; | ||
import { Connector, SlackMessages } from "@connectors/lib/models"; | ||
|
||
async function main() { | ||
// if first arg is "all", update all connectors, else update only the | ||
// connector for the corresponding workspace id | ||
const connectors = | ||
process.argv[2] === "all" | ||
? await Connector.findAll({ | ||
where: { | ||
type: "slack", | ||
}, | ||
}) | ||
: await Connector.findAll({ | ||
where: { | ||
type: "slack", | ||
workspaceId: process.argv[2], | ||
}, | ||
}); | ||
|
||
for (const connector of connectors) { | ||
console.log(`Updating parents field for connector ${connector.id}`); | ||
await updateParentsFieldForConnector(connector); | ||
} | ||
} | ||
|
||
async function updateParentsFieldForConnector(connector: Connector) { | ||
// get all distinct documentIds and their channel ids from slack messages in | ||
// this connector | ||
const documentIdsAndChannels = await SlackMessages.findAll({ | ||
where: { | ||
connectorId: connector.id, | ||
}, | ||
attributes: ["documentId", "channelId"], | ||
group: ["documentId", "channelId"], | ||
}); | ||
// update all parents fields for all pages and databases by chunks of 128 | ||
const chunkSize = 128; | ||
for (let i = 0; i < documentIdsAndChannels.length; i += chunkSize) { | ||
const chunk = documentIdsAndChannels.slice(i, i + chunkSize); | ||
console.log(`Updating ${chunk.length} documents`); | ||
// update parents field for each document of the chunk, in parallel | ||
await Promise.all( | ||
chunk.map((documentIdAndChannel) => | ||
updateDocumentParentsField(connector, documentIdAndChannel.documentId, [ | ||
documentIdAndChannel.channelId, | ||
]) | ||
) | ||
); | ||
} | ||
} | ||
|
||
main() | ||
.then(() => { | ||
console.log("Done"); | ||
process.exit(0); | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
process.exit(1); | ||
}); |