Skip to content

Commit bf59a0d

Browse files
committed
Start with new transaction approach
1 parent d1a6459 commit bf59a0d

4 files changed

Lines changed: 41 additions & 4 deletions

File tree

gleam.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ gleam_erlang = ">= 1.2.0 and < 2.0.0"
2020
gleam_otp = ">= 1.0.0 and < 2.0.0"
2121
gleam_stdlib = ">= 0.51.0 and < 2.0.0"
2222
gleam_time = ">= 1.0.0 and < 2.0.0"
23-
pgo = ">= 0.12.0 and < 2.0.0"
23+
pgo = ">= 0.14.0 and < 1.0.0"
2424

2525
[dev-dependencies]
2626
gleeunit = ">= 1.0.0 and < 2.0.0"

manifest.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ gleam_otp = { version = ">= 1.0.0 and < 2.0.0" }
2121
gleam_stdlib = { version = ">= 0.51.0 and < 2.0.0" }
2222
gleam_time = { version = ">= 1.0.0 and < 2.0.0" }
2323
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
24-
pgo = { version = ">= 0.12.0 and < 2.0.0" }
24+
pgo = { version = ">= 0.14.0 and < 1.0.0" }

src/pog.gleam

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
////
33
//// Gleam wrapper around pgo library
44

5+
// TODO: Connection is a union of the name and the checked out connection
6+
// TODO: Move handling of this into Gleam to make it easier to write
7+
58
// TODO: add time and timestamp with zone once pgo supports them
69

710
import gleam/dynamic.{type Dynamic}
@@ -406,12 +409,17 @@ pub type TransactionError {
406409
///
407410
/// If the function returns an `Error` or panics then the transaction is rolled
408411
/// back.
409-
@external(erlang, "pog_ffi", "transaction")
410412
pub fn transaction(
411413
pool: Subject(Message),
412414
callback: fn(Subject(Message)) -> Result(t, String),
413415
) -> Result(t, TransactionError)
414416

417+
@external(erlang, "pog_ffi", "transaction")
418+
fn run_transaction(
419+
pool: Name(Message),
420+
callback: fn(Subject(Message)) -> Result(t, String),
421+
) -> Result(t, TransactionError)
422+
415423
pub fn nullable(inner_type: fn(a) -> Value, value: Option(a)) -> Value {
416424
case value {
417425
Some(term) -> inner_type(term)

src/pog_ffi.erl

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ start(Config) ->
8080
end,
8181
pgo_pool:start_link(PoolName, Options2).
8282

83-
transaction(Pool, Callback) when is_atom(Pool) ->
83+
old_transaction(Pool, Callback) when is_atom(Pool) ->
8484
F = fun() ->
8585
case Callback(Pool) of
8686
{ok, T} -> {ok, T};
@@ -95,6 +95,35 @@ transaction(Pool, Callback) when is_atom(Pool) ->
9595
end.
9696

9797

98+
transaction(Pool, Fun) when is_atom(Pool) andalso is_function(Fun, 1) ->
99+
Exec = fun(Conn, Sql) ->
100+
pgo_handler:extended_query(Conn, Sql, [], #{queue_time => undefined})
101+
end,
102+
case pgo:checkout(Pool) of
103+
{ok, Ref, Conn} ->
104+
try
105+
#{command := 'begin'} = Exec(Conn, "BEGIN"),
106+
Result = Fun(),
107+
case Exec(Conn, "COMMIT") of
108+
#{command := commit} -> Result;
109+
#{command := rollback} -> Result
110+
end
111+
catch
112+
Type:Reason:Stacktrace ->
113+
Exec(Conn, "ROLLBACK"),
114+
erlang:raise(Type, Reason, Stacktrace)
115+
after
116+
pgo:checkin(Ref, Conn)
117+
end;
118+
{error, _} = Error ->
119+
Error
120+
end;
121+
% TODO: remove
122+
transaction(A, B) ->
123+
erlang:display(A),
124+
erlang:display(B),
125+
erlang:raise(badarg).
126+
98127
query(Pool, Sql, Arguments, Timeout) when is_atom(Pool) ->
99128
Options = #{
100129
pool => Pool,

0 commit comments

Comments
 (0)