From e0b156449bd7e3566090ee1f3c2f7afb37fac40c Mon Sep 17 00:00:00 2001 From: Octogonapus Date: Fri, 16 Feb 2024 15:56:37 -0500 Subject: [PATCH] Fix transaction() not actually using a transaction (#213) * patch version bump * Fix transaction() not actually using a transaction --- .gitignore | 2 ++ Project.toml | 2 +- src/load.jl | 4 +--- test/runtests.jl | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 25402a6..425e40f 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,5 @@ deps/build.log # Manifest file Manifest.toml + +.vscode/ diff --git a/Project.toml b/Project.toml index fcd75ad..1b88cde 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "MySQL" uuid = "39abe10b-433b-5dbd-92d4-e302a9df00cd" author = ["quinnj"] -version = "1.4.3" +version = "1.4.4" [deps] DBInterface = "a10d1c49-ce27-4219-8d33-6db1a4562965" diff --git a/src/load.jl b/src/load.jl index 5d34e3e..1aaa58e 100644 --- a/src/load.jl +++ b/src/load.jl @@ -105,14 +105,12 @@ function load(itr, conn::Connection, name::AbstractString="mysql_"*Random.randst end function transaction(f::Function, conn) - API.autocommit(conn.mysql, false) + execute(conn, "START TRANSACTION") try f() API.commit(conn.mysql) catch API.rollback(conn.mysql) rethrow() - finally - API.autocommit(conn.mysql, true) end end \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 583be13..9339aa6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -350,3 +350,39 @@ ret = columntable(res) # load on closed connection should throw @test_throws ArgumentError MySQL.load(ct, conn, "test194") + +@testset "transactions" begin + DBInterface.execute(conn, "DROP TABLE IF EXISTS TransactionTest") + DBInterface.execute(conn, "CREATE TABLE TransactionTest (a int)") + + conn2 = DBInterface.connect(MySQL.Connection, "", ""; option_file=joinpath(dirname(pathof(MySQL)), "../test/", "my.ini")) + + try + # happy path + DBInterface.transaction(conn) do + DBInterface.execute(conn, "INSERT INTO TransactionTest (a) VALUES (1)") + + # we can see the result inside our transaction + result = DBInterface.execute(conn, "SELECT * FROM TransactionTest") |> Tables.columntable + @test result.a == [1] + + # and can't see it outside our transaction + result = DBInterface.execute(conn2, "SELECT * FROM TransactionTest") |> Tables.columntable + @test isempty(result.a) + end + result = DBInterface.execute(conn, "SELECT * FROM TransactionTest") |> Tables.columntable + @test result.a == [1] + result = DBInterface.execute(conn2, "SELECT * FROM TransactionTest") |> Tables.columntable + @test result.a == [1] + + # roll back due to exception + @test_throws ErrorException DBInterface.transaction(conn) do + DBInterface.execute(conn, "INSERT INTO TransactionTest (a) VALUES (2)") + error("force rollback") + end + result = DBInterface.execute(conn, "SELECT * FROM TransactionTest") |> Tables.columntable + @test result.a == [1] # the table did not change + finally + close(conn2) + end +end