Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transactions again and update tests #215

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading