Skip to content

Commit

Permalink
Fix transactions again and update tests (#215)
Browse files Browse the repository at this point in the history
* Fix transactions again and update tests

* patch version bump
  • Loading branch information
Octogonapus committed Feb 23, 2024
1 parent e0b1564 commit 7b75f23
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 43 deletions.
12 changes: 1 addition & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,7 @@ jobs:
- x64
steps:
- uses: actions/checkout@v2
- uses: getong/[email protected]
with:
host port: 3306 # Optional, default value is 3306. The port of host
container port: 3306 # Optional, default value is 3306. The port of container
character set server: 'utf8mb4' # Optional, default value is 'utf8mb4'. The '--character-set-server' option for mysqld
collation server: 'utf8mb4_general_ci' # Optional, default value is 'utf8mb4_general_ci'. The '--collation-server' option for mysqld
mariadb version: 'latest' # Optional, default value is "latest". The version of the MariaDB
mysql database: 'mysqltest' # Optional, default value is "test". The specified database which will be create
mysql root password: '' # Required if "mysql user" is empty, default is empty. The root superuser password
# mysql user: 'developer' # Required if "mysql root password" is empty, default is empty. The superuser for the specified database. Can use secrets, too
# mysql password: ${{ secrets.DatabasePassword }} # Required if "mysql user" exists. The password for the "mysql user"
- run: docker compose up -d
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.version }}
Expand Down
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.4"
version = "1.4.5"

[deps]
DBInterface = "a10d1c49-ce27-4219-8d33-6db1a4562965"
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@

Package for interfacing with MySQL databases from Julia via the MariaDB C connector library, version 3.1.6.

### Documentation
## Documentation

[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://mysql.juliadatabases.org/stable)
[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://mysql.juliadatabases.org/dev)

## Contributing

The tests require a MySQL DB to be running, which is provided by Docker:

```sh
docker compose up -d
julia --project -e 'using Pkg; Pkg.test()'
docker compose down
```
29 changes: 29 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: "3.9"
name: "mysqljl-test"
services:
db:
image: mysql:8
ports:
- 3306:3306
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: true
healthcheck:
test:
[
"CMD",
"mysql",
"-u",
"root",
"-p''",
"--silent",
"--execute",
"SELECT 1;",
]
interval: 30s
timeout: 10s
retries: 5
networks:
- app
networks:
app:
driver: bridge
1 change: 1 addition & 0 deletions src/MySQL.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module MySQL

using Dates, DBInterface, Tables, Parsers, DecFP
import DBInterface: transaction

export DBInterface, DateAndTime

Expand Down
8 changes: 4 additions & 4 deletions src/load.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function load(itr, conn::Connection, name::AbstractString="mysql_"*Random.randst
DBInterface.execute(conn, "DELETE FROM $name")
end
# start a transaction for inserting rows
transaction(conn) do
DBInterface.transaction(conn) do
params = chop(repeat("?,", length(sch.names)))
stmt = DBInterface.prepare(conn, "INSERT INTO $name VALUES ($params)")
for (i, row) in enumerate(rows)
Expand All @@ -104,13 +104,13 @@ function load(itr, conn::Connection, name::AbstractString="mysql_"*Random.randst
return name
end

function transaction(f::Function, conn)
execute(conn, "START TRANSACTION")
function DBInterface.transaction(f::Function, conn::Connection)
DBInterface.execute(conn, "START TRANSACTION")
try
f()
API.commit(conn.mysql)
catch
API.rollback(conn.mysql)
rethrow()
end
end
end
61 changes: 35 additions & 26 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -352,37 +352,46 @@ ret = columntable(res)
@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"))

conn = DBInterface.connect(MySQL.Connection, "127.0.0.1", "root", ""; port=3306)
try
# happy path
DBInterface.transaction(conn) do
DBInterface.execute(conn, "INSERT INTO TransactionTest (a) VALUES (1)")

# we can see the result inside our transaction
DBInterface.execute(conn, "DROP DATABASE if exists mysqltest")
DBInterface.execute(conn, "CREATE DATABASE mysqltest")
DBInterface.execute(conn, "use mysqltest")
DBInterface.execute(conn, "DROP TABLE IF EXISTS TransactionTest")
DBInterface.execute(conn, "CREATE TABLE TransactionTest (a int)")

conn2 = DBInterface.connect(MySQL.Connection, "127.0.0.1", "root", ""; port=3306)
DBInterface.execute(conn2, "use mysqltest")

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]

# 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")
@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
DBInterface.close!(conn2)
end
result = DBInterface.execute(conn, "SELECT * FROM TransactionTest") |> Tables.columntable
@test result.a == [1] # the table did not change
finally
close(conn2)
DBInterface.close!(conn)
end
end

2 comments on commit 7b75f23

@quinnj
Copy link
Member

@quinnj quinnj commented on 7b75f23 Feb 23, 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/101553

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.5 -m "<description of version>" 7b75f232b1454e65401f12acf0a97ba6fed95536
git push origin v1.4.5

Please sign in to comment.