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

[ADP-2565] Make runSqlM atomic #4702

Merged
merged 3 commits into from
Aug 1, 2024

Conversation

HeinrichApfelmus
Copy link
Contributor

@HeinrichApfelmus HeinrichApfelmus commented Jul 25, 2024

This pull request changes the semantics of runSqlM to become

  • atomic in the presence of exceptions
  • atomic in the presence of concurrency

This makes the SqlM monad less error-prone and more convenient to use, but disallows some performance techniques, such as SQL savepoints or concurrent access to the SQLite database. However, we need to wrap the monad in a less error-prone interface anyway, and we can always attempt to reintroduce these techniques later, so the gain in convenience is worth the trade now.

This pull request also exposes the exception effect in the SqlM monad explicitly by exposing standard functions such as throw and catch.

Comments

  • I have skipped extensive property testing for now, as I want to stabilize the API first. The module Demo.Database provides a basic functionality check.

Issue Number

ADP-2565

@HeinrichApfelmus HeinrichApfelmus self-assigned this Jul 25, 2024
@HeinrichApfelmus HeinrichApfelmus changed the base branch from master to HeinrichApfelmus/ADP-2565/sql-insertMany July 25, 2024 12:08
@HeinrichApfelmus HeinrichApfelmus force-pushed the HeinrichApfelmus/ADP-2565/sql-monad branch from aa37624 to 16257bc Compare July 25, 2024 13:44
Base automatically changed from HeinrichApfelmus/ADP-2565/sql-insertMany to master July 25, 2024 14:21
@HeinrichApfelmus HeinrichApfelmus force-pushed the HeinrichApfelmus/ADP-2565/sql-monad branch 2 times, most recently from ac8d761 to b46a69e Compare July 26, 2024 10:27
@HeinrichApfelmus HeinrichApfelmus force-pushed the HeinrichApfelmus/ADP-2565/sql-monad branch from b46a69e to 307d0cb Compare July 31, 2024 10:13
Copy link
Member

@Anviking Anviking left a comment

Choose a reason for hiding this comment

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

👌

Comment on lines +76 to +91
testExceptions :: IO ()
testExceptions = do
Sql.withConnection ":memory:" $ \conn -> do
_ <- Sql.runSqlM action conn
putStrLn "Before"
printTablePerson conn
Sql.runSqlM
( do
Sql.deleteWhere (colBirthYear Sql.>. 1800) tablePerson
error "oops"
) conn `catch` (\(_ :: SomeException) -> pure ())
putStrLn "After"
printTablePerson conn
where
printTablePerson conn =
mapM_ print =<< Sql.runSqlM (Sql.selectAll tablePerson) conn
Copy link
Member

@Anviking Anviking Jul 31, 2024

Choose a reason for hiding this comment

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

Did you consider writing these demos as simple unit tests from the beginning?

I imagine it would be little additional work to e.g. replace printTablePerson conn with

selectTablePerson conn
    `shouldReturn`
    [ ("Babbage",1791)
    , ("William",1805)
    , ("Ada",1815)
    ]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did you consider writing these demos as simple unit tests from the beginning?

Since I didn't know what the interface will look like, it didn't make sense for me to write the demo as a unit test in the beginning. But I agree that it makes much more sense now, and that's what I intend to do — in a future pull request. 😅

@HeinrichApfelmus HeinrichApfelmus added this pull request to the merge queue Aug 1, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 1, 2024
@HeinrichApfelmus HeinrichApfelmus added this pull request to the merge queue Aug 1, 2024
Merged via the queue into master with commit 25090fe Aug 1, 2024
23 checks passed
@HeinrichApfelmus HeinrichApfelmus deleted the HeinrichApfelmus/ADP-2565/sql-monad branch August 1, 2024 12:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants