yesodweb / yesodweb/persistent

"default" expressions in persistentmodels files no longer parse as before

Open
#1,620 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Something changed between persistent-2.15.1.0 & persistent-2.16.0.0 (and later); "default" expressions that used to parse now fail to compile.

The quasiquoter does not like the expression default=to_jsonb('{"!v":0}'::json) under 2.18; fortunately there is a workaround in this case (default='{"!v":0}'::jsonb) but that might also trigger an unnecessary migration.

I've tested with GHC 9.10.3 and 9.12.2, with the same results.

This program demonstrates the bug with ghc --make (and the appropriate persistent library installed):

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

module Main where

import Data.Text (Text)
import Database.Persist.TH (mkPersist, persistLowerCase, share, sqlSettings)

share
  [mkPersist sqlSettings]
  [persistLowerCase|
DBThing
  name Text
  metadata1 Text default=to_jsonb('{"!v":0}'::json)
  metadata2 Text default='{"!v":0}'::jsonb
  deriving Show Eq
|]

main :: IO ()
main = pure ()

Compiled as above, GHC will report:

persistent-default-bug.hs:15:21: error: [GHC-39584]
    • persistent-default-bug.hs:4:36:
  |
4 |   metadata1 Text default=to_jsonb('{"!v":0}'::json)
  |                                    ^
unexpected '{'
expecting '!', '"', ''', '(', ')', ',', '-', '.', ':', '[', '\', ']', '_', '~', alphanumeric character, assignment expression, end of input, newline, parenthetical, or plain attribute

    • In the quasi-quotation:
        [persistLowerCase|
DBThing
  name Text
  metadata1 Text default=to_jsonb('{"!v":0}'::json)
  metadata2 Text default='{"!v":0}'::jsonb
  deriving Show Eq
|]
   |
15 |   [persistLowerCase|
   |                     ^...

If metadata1 is commented out, the program compiles as expected.

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 with the persistent-default-bug.hs reproducer and run it with ghc --make and the appropriate persistent library installed. Trace the persistLowerCase quasiquotation and its handling of default expressions. Done means the to_jsonb('{"!v":0}'::json) default parses and the reproducer compiles without requiring the workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell, postgresql
Domain
databases, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.