Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TransactionRollbackError behavior isn't documented #269

Open
MarkSFrancis opened this issue Apr 9, 2024 · 0 comments
Open

TransactionRollbackError behavior isn't documented #269

MarkSFrancis opened this issue Apr 9, 2024 · 0 comments

Comments

@MarkSFrancis
Copy link

From the transaction docs, it appears that no error will be thrown by calling rollback() on a transaction, but doing so will throw a TransactionRollbackError. Should the docs be updated?

Documented behavior:
The code provided in the docs:

const db = drizzle(...)
await db.transaction(async (tx) => {
  const [account] = await tx.select({ balance: accounts.balance }).from(accounts).where(eq(users.name, 'Dan'));
  if (account.balance < 100) {
    await tx.rollback()
    return
  }
  await tx.update(accounts).set({ balance: sql`${accounts.balance} - 100.00` }).where(eq(users.name, 'Dan'));
  await tx.update(accounts).set({ balance: sql`${accounts.balance} + 100.00` }).where(eq(users.name, 'Andrew'));
});

seems to be misleading, as the await before tx.rollback() is a no-op, and the return underneath will never be called. What will actually happen is that rollback() will throw an error, which is uncaught in this code example.

Actual behavior:

const db = drizzle(...)
await db.transaction(async (tx) => {
  const [account] = await tx.select({ balance: accounts.balance }).from(accounts).where(eq(users.name, 'Dan'));
  if (account.balance < 100) {
    // Throws a `TransactionRollbackError`
    tx.rollback();
  }
  await tx.update(accounts).set({ balance: sql`${accounts.balance} - 100.00` }).where(eq(users.name, 'Dan'));
  await tx.update(accounts).set({ balance: sql`${accounts.balance} + 100.00` }).where(eq(users.name, 'Andrew'));
});

This suggests that throwing was unintentional behavior, but according to this issue in the drizzle-orm repo, it is intentional but undocumented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant