Skip to content

Commit

Permalink
Use cte instead of transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
kalilsn committed May 7, 2024
1 parent ee4c985 commit c7cd5f5
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions core/actions/move/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,26 @@
import { logger } from "logger";

import type { action } from "./action";
import type { PubsId } from "~/kysely/types/public/Pubs";
import type { StagesId } from "~/kysely/types/public/Stages";
import { db } from "~/kysely/database";
import { PubsId } from "~/kysely/types/public/Pubs";
import { StagesId } from "~/kysely/types/public/Stages";
import { defineRun } from "../types";

export const run = defineRun<typeof action>(async ({ pub, config, stageId }) => {
try {
await db.transaction().execute(async (trx) => {
// Remove the pub from the current stage.
await trx
.deleteFrom("PubsInStages")
.where("pubId", "=", pub.id as PubsId)
.where("stageId", "=", stageId)
.execute();

// Add the pub to the destination stage.
return await trx
.insertInto("PubsInStages")
.values({
pubId: pub.id as PubsId,
stageId: config.stage as StagesId,
})
.execute();
});
await db
.with("leave-stage", (db) =>
db
.deleteFrom("PubsInStages")
.where("pubId", "=", pub.id as PubsId)
.where("stageId", "=", stageId)
)
.insertInto("PubsInStages")
.values({
pubId: pub.id as PubsId,
stageId: config.stage as StagesId,
})
.execute();
} catch (error) {
logger.error({ msg: "move", error });
return {
Expand Down

0 comments on commit c7cd5f5

Please sign in to comment.