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

add ControlledTransaction. #962

Merged
merged 22 commits into from
Oct 5, 2024

Conversation

igalklebanov
Copy link
Member

@igalklebanov igalklebanov commented Apr 23, 2024

closes #257.

This PR adds some optional savepoint-related methods to drivers - need to make sure we don't introduce a breaking change here!
A new ControlledTransaction that allows manual commit, rollback and savepoint-related commands.

const trx = await db.startTransaction().execute()

try {
  const moshe = await trx
    .insertInto('person')
    .values({ name: 'Moshe' })
    .returning('id')
    .executeTakeFirstOrThrow()

  const trxAfterFoo = await trx.savepoint('foo').execute()

  try {
    const bone = await trxAfterFoo
      .insertInto('toy')
      .values({ name: 'Bone', price: 1.99 })
      .returning('id')
      .executeTakeFirstOrThrow()

    await trxAfterFoo
      .insertInto('pet')
      .values({ 
        owner_id: moshe.id, 
        name: 'Lucky', 
        favorite_toy_id: bone.id 
      })
      .execute()
  } catch (error) {
    await trxAfterFoo.rollbackToSavepoint('foo').execute()
  }

  await trxAfterFoo.releaseSavepoint('foo').execute()

  await trx.insertInto('audit').values({ action: 'added Moshe' }).execute()

  await trx.commit().execute()
} catch (error) {
  await trx.rollback().execute()
}

Some design goals (that hopefully will be met once this is done) with ControlledTransaction:

  • allow starting a controlled transaction in single connection scenario - it should re-use the connection, and not close/release it on commit/rollback by default.
    - allow to release the connection on commit/rollback on demand via an option.
  • once the controlled transaction is committed/rolled back, it should not allow executing any further queries with it.
  • savepoint names are type-safe and autocompletion friendly - cannot release/rollback to a name that was not saved before. cannot save with a name that already exists.

@igalklebanov igalklebanov added api Related to library's API enhancement New feature or request labels Apr 23, 2024
Copy link

vercel bot commented Apr 23, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
kysely ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 5, 2024 4:53pm

@igalklebanov igalklebanov added custom dialect Related to a custom dialect built-in dialect Related to a built-in dialect breaking change Includes breaking changes and removed breaking change Includes breaking changes labels Apr 24, 2024
@igalklebanov igalklebanov marked this pull request as ready for review April 29, 2024 11:10
@seivan
Copy link

seivan commented May 9, 2024

I was wondering, since you already have a reference to the transaction trxAfterFoo and it has been named at creation with trx.savepoint('foo').execute() why not default to it

instead of

trxAfterFoo.rollbackToSavepoint('foo')

It should technically be enough with trxAfterFoo.rollbackToSavepoint() or am I missing something?

The reason I ask is because I'd like something simple like db.startTransaction() and db.endTransaction() to be in beforeEach and afterEach test, so each unit test runs with empty tables.

@igalklebanov igalklebanov merged commit b841ab1 into kysely-org:v0.28 Oct 5, 2024
12 checks passed
@igalklebanov igalklebanov deleted the controlled-transaction branch October 6, 2024 00:04
igalklebanov added a commit that referenced this pull request Oct 12, 2024
Co-authored-by: Igal Klebanov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Related to library's API built-in dialect Related to a built-in dialect custom dialect Related to a custom dialect enhancement New feature or request ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

More fine-grained transaction API with savepoints?
3 participants