yesodweb / yesodweb/persistent

updateWhere with SelectOpt

Open
#692 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement MySQL
Dominant language
Haskell
Stars
486
Forks
306
PR merge metrics
No merged PRs in 30d

Description

Currently selectList, updateWhere types is following:

selectList  :: [Filter val] -> [SelectOpt val] -> ReaderT backend m [Entity val]
updateWhere :: [Filter val] -> [Update val]    -> ReaderT backend m ()

But, I want to this.

updateWhere :: [Filter val] -> [Update val] -> [SelectOpt val] -> ReaderT backend m ()

Because, ran into this problem.

#!/usr/bin/env stack
-- stack --resolver lts-9.1 --install-ghc runghc --package persistent --package persistent-sqlite
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}

import Control.Monad.IO.Class (liftIO)
import Database.Persist
import Database.Persist.Sqlite
import Database.Persist.TH

share
  [mkPersist sqlSettings, mkMigrate "migrateAll"]
  [persistLowerCase|
    Item
        name String
        order Int
        deriving Show
        UniqueOrder order
  |]

main :: IO ()
main =
  runSqlite ":memory:" $ do
    runMigration migrateAll
    t1 <- insert $ Item "test1" 1
    t2 <- insert $ Item "test2" 2
    t3 <- insert $ Item "test3" 3
    updateWhere [] [ItemOrder +=. 1] -- NG uniqueness error
    updateWhere [] [ItemOrder -=. 1] -- OK
    items <- selectList [] []
    liftIO $ print (items :: [Entity Item])
    deleteWhere ([] :: [Filter Item])

Error message;

test.hs: SQLite3 returned ErrorConstraint while attempting to perform step.

If can write updateWhere [] [ItemOrder +=. 1] [Desc ItemOrder] then nothing problem.

mysql 5.6 suport this syntax.

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
    SET assignment_list
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]

I tried make function updateWhereWithOpt, but many used internal functions.

Can I submit PR?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in persistent/Database/Persist/Sql/Orphan/PersistQuery.hs around the referenced update functions and compare how selectList handles SelectOpt values. Use the supplied SQLite reproduction to define done: updateWhere accepts ordering options and the ordered increment avoids the uniqueness error, with the supported backend behavior covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.