Skip to content

Commit

Permalink
transactions should defect
Browse files Browse the repository at this point in the history
  • Loading branch information
jkonowitch committed Oct 2, 2024
1 parent 7102698 commit 72eadfd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
18 changes: 3 additions & 15 deletions packages/sql-libsql/src/LibsqlClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as Context from "effect/Context"
import * as Effect from "effect/Effect"
import { identity } from "effect/Function"
import * as Layer from "effect/Layer"
import * as Scope from "effect/Scope"
import type * as Scope from "effect/Scope"

/**
* @category type ids
Expand Down Expand Up @@ -154,22 +154,10 @@ export const make = (
})
})

const semaphore = yield* Effect.makeSemaphore(1)
const connection = yield* makeConnection

const acquirer = semaphore.withPermits(1)(Effect.succeed(connection))
const transactionAcquirer = Effect.uninterruptibleMask((restore) =>
Effect.as(
Effect.zipRight(
restore(semaphore.take(1)),
Effect.tap(
Effect.scope,
(scope) => Scope.addFinalizer(scope, semaphore.release(1))
)
),
connection
)
)
const acquirer = Effect.succeed(connection)
const transactionAcquirer = Effect.dieMessage("transactions are not supported in libsql")

return Object.assign(
Client.make({
Expand Down
12 changes: 8 additions & 4 deletions packages/sql-libsql/test/Client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LibsqlClient } from "@effect/sql-libsql"
import { assert, describe, layer } from "@effect/vitest"
import { Effect } from "effect"
import { Cause, Effect } from "effect"
import { LibsqlContainer } from "./util.js"

describe("Client", () => {
Expand Down Expand Up @@ -53,13 +53,17 @@ describe("Client", () => {
})
}))

it.scoped("withTransaction", () =>
it.scoped("should defect on transactions", () =>
Effect.gen(function*() {
const sql = yield* LibsqlClient.LibsqlClient
yield* sql`CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)`
yield* sql.withTransaction(sql`INSERT INTO test (name) VALUES ('hello')`)
const res = yield* sql`INSERT INTO test ${sql.insert({ name: "hello" })}`.pipe(
sql.withTransaction,
Effect.catchAllDefect((defect) => Effect.succeed(defect))
)
const rows = yield* sql`SELECT * FROM test`
assert.deepStrictEqual(rows, [{ id: 1, name: "hello" }])
assert.deepStrictEqual(rows, [])
assert.equal(Cause.isRuntimeException(res), true)
}))
})
})

0 comments on commit 72eadfd

Please sign in to comment.