Skip to content

Commit 7b75f23

Browse files
authored
Fix transactions again and update tests (#215)
* Fix transactions again and update tests * patch version bump
1 parent e0b1564 commit 7b75f23

File tree

7 files changed

+82
-43
lines changed

7 files changed

+82
-43
lines changed

.github/workflows/ci.yml

+1-11
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,7 @@ jobs:
2121
- x64
2222
steps:
2323
- uses: actions/checkout@v2
24-
- uses: getong/mariadb-action@v1.1
25-
with:
26-
host port: 3306 # Optional, default value is 3306. The port of host
27-
container port: 3306 # Optional, default value is 3306. The port of container
28-
character set server: 'utf8mb4' # Optional, default value is 'utf8mb4'. The '--character-set-server' option for mysqld
29-
collation server: 'utf8mb4_general_ci' # Optional, default value is 'utf8mb4_general_ci'. The '--collation-server' option for mysqld
30-
mariadb version: 'latest' # Optional, default value is "latest". The version of the MariaDB
31-
mysql database: 'mysqltest' # Optional, default value is "test". The specified database which will be create
32-
mysql root password: '' # Required if "mysql user" is empty, default is empty. The root superuser password
33-
# mysql user: 'developer' # Required if "mysql root password" is empty, default is empty. The superuser for the specified database. Can use secrets, too
34-
# mysql password: ${{ secrets.DatabasePassword }} # Required if "mysql user" exists. The password for the "mysql user"
24+
- run: docker compose up -d
3525
- uses: julia-actions/setup-julia@v1
3626
with:
3727
version: ${{ matrix.version }}

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.4"
4+
version = "1.4.5"
55

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

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,17 @@
1111

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

14-
### Documentation
14+
## Documentation
1515

1616
[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://mysql.juliadatabases.org/stable)
1717
[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://mysql.juliadatabases.org/dev)
18+
19+
## Contributing
20+
21+
The tests require a MySQL DB to be running, which is provided by Docker:
22+
23+
```sh
24+
docker compose up -d
25+
julia --project -e 'using Pkg; Pkg.test()'
26+
docker compose down
27+
```

docker-compose.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
version: "3.9"
2+
name: "mysqljl-test"
3+
services:
4+
db:
5+
image: mysql:8
6+
ports:
7+
- 3306:3306
8+
environment:
9+
MYSQL_ALLOW_EMPTY_PASSWORD: true
10+
healthcheck:
11+
test:
12+
[
13+
"CMD",
14+
"mysql",
15+
"-u",
16+
"root",
17+
"-p''",
18+
"--silent",
19+
"--execute",
20+
"SELECT 1;",
21+
]
22+
interval: 30s
23+
timeout: 10s
24+
retries: 5
25+
networks:
26+
- app
27+
networks:
28+
app:
29+
driver: bridge

src/MySQL.jl

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module MySQL
22

33
using Dates, DBInterface, Tables, Parsers, DecFP
4+
import DBInterface: transaction
45

56
export DBInterface, DateAndTime
67

src/load.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function load(itr, conn::Connection, name::AbstractString="mysql_"*Random.randst
9191
DBInterface.execute(conn, "DELETE FROM $name")
9292
end
9393
# start a transaction for inserting rows
94-
transaction(conn) do
94+
DBInterface.transaction(conn) do
9595
params = chop(repeat("?,", length(sch.names)))
9696
stmt = DBInterface.prepare(conn, "INSERT INTO $name VALUES ($params)")
9797
for (i, row) in enumerate(rows)
@@ -104,13 +104,13 @@ function load(itr, conn::Connection, name::AbstractString="mysql_"*Random.randst
104104
return name
105105
end
106106

107-
function transaction(f::Function, conn)
108-
execute(conn, "START TRANSACTION")
107+
function DBInterface.transaction(f::Function, conn::Connection)
108+
DBInterface.execute(conn, "START TRANSACTION")
109109
try
110110
f()
111111
API.commit(conn.mysql)
112112
catch
113113
API.rollback(conn.mysql)
114114
rethrow()
115115
end
116-
end
116+
end

test/runtests.jl

+35-26
Original file line numberDiff line numberDiff line change
@@ -352,37 +352,46 @@ ret = columntable(res)
352352
@test_throws ArgumentError MySQL.load(ct, conn, "test194")
353353

354354
@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-
355+
conn = DBInterface.connect(MySQL.Connection, "127.0.0.1", "root", ""; port=3306)
360356
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
357+
DBInterface.execute(conn, "DROP DATABASE if exists mysqltest")
358+
DBInterface.execute(conn, "CREATE DATABASE mysqltest")
359+
DBInterface.execute(conn, "use mysqltest")
360+
DBInterface.execute(conn, "DROP TABLE IF EXISTS TransactionTest")
361+
DBInterface.execute(conn, "CREATE TABLE TransactionTest (a int)")
362+
363+
conn2 = DBInterface.connect(MySQL.Connection, "127.0.0.1", "root", ""; port=3306)
364+
DBInterface.execute(conn2, "use mysqltest")
365+
366+
try
367+
# happy path
368+
DBInterface.transaction(conn) do
369+
DBInterface.execute(conn, "INSERT INTO TransactionTest (a) VALUES (1)")
370+
371+
# we can see the result inside our transaction
372+
result = DBInterface.execute(conn, "SELECT * FROM TransactionTest") |> Tables.columntable
373+
@test result.a == [1]
374+
375+
# and can't see it outside our transaction
376+
result = DBInterface.execute(conn2, "SELECT * FROM TransactionTest") |> Tables.columntable
377+
@test isempty(result.a)
378+
end
366379
result = DBInterface.execute(conn, "SELECT * FROM TransactionTest") |> Tables.columntable
367380
@test result.a == [1]
368-
369-
# and can't see it outside our transaction
370381
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+
@test result.a == [1]
383+
384+
# roll back due to exception
385+
@test_throws ErrorException DBInterface.transaction(conn) do
386+
DBInterface.execute(conn, "INSERT INTO TransactionTest (a) VALUES (2)")
387+
error("force rollback")
388+
end
389+
result = DBInterface.execute(conn, "SELECT * FROM TransactionTest") |> Tables.columntable
390+
@test result.a == [1] # the table did not change
391+
finally
392+
DBInterface.close!(conn2)
382393
end
383-
result = DBInterface.execute(conn, "SELECT * FROM TransactionTest") |> Tables.columntable
384-
@test result.a == [1] # the table did not change
385394
finally
386-
close(conn2)
395+
DBInterface.close!(conn)
387396
end
388397
end

0 commit comments

Comments
 (0)