From 01e7c70a9ff6a6514706b6de2229c63c83aad65d Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 17 Aug 2017 22:38:29 +0200 Subject: 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. --- src/Plugin/GitRemoteSetOrigin.hs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'src/Plugin/GitRemoteSetOrigin.hs') 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 -- cgit v1.2.3