-
Notifications
You must be signed in to change notification settings - Fork 21
Transactions #73
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
Merged
Merged
Transactions #73
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
55e47be
add clean task
elcritch 072f949
add composite key test
elcritch 520ec82
add postgres setup scripts
elcritch eff70fe
add postgres setup scripts
elcritch 131c079
fix postgres on macosx
elcritch 2d99159
fix postgres
elcritch d5707d0
fix postgres
elcritch 62669fb
fix postgres test, document using db_connector
elcritch 764035e
fix postgres test, document using db_connector
elcritch 41beb00
run pg tests
elcritch 5f69404
run pg tests
elcritch 1db1448
run pg tests
elcritch 446b1b6
run pg tests
elcritch 30bf3e4
run pg tests
elcritch 55b8724
run pg tests
elcritch 3bb38d8
use pegs
elcritch 6587455
use pegs
elcritch 6973415
Use parsesql for table name detection
elcritch db15fc4
Merge pull request #6 from elcritch/codex/switch-db_utils-to-uses-par…
elcritch 9beb13d
updates
elcritch 32e6594
updates
elcritch 547d731
updates
elcritch 236f721
updates
elcritch 7cfe86a
adding transactions
elcritch 471f1bc
Merge branch 'master' into transactions
elcritch 3856f35
fix transaction tests
elcritch df938c5
switch to templates
elcritch 398df31
switch to templates
elcritch fcbed07
switch to templates
elcritch 32cbbba
switch to templates
elcritch 03bba5a
switch to templates
elcritch e888269
switch to templates
elcritch 49b9815
switch to templates
elcritch 000393c
switch to templates
elcritch 2177fc4
switch to templates
elcritch bde349d
switch to templates
elcritch 862da5a
switch to templates
elcritch 201856f
update tests
elcritch 23d8e29
update tests
elcritch 9a704cf
update tests
elcritch 98b1baa
Update config.nims
elcritch cedcb8f
Update ormin/queries.nim
elcritch 0328899
update tests
elcritch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import unittest, os, strformat | ||
| import ormin | ||
| import ormin/db_utils | ||
| when NimVersion < "1.2.0": import ./compat | ||
|
|
||
| let testDir = currentSourcePath.parentDir() | ||
|
|
||
| when defined postgre: | ||
| when defined(macosx): | ||
| {.passL: "-Wl,-rpath,/opt/homebrew/lib/postgresql@14".} | ||
| from db_connector/db_postgres import exec, getValue | ||
| const backend = DbBackend.postgre | ||
| importModel(backend, "forum_model_postgres") | ||
| const sqlFileName = "forum_model_postgres.sql" | ||
| let db {.global.} = open("localhost", "test", "test", "test_ormin") | ||
| else: | ||
| from db_connector/db_sqlite import exec, getValue | ||
| const backend = DbBackend.sqlite | ||
| importModel(backend, "forum_model_sqlite") | ||
| const sqlFileName = "forum_model_sqlite.sql" | ||
| var memoryPath = testDir & "/" & ":memory:" | ||
| let db {.global.} = open(memoryPath, "", "", "") | ||
|
|
||
| var sqlFilePath = Path(testDir & "/" & sqlFileName) | ||
|
|
||
| # Fresh schema | ||
| db.dropTable(sqlFilePath) | ||
| db.createTable(sqlFilePath) | ||
|
|
||
| suite &"Transactions ({backend})": | ||
|
|
||
| test "commit on success": | ||
| transaction: | ||
| query: | ||
| insert person(id = ?(101), name = ?"john101", password = ?"p101", email = ?"john101@mail.com", salt = ?"s101", status = ?"ok") | ||
| check db.getValue(sql"select count(*) from person where id = 101") == "1" | ||
|
|
||
| test "rollback on error with manual try except": | ||
| # prepare one row | ||
| query: | ||
| insert person(id = ?(201), name = ?"john201", password = ?"p201", email = ?"john201@mail.com", salt = ?"s201", status = ?"ok") | ||
| # in transaction insert a new row and then violate PK | ||
| try: | ||
| transaction: | ||
| query: | ||
| insert person(id = ?(202), name = ?"john202", password = ?"p202", email = ?"john202@mail.com", salt = ?"s202", status = ?"ok") | ||
| # duplicate key error | ||
| query: | ||
| insert person(id = ?(201), name = ?"dup", password = ?"p", email = ?"e", salt = ?"s", status = ?"x") | ||
| check false # should not reach | ||
| except DbError as e: | ||
| discard | ||
| # both inserts inside the transaction should be rolled back | ||
| check db.getValue(sql"select count(*) from person where id = 202") == "0" | ||
| check db.getValue(sql"select count(*) from person where id = 201 and name = 'dup'") == "0" | ||
|
|
||
| test "rollback on error with else": | ||
| # prepare one row | ||
| var failed = false | ||
| query: | ||
| insert person(id = ?(501), name = ?"john501", password = ?"p501", email = ?"john501@mail.com", salt = ?"s501", status = ?"ok") | ||
| # in transaction insert a new row and then violate PK | ||
| transaction: | ||
| query: | ||
| insert person(id = ?(502), name = ?"john502", password = ?"p502", email = ?"john502@mail.com", salt = ?"s502", status = ?"ok") | ||
| # duplicate key error | ||
| query: | ||
| insert person(id = ?(501), name = ?"dup", password = ?"p", email = ?"e", salt = ?"s", status = ?"x") | ||
| check false # should not reach | ||
| else: | ||
| echo "do something else..." | ||
| failed = true | ||
|
|
||
| check failed | ||
| # both inserts inside the transaction should be rolled back | ||
| check db.getValue(sql"select count(*) from person where id = 502") == "0" | ||
| check db.getValue(sql"select count(*) from person where id = 501 and name = 'dup'") == "0" | ||
|
|
||
| test "commit normally with else": | ||
| # prepare one row | ||
| var failed = false | ||
| query: | ||
| insert person(id = ?(601), name = ?"john601", password = ?"p601", email = ?"john601@mail.com", salt = ?"s601", status = ?"ok") | ||
| # in transaction insert a new row and then violate PK | ||
| transaction: | ||
| query: | ||
| insert person(id = ?(602), name = ?"john602", password = ?"p602", email = ?"john602@mail.com", salt = ?"s602", status = ?"ok") | ||
| query: | ||
| insert person(id = ?(603), name = ?"dup", password = ?"p", email = ?"e", salt = ?"s", status = ?"x") | ||
| else: | ||
| failed = true | ||
|
|
||
| check not failed | ||
| # both inserts inside the transaction should be rolled back | ||
| check db.getValue(sql"select count(*) from person where id = 603") == "1" | ||
| check db.getValue(sql"select count(*) from person where id = 602") == "1" | ||
| check db.getValue(sql"select count(*) from person where id = 601") == "1" | ||
|
|
||
| test "transaction set false on DbError": | ||
| var failed = false | ||
| transaction: | ||
| query: | ||
| insert person(id = ?(301), name = ?"john301", password = ?"p301", email = ?"john301@mail.com", salt = ?"s301", status = ?"ok") | ||
| query: | ||
| insert person(id = ?(301), name = ?"dup", password = ?"p", email = ?"e", salt = ?"s", status = ?"x") | ||
| else: | ||
| failed = true | ||
| check failed | ||
| check db.getValue(sql"select count(*) from person where id = 301") == "0" | ||
|
|
||
| test "nested savepoints": | ||
| var failed = false | ||
| transaction: | ||
| query: | ||
| insert person(id = ?(401), name = ?"john401", password = ?"p401", email = ?"john401@mail.com", salt = ?"s401", status = ?"ok") | ||
| var innerOk = true | ||
| transaction: | ||
| query: | ||
| insert person(id = ?(402), name = ?"john402", password = ?"p402", email = ?"john402@mail.com", salt = ?"s402", status = ?"ok") | ||
| query: | ||
| insert person(id = ?(401), name = ?"dup401", password = ?"p", email = ?"e", salt = ?"s", status = ?"x") | ||
| else: | ||
| innerOk = false | ||
|
|
||
| check innerOk == false | ||
|
|
||
| # after inner rollback, we can still insert another row and commit outer | ||
| query: | ||
| insert person(id = ?(403), name = ?"john403", password = ?"p403", email = ?"john403@mail.com", salt = ?"s403", status = ?"ok") | ||
| else: | ||
| failed = true | ||
| check db.getValue(sql"select count(*) from person where id in (401,402,403)") == "2" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, so that is something GPT5 added. I'd committed the first changes already when I saw these. It looked odd but I figured they already existed.
I'm not sure if these are any better to use than a plain
db.exec. Is doing the prepare statement and query steps beneficial? Seems like a bit of AI slop to drop.