Skip to content

Commit

Permalink
Handle qualified table name (#1477)
Browse files Browse the repository at this point in the history
* handle qualified table name

* further handle qualifier. and drop the qualifier when entityMap lookup
in extractForeignRef

* working solution (pass tests and handle conflicting name in Proxy)

* remove stale code

* merge master

* neat

* changelog

---------

Co-authored-by: parsonsmatt <parsonsmatt@gmail.com>
  • Loading branch information
wavewave and parsonsmatt committed Oct 3, 2023
1 parent 68b7ca7 commit bd32280
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion persistent-redis/persistent-redis.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ library
, scientific >= 0.3.5 && < 0.4
, text >= 1.2
, time >= 1.6
, transformers >= 0.5
, transformers >= 0.5
, utf8-string >= 1.0 && < 1.1

exposed-modules: Database.Persist.Redis
Expand Down
2 changes: 2 additions & 0 deletions persistent/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 2.14.6.0 (unreleased)

* [#1477](https://github.com/yesodweb/persistent/pull/1477)
* Qualified references to other tables will work
* [#1503](https://github.com/yesodweb/persistent/pull/1503)
* Create Haddocks from entity documentation comments
* [1497](https://github.com/yesodweb/persistent/pull/1497)
Expand Down
28 changes: 27 additions & 1 deletion persistent/Database/Persist/TH.hs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,9 @@ liftAndFixKeys mps emEntities entityMap unboundEnt =
(fieldRef', sqlTyp') =
case extractForeignRef entityMap ufd of
Just targetTable ->
(lift (ForeignRef targetTable), liftSqlTypeExp (SqlTypeReference targetTable))
let targetTableQualified =
fromMaybe targetTable (guessFieldReferenceQualified ufd)
in (lift (ForeignRef targetTable), liftSqlTypeExp (SqlTypeReference targetTableQualified))
Nothing ->
(lift NoReference, liftSqlTypeExp sqlTypeExp)

Expand Down Expand Up @@ -537,6 +539,30 @@ guessReference ft =
guessReferenceText (Just next)
]

guessFieldReferenceQualified :: UnboundFieldDef -> Maybe EntityNameHS
guessFieldReferenceQualified = guessReferenceQualified . unboundFieldType

guessReferenceQualified :: FieldType -> Maybe EntityNameHS
guessReferenceQualified ft =
EntityNameHS <$> guessReferenceText (Just ft)
where
checkIdSuffix =
T.stripSuffix "Id"
guessReferenceText mft =
asum
[ do
FTTypeCon mmod (checkIdSuffix -> Just tableName) <- mft
-- handle qualified name.
pure $ maybe tableName (\qualName -> qualName <> "." <> tableName) mmod
, do
FTApp (FTTypeCon _ "Key") (FTTypeCon mmod tableName) <- mft
-- handle qualified name.
pure $ maybe tableName (\qualName -> qualName <> "." <> tableName) mmod
, do
FTApp (FTTypeCon _ "Maybe") next <- mft
guessReferenceText (Just next)
]

mkDefaultKey
:: MkPersistSettings
-> FieldNameDB
Expand Down
13 changes: 12 additions & 1 deletion persistent/test/Database/Persist/THSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ import qualified Database.Persist.TH.TypeLitFieldDefsSpec as TypeLitFieldDefsSpe
-- machinery
type TextId = Text

share [mkPersist sqlSettings { mpsGeneric = False, mpsDeriveInstances = [''Generic] }] [persistUpperCase|
share [mkPersistWith sqlSettings { mpsGeneric = False, mpsDeriveInstances = [''Generic] } [entityDef @JsonEncodingSpec.JsonEncoding Proxy]] [persistUpperCase|

Person json
name Text
Expand All @@ -102,6 +102,10 @@ CustomIdName
Id sql=id_col
name Text
deriving Show Eq

QualifiedReference
jsonEncoding JsonEncodingSpec.JsonEncodingId

|]

mkPersist sqlSettings [persistLowerCase|
Expand Down Expand Up @@ -207,6 +211,13 @@ spec = describe "THSpec" $ do
CommentSpec.spec
EntityHaddockSpec.spec
CompositeKeyStyleSpec.spec
it "QualifiedReference" $ do
let ed = entityDef @QualifiedReference Proxy
[FieldDef {..}] = entityFields ed
fieldType `shouldBe` FTTypeCon (Just "JsonEncodingSpec") "JsonEncodingId"
fieldSqlType `shouldBe` sqlType @JsonEncodingSpec.JsonEncodingId Proxy
fieldReference `shouldBe` ForeignRef (EntityNameHS "JsonEncoding")

describe "TestDefaultKeyCol" $ do
let EntityIdField FieldDef{..} =
entityId (entityDef (Proxy @TestDefaultKeyCol))
Expand Down

0 comments on commit bd32280

Please sign in to comment.