Skip to content

Commit

Permalink
More efficient transfer course update
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Mar 24, 2024
1 parent 7dfc90d commit 4d8a392
Showing 1 changed file with 26 additions and 28 deletions.
54 changes: 26 additions & 28 deletions src/tasks/scrape-transfer-courses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,32 @@ export class ScrapeTransferCoursesTask {
this.logger.log(`Scraped ${extensionTransferCourses.length} transfer courses (${extensionUniqueTransferCourses.length} unique ones)...`);

const startedUpdatingAt = new Date();
await db.serializable(this.pool, async trx => {
// Batch updates
for (let i = 0; i <= extensionUniqueTransferCourses.length; i += 100) {
// eslint-disable-next-line no-await-in-loop
await db.upsert(
'TransferCourse',
extensionUniqueTransferCourses.slice(i, i + 100).map(course => ({
fromCollege: course.from.college,
fromCollegeState: course.from.state,
fromCRSE: course.from.crse,
fromCredits: course.from.credits,
fromSubject: course.from.subject,
toCRSE: course.to.crse,
toCredits: course.to.credits,
toSubject: course.to.subject,
title: course.to.title,
updatedAt: new Date(),
})),
['fromCollege', 'fromCRSE', 'fromSubject', 'toCRSE', 'toSubject', 'toCredits'],
{
updateValues: updateUpdatedAtForUpsert('TransferCourse', ['fromCredits', 'toCredits', 'title', 'updatedAt']),
}
).run(trx);
}
// Batch updates
for (let i = 0; i <= extensionUniqueTransferCourses.length; i += 100) {
// eslint-disable-next-line no-await-in-loop
await db.upsert(
'TransferCourse',
extensionUniqueTransferCourses.slice(i, i + 100).map(course => ({
fromCollege: course.from.college,
fromCollegeState: course.from.state,
fromCRSE: course.from.crse,
fromCredits: course.from.credits,
fromSubject: course.from.subject,
toCRSE: course.to.crse,
toCredits: course.to.credits,
toSubject: course.to.subject,
title: course.to.title,
updatedAt: new Date(),
})),
['fromCollege', 'fromCRSE', 'fromSubject', 'toCRSE', 'toSubject', 'toCredits'],
{
updateValues: updateUpdatedAtForUpsert('TransferCourse', ['fromCredits', 'toCredits', 'title', 'updatedAt']),
}
).run(this.pool);
}

await db.deletes('TransferCourse', {
updatedAt: db.conditions.lt(startedUpdatingAt),
}).run(trx);
});
await db.deletes('TransferCourse', {
updatedAt: db.conditions.lt(startedUpdatingAt),
}).run(this.pool);
}
}

0 comments on commit 4d8a392

Please sign in to comment.