Skip to content

Commit

Permalink
fix(github-actions-usage): Delete and save results in a single transa…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
akash1810 committed Feb 24, 2024
1 parent 331500f commit 165452a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 9 additions & 2 deletions packages/github-actions-usage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export async function main() {
const now = new Date();
const data = await getWorkflows(prismaClient);
const recordsToSave = transform(data);
await prismaClient.guardian_github_actions_usage.deleteMany();
await saveResults(prismaClient, recordsToSave, now);

/*
Prisma performs `deleteMany` and `createMany` in separate transactions by default.
Here we manually wrap them in a single transaction to ensure that the delete and create operations are atomic.
*/
await prismaClient.$transaction([
prismaClient.guardian_github_actions_usage.deleteMany(),
saveResults(prismaClient, recordsToSave, now),
]);
}
4 changes: 2 additions & 2 deletions packages/github-actions-usage/src/query/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type UnsavedGithubActionUsage = Omit<
* Save records to the `guardian_github_actions_usage` table.
* Each record will receive the same timestamp in its `evaluated_on` column.
*/
export async function saveResults(
export function saveResults(
client: PrismaClient,
results: UnsavedGithubActionUsage[],
timestamp: Date,
Expand All @@ -24,5 +24,5 @@ export async function saveResults(
}));

console.log(`Saving ${records.length} guardian_github_actions_usage`);
await client.guardian_github_actions_usage.createMany({ data: records });
return client.guardian_github_actions_usage.createMany({ data: records });
}

0 comments on commit 165452a

Please sign in to comment.