Skip to content

Commit e0b1564

Browse files
authored
Fix transaction() not actually using a transaction (#213)
* patch version bump * Fix transaction() not actually using a transaction
1 parent ffd5aeb commit e0b1564

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ deps/build.log
4040

4141
# Manifest file
4242
Manifest.toml
43+
44+
.vscode/

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "MySQL"
22
uuid = "39abe10b-433b-5dbd-92d4-e302a9df00cd"
33
author = ["quinnj"]
4-
version = "1.4.3"
4+
version = "1.4.4"
55

66
[deps]
77
DBInterface = "a10d1c49-ce27-4219-8d33-6db1a4562965"

src/load.jl

+1-3
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,12 @@ function load(itr, conn::Connection, name::AbstractString="mysql_"*Random.randst
105105
end
106106

107107
function transaction(f::Function, conn)
108-
API.autocommit(conn.mysql, false)
108+
execute(conn, "START TRANSACTION")
109109
try
110110
f()
111111
API.commit(conn.mysql)
112112
catch
113113
API.rollback(conn.mysql)
114114
rethrow()
115-
finally
116-
API.autocommit(conn.mysql, true)
117115
end
118116
end

test/runtests.jl

+36
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,39 @@ ret = columntable(res)
350350

351351
# load on closed connection should throw
352352
@test_throws ArgumentError MySQL.load(ct, conn, "test194")
353+
354+
@testset "transactions" begin
355+
DBInterface.execute(conn, "DROP TABLE IF EXISTS TransactionTest")
356+
DBInterface.execute(conn, "CREATE TABLE TransactionTest (a int)")
357+
358+
conn2 = DBInterface.connect(MySQL.Connection, "", ""; option_file=joinpath(dirname(pathof(MySQL)), "../test/", "my.ini"))
359+
360+
try
361+
# happy path
362+
DBInterface.transaction(conn) do
363+
DBInterface.execute(conn, "INSERT INTO TransactionTest (a) VALUES (1)")
364+
365+
# we can see the result inside our transaction
366+
result = DBInterface.execute(conn, "SELECT * FROM TransactionTest") |> Tables.columntable
367+
@test result.a == [1]
368+
369+
# and can't see it outside our transaction
370+
result = DBInterface.execute(conn2, "SELECT * FROM TransactionTest") |> Tables.columntable
371+
@test isempty(result.a)
372+
end
373+
result = DBInterface.execute(conn, "SELECT * FROM TransactionTest") |> Tables.columntable
374+
@test result.a == [1]
375+
result = DBInterface.execute(conn2, "SELECT * FROM TransactionTest") |> Tables.columntable
376+
@test result.a == [1]
377+
378+
# roll back due to exception
379+
@test_throws ErrorException DBInterface.transaction(conn) do
380+
DBInterface.execute(conn, "INSERT INTO TransactionTest (a) VALUES (2)")
381+
error("force rollback")
382+
end
383+
result = DBInterface.execute(conn, "SELECT * FROM TransactionTest") |> Tables.columntable
384+
@test result.a == [1] # the table did not change
385+
finally
386+
close(conn2)
387+
end
388+
end

0 commit comments

Comments
 (0)