Make batch transactions actually work

This commit is contained in:
Regalijan 2024-11-20 16:50:38 -05:00
parent 0829e66645
commit c9dcbb3629
Signed by: regalijan
GPG Key ID: 5D4196DA269EF520
3 changed files with 31 additions and 66 deletions

View File

@ -1,37 +1,25 @@
import { jsonError } from "../../../../common.js"; import { jsonError } from "../../../../common.js";
export async function onRequestPost(context: RequestContext) { export async function onRequestPost(context: RequestContext) {
if (context.data.event.reached_minimum_player_count) const { event } = context.data;
if (event.reached_minimum_player_count)
return jsonError("This event is already certified", 400); return jsonError("This event is already certified", 400);
const { D1 } = context.env; const { D1 } = context.env;
await D1.exec("BEGIN TRANSACTION;");
try { try {
const row = await D1.prepare( await D1.batch([
"UPDATE events SET reached_minimum_player_count = 1 WHERE id = ? RETURNING created_by;", D1.prepare(
) "UPDATE events SET reached_minimum_player_count = 1 WHERE id = ?",
.bind(context.data.event.id) ).bind(event.id),
.first(); D1.prepare(
"UPDATE et_members SET points = points + 10 WHERE id = ?;",
if (!row) { ).bind(event.created_by),
await D1.exec("ROLLBACK;"); ]);
return jsonError("Event does not exist", 404);
}
await D1.prepare("UPDATE et_members SET points = points + 10 WHERE id = ?;")
.bind(row.created_by)
.run();
await D1.exec("COMMIT;");
} catch (e) { } catch (e) {
console.log(e); console.log(e);
await D1.exec("ROLLBACK;");
return jsonError( return jsonError("Failed to complete batch transaction", 500);
"Failed to certify event because the event or points could not be updated",
500,
);
} }
return new Response(null, { return new Response(null, {

View File

@ -1,33 +1,22 @@
import { jsonError } from "../../../../common.js"; import { jsonError } from "../../../../common.js";
export async function onRequestPost(context: RequestContext) { export async function onRequestPost(context: RequestContext) {
const id = context.params.id as string;
const { D1 } = context.env; const { D1 } = context.env;
const { event } = context.data;
await D1.exec("BEGIN TRANSACTION;");
try { try {
const row = await D1.prepare( await D1.batch([
"UPDATE events SET performed_at = ? WHERE id = ? RETURNING created_by;", D1.prepare("UPDATE events SET performed_at = ? WHERE id = ?;").bind(
) event.id,
.bind(Date.now(), id) ),
.first(); D1.prepare(
"UPDATE et_members SET points = points + 10 WHERE id = ?;",
if (!row) { ).bind(event.created_by),
await D1.exec("ROLLBACK;"); ]);
return jsonError("Event does not exist", 404);
}
await D1.prepare("UPDATE et_members SET points = points + 10 WHERE id = ?;")
.bind(row.created_by)
.run();
await D1.exec("COMMIT;");
} catch (e) { } catch (e) {
console.log(e); console.log(e);
await D1.exec("ROLLBACK;");
return jsonError("Failed to update event data", 500); return jsonError("Failed to complete batch transaction", 500);
} }
return new Response(null, { return new Response(null, {

View File

@ -1,34 +1,22 @@
import { jsonError } from "../../../../common.js"; import { jsonError } from "../../../../common.js";
export async function onRequestPost(context: RequestContext) { export async function onRequestPost(context: RequestContext) {
const id = context.params.id as string;
const { D1 } = context.env; const { D1 } = context.env;
const { event } = context.data;
await D1.exec("BEGIN TRANSACTION;");
try { try {
const row = await D1.prepare( await D1.batch([
"UPDATE events SET performed_at = 0 WHERE id = ? RETURNING created_by;", D1.prepare("UPDATE events SET performed_at = 0 WHERE id = ?;").bind(
) event.id,
.bind(id) ),
.first(); D1.prepare(
"UPDATE et_members SET points = points - 5 WHERE id = ?;",
if (!row) { ).bind(event.created_by),
await D1.exec("ROLLBACK;"); ]);
return jsonError("Nonexistent event cannot be marked as forgotten.", 404);
}
await D1.prepare("UPDATE et_members SET points = points - 5 WHERE id = ?;")
.bind(row.created_by)
.run();
} catch (e) { } catch (e) {
console.log(e); console.log(e);
await D1.exec("ROLLBACK;");
return jsonError( return jsonError("Failed to complete batch transaction", 500);
"Failed to mark event as forgotten, points were not changed.",
500,
);
} }
return new Response(null, { return new Response(null, {