diff options
| author | Teddy Wing | 2017-08-17 22:38:29 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2017-08-17 22:38:29 +0200 | 
| commit | 01e7c70a9ff6a6514706b6de2229c63c83aad65d (patch) | |
| tree | 4bc6b8df07c3ab9bb9105bae2a7092b3c73b33f9 /src/Plugin | |
| parent | ce5e3cf7fe71c5c508c7eaecfb2280b9346e792a (diff) | |
| download | sorbot-01e7c70a9ff6a6514706b6de2229c63c83aad65d.tar.bz2 | |
gitRemoteSetOriginAction: Upsert instead of insert
I had used `INSERT` as a placeholder while trying to get the plugin
working properly. This would create a row every time the "git remote set
origin" command was invoked. What I really wanted was an upsert.
Looked through a number of different ways of accomplishing that in
SQLite:
- https://stackoverflow.com/questions/418898/sqlite-upsert-not-insert-or-replace/7511635#7511635
- https://stackoverflow.com/questions/15277373/sqlite-upsert-update-or-insert
Ended up settling on this solution from CL.:
https://stackoverflow.com/questions/20323174/upsert-in-sqlite/20326705#20326705
It seemed to be pretty clean and understandable, so I leveraged that
approach.
Diffstat (limited to 'src/Plugin')
| -rw-r--r-- | src/Plugin/GitRemoteSetOrigin.hs | 27 | 
1 files changed, 21 insertions, 6 deletions
| diff --git a/src/Plugin/GitRemoteSetOrigin.hs b/src/Plugin/GitRemoteSetOrigin.hs index 8cf2e51..c8a485e 100644 --- a/src/Plugin/GitRemoteSetOrigin.hs +++ b/src/Plugin/GitRemoteSetOrigin.hs @@ -26,12 +26,27 @@ gitRemoteSetOriginAction message = do              let url = last m              dbConn <- liftIO $ open "db/sorbot_development.sqlite3" -            liftIO $ execute dbConn "INSERT INTO \ -                \ plugin_github_commit_channel_repo_urls \ -                \ (channel, repo_url) \ -                \ VALUES \ -                \ (?, ?)" -                (M.channel message, url) +            liftIO $ withTransaction dbConn $ do +                let params = +                        [":channel"   := M.channel message +                        , ":repo_url" := url +                        ] + +                -- Upsert repo URL for channel +                executeNamed dbConn "UPDATE \ +                    \ plugin_github_commit_channel_repo_urls \ +                    \ SET channel = :channel, \ +                    \     repo_url = :repo_url \ +                    \ WHERE channel = :channel" +                    params +                executeNamed dbConn "INSERT INTO \ +                    \ plugin_github_commit_channel_repo_urls \ +                    \ (channel, repo_url) \ +                    \ SELECT \ +                    \     :channel, \ +                    \     :repo_url \ +                    \ WHERE changes() = 0" +                    params              liftIO $ close dbConn              return $ Right $ T.pack url | 
