Skip to content

Commit

Permalink
Fix postgres add reference (#1290)
Browse files Browse the repository at this point in the history
* Fix add reference

* Changelog/cabal

* no
  • Loading branch information
parsonsmatt authored Jun 18, 2021
1 parent 6de3ffe commit 2aa65e2
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
5 changes: 5 additions & 0 deletions persistent-postgresql/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog for persistent-postgresql

## 2.13.0.3

* [#1290](https://github.com/yesodweb/persistent/pull/1290)
* Fix the code path for adding references to previously defined columns.

## 2.13.0.2

* Actually release the SafeTORemove fix
Expand Down
2 changes: 1 addition & 1 deletion persistent-postgresql/Database/Persist/Postgresql.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ findAlters defs edef col@(Column name isNull sqltype def _gen _defConstraintName
| Just _oldName /= fmap fieldDB (getEntityIdField edef)
->
[AddReference
(getEntityDBName edef)
(crTableName colRef)
(crConstraintName colRef)
[name]
(NEL.toList $ Util.dbIdColumnsEsc escapeF refdef)
Expand Down
3 changes: 2 additions & 1 deletion persistent-postgresql/persistent-postgresql.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: persistent-postgresql
version: 2.13.0.2
version: 2.13.0.3
license: MIT
license-file: LICENSE
author: Felipe Lessa, Michael Snoyman <michael@snoyman.com>
Expand Down Expand Up @@ -55,6 +55,7 @@ test-suite test
PgIntervalTest
UpsertWhere
ImplicitUuidSpec
MigrationReferenceSpec
ghc-options: -Wall

build-depends: base >= 4.9 && < 5
Expand Down
56 changes: 56 additions & 0 deletions persistent-postgresql/test/MigrationReferenceSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE OverloadedStrings, DataKinds, FlexibleInstances #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# OPTIONS_GHC -Wno-unused-top-binds #-}

module MigrationReferenceSpec where

import PgInit

import Control.Monad.Trans.Writer (censor, mapWriterT)
import Data.Text (Text, isInfixOf)

share [mkPersist sqlSettings, mkMigrate "referenceMigrate"] [persistLowerCase|

LocationCapabilities
Id Text
bio Text

LocationCapabilitiesPrintingProcess
locationCapabilitiesId LocationCapabilitiesId

LocationCapabilitiesPrintingFinish
locationCapabilitiesId LocationCapabilitiesId

LocationCapabilitiesSubstrate
locationCapabilitiesId LocationCapabilitiesId

|]

spec :: Spec
spec = describe "MigrationReferenceSpec" $ do
it "works" $ runConnAssert $ do
let
noForeignKeys :: CautiousMigration -> CautiousMigration
noForeignKeys = filter ((not . isReference) . snd)

onlyForeignKeys :: CautiousMigration -> CautiousMigration
onlyForeignKeys = filter (isReference . snd)

isReference :: Text -> Bool
isReference migration = "REFERENCES" `isInfixOf` migration

runMigration
$ mapWriterT (censor noForeignKeys)
$ referenceMigrate

runMigration
$ mapWriterT (censor onlyForeignKeys)
$ referenceMigrate
2 changes: 2 additions & 0 deletions persistent-postgresql/test/main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import qualified CustomConstraintTest
import qualified LongIdentifierTest
import qualified PgIntervalTest
import qualified GeneratedColumnTestSQL
import qualified MigrationReferenceSpec

type Tuple = (,)

Expand Down Expand Up @@ -138,6 +139,7 @@ main = do

hspec $ do
ImplicitUuidSpec.spec
MigrationReferenceSpec.spec
RenameTest.specsWith runConnAssert
DataTypeTest.specsWith runConnAssert
(Just (runMigrationSilent dataTypeMigrate))
Expand Down

0 comments on commit 2aa65e2

Please sign in to comment.