-
Notifications
You must be signed in to change notification settings - Fork 183
Proper use of SQLite's RETURNING mechanism for inserts
#774
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
Changes from all commits
Commits
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
132 changes: 132 additions & 0 deletions
132
beam-sqlite/test/Database/Beam/Sqlite/Test/InsertOnConflictReturning.hs
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 @@ | ||
| {-# LANGUAGE DerivingStrategies #-} | ||
|
|
||
| {- | ||
| This module is based on the bug reproducer from issue #773 | ||
| https://github.com/haskell-beam/beam/issues/773 | ||
| -} | ||
| module Database.Beam.Sqlite.Test.InsertOnConflictReturning (tests) where | ||
|
|
||
| import Data.Int (Int32) | ||
| import Data.Text (Text) | ||
| import Database.Beam ( | ||
| Beamable, | ||
| Columnar, | ||
| Database, | ||
| DatabaseSettings, | ||
| Generic, | ||
| Identity, | ||
| SqlEq ((/=.)), | ||
| Table (..), | ||
| TableEntity, | ||
| current_, | ||
| defaultDbSettings, | ||
| insert, | ||
| insertValues, | ||
| runInsert, | ||
| (<-.), | ||
| ) | ||
| import Database.Beam.Backend.SQL.BeamExtensions ( | ||
| BeamHasInsertOnConflict ( | ||
| conflictingFields, | ||
| insertOnConflict, | ||
| onConflictUpdateSetWhere | ||
| ), | ||
| MonadBeamInsertReturning (runInsertReturningList), | ||
| ) | ||
| import Database.Beam.Sqlite (Sqlite, runBeamSqlite) | ||
| import Database.Beam.Sqlite.Test (withTestDb) | ||
| import Database.SQLite.Simple (execute_) | ||
| import Test.Tasty (TestTree, testGroup) | ||
| import Test.Tasty.HUnit (testCase, (@?=)) | ||
|
|
||
| import Database.Beam | ||
| import Database.Beam.Backend.SQL.BeamExtensions ( | ||
| conflictingFields, | ||
| insertOnConflict, | ||
| onConflictUpdateSetWhere, | ||
| runInsertReturningList, | ||
| ) | ||
| import Database.Beam.Migrate (defaultMigratableDbSettings) | ||
| import Database.Beam.Migrate.Simple (CheckedDatabaseSettings, autoMigrate) | ||
| import Database.Beam.Sqlite (Sqlite, runBeamSqliteDebug) | ||
| import Database.Beam.Sqlite.Migrate (migrationBackend) | ||
| import Database.SQLite.Simple (open) | ||
|
|
||
| tests :: TestTree | ||
| tests = | ||
| testGroup | ||
| "Insertion on conflict returning tests" | ||
| [testInsertOnConflictReturning] | ||
|
|
||
| data TestDb f | ||
| = TestDb | ||
| { usersTable :: f (TableEntity User) | ||
| } | ||
| deriving stock (Generic) | ||
|
|
||
| deriving anyclass instance Database be TestDb | ||
|
|
||
| testDb :: DatabaseSettings be TestDb | ||
| testDb = defaultDbSettings | ||
|
|
||
| checkedDb :: CheckedDatabaseSettings Sqlite TestDb | ||
| checkedDb = defaultMigratableDbSettings | ||
|
|
||
| data User f | ||
| = User | ||
| { userId :: Columnar f Int32 | ||
| , userName :: Columnar f Text | ||
| } | ||
| deriving stock (Generic) | ||
|
|
||
| deriving stock instance Show (PrimaryKey User Identity) | ||
| deriving stock instance Show (User Identity) | ||
|
|
||
| instance Table User where | ||
| newtype PrimaryKey User f = UserId (Columnar f Int32) | ||
| deriving stock (Generic) | ||
| primaryKey = UserId . userId | ||
|
|
||
| deriving anyclass instance Beamable User | ||
| deriving anyclass instance Beamable (PrimaryKey User) | ||
|
|
||
| testInsertOnConflictReturning :: TestTree | ||
| testInsertOnConflictReturning = testCase "Check that conflicting values are returned by `runInsertReturningList`" $ | ||
| withTestDb $ \conn -> do | ||
| conflicts <- | ||
| runBeamSqlite conn $ do | ||
| autoMigrate migrationBackend checkedDb | ||
|
|
||
| runInsert $ | ||
| insert | ||
| (usersTable testDb) | ||
| ( insertValues | ||
| [ User{userId = 0, userName = "user0"} | ||
| , User{userId = 2, userName = "user2"} | ||
| , User{userId = 5, userName = "user5"} | ||
| ] | ||
| ) | ||
|
|
||
| let newUsers = | ||
| [ User{userId = 1, userName = "user1"} | ||
| , User{userId = 2, userName = "different_user2"} | ||
| ] | ||
|
|
||
| runInsertReturningList $ | ||
| insertOnConflict | ||
| (usersTable testDb) | ||
| (insertValues newUsers) | ||
| (conflictingFields userId) | ||
| ( onConflictUpdateSetWhere | ||
| ( \(User{userName = fld}) | ||
| (User{userName = excl}) -> | ||
| fld <-. excl | ||
| ) | ||
| ( \(User{userName = fld}) | ||
| (User{userName = excl}) -> | ||
| current_ fld /=. excl | ||
| ) | ||
| ) | ||
|
|
||
| -- Expecting that the conflicting user, User id 2, is also returned | ||
| userId <$> conflicts @?= [1, 2] |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Only a minor comment (that probably should be punted to a separate ticket), but: would it be possible to instead classify the rows by which fields have DEFAULT values? For example, if all the rows to be inserted have DEFAULT in the same position, then we can still insert all the rows at once (although we still need to filter out the DEFAULT fields).
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.
Yes that's very true.
I considered doing something like it, but I started with this simpler implementation instead because the order of insertion is preserved this way. If you grouped rows by the positions of their default values, then you would have to reconstruct the return order.
This isn't impossible, but this is significantly more complex. As you mention, this could be punted to a new ticket.
Actually, I see you have made a prototype implementation for this below; would you like to submit a PR?