Skip to content

Commit

Permalink
Fix transaction() not actually using a transaction (#213)
Browse files Browse the repository at this point in the history
* patch version bump

* Fix transaction() not actually using a transaction
  • Loading branch information
Octogonapus committed Feb 16, 2024
1 parent ffd5aeb commit e0b1564
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ deps/build.log

# Manifest file
Manifest.toml

.vscode/
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 1 addition & 3 deletions src/load.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 36 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 comments on commit e0b1564

@quinnj
Copy link
Member

@quinnj quinnj commented on e0b1564 Feb 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/101054

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.4.4 -m "<description of version>" e0b156449bd7e3566090ee1f3c2f7afb37fac40c
git push origin v1.4.4

Please sign in to comment.