Skip to content

Commit 2aa65e2

Browse files
authored
Fix postgres add reference (#1290)
* Fix add reference * Changelog/cabal * no
1 parent 6de3ffe commit 2aa65e2

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

persistent-postgresql/ChangeLog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog for persistent-postgresql
22

3+
## 2.13.0.3
4+
5+
* [#1290](https://github.com/yesodweb/persistent/pull/1290)
6+
* Fix the code path for adding references to previously defined columns.
7+
38
## 2.13.0.2
49

510
* Actually release the SafeTORemove fix

persistent-postgresql/Database/Persist/Postgresql.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,7 @@ findAlters defs edef col@(Column name isNull sqltype def _gen _defConstraintName
12681268
| Just _oldName /= fmap fieldDB (getEntityIdField edef)
12691269
->
12701270
[AddReference
1271-
(getEntityDBName edef)
1271+
(crTableName colRef)
12721272
(crConstraintName colRef)
12731273
[name]
12741274
(NEL.toList $ Util.dbIdColumnsEsc escapeF refdef)

persistent-postgresql/persistent-postgresql.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: persistent-postgresql
2-
version: 2.13.0.2
2+
version: 2.13.0.3
33
license: MIT
44
license-file: LICENSE
55
author: Felipe Lessa, Michael Snoyman <[email protected]>
@@ -55,6 +55,7 @@ test-suite test
5555
PgIntervalTest
5656
UpsertWhere
5757
ImplicitUuidSpec
58+
MigrationReferenceSpec
5859
ghc-options: -Wall
5960

6061
build-depends: base >= 4.9 && < 5
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{-# LANGUAGE ExistentialQuantification #-}
2+
{-# LANGUAGE OverloadedStrings, DataKinds, FlexibleInstances #-}
3+
{-# LANGUAGE QuasiQuotes #-}
4+
{-# LANGUAGE MultiParamTypeClasses #-}
5+
{-# LANGUAGE TemplateHaskell #-}
6+
{-# LANGUAGE TypeFamilies #-}
7+
{-# LANGUAGE UndecidableInstances #-}
8+
{-# LANGUAGE StandaloneDeriving #-}
9+
{-# LANGUAGE DerivingStrategies #-}
10+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
11+
{-# OPTIONS_GHC -Wno-unused-top-binds #-}
12+
13+
module MigrationReferenceSpec where
14+
15+
import PgInit
16+
17+
import Control.Monad.Trans.Writer (censor, mapWriterT)
18+
import Data.Text (Text, isInfixOf)
19+
20+
share [mkPersist sqlSettings, mkMigrate "referenceMigrate"] [persistLowerCase|
21+
22+
LocationCapabilities
23+
Id Text
24+
bio Text
25+
26+
LocationCapabilitiesPrintingProcess
27+
locationCapabilitiesId LocationCapabilitiesId
28+
29+
LocationCapabilitiesPrintingFinish
30+
locationCapabilitiesId LocationCapabilitiesId
31+
32+
LocationCapabilitiesSubstrate
33+
locationCapabilitiesId LocationCapabilitiesId
34+
35+
|]
36+
37+
spec :: Spec
38+
spec = describe "MigrationReferenceSpec" $ do
39+
it "works" $ runConnAssert $ do
40+
let
41+
noForeignKeys :: CautiousMigration -> CautiousMigration
42+
noForeignKeys = filter ((not . isReference) . snd)
43+
44+
onlyForeignKeys :: CautiousMigration -> CautiousMigration
45+
onlyForeignKeys = filter (isReference . snd)
46+
47+
isReference :: Text -> Bool
48+
isReference migration = "REFERENCES" `isInfixOf` migration
49+
50+
runMigration
51+
$ mapWriterT (censor noForeignKeys)
52+
$ referenceMigrate
53+
54+
runMigration
55+
$ mapWriterT (censor onlyForeignKeys)
56+
$ referenceMigrate

persistent-postgresql/test/main.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import qualified CustomConstraintTest
5757
import qualified LongIdentifierTest
5858
import qualified PgIntervalTest
5959
import qualified GeneratedColumnTestSQL
60+
import qualified MigrationReferenceSpec
6061

6162
type Tuple = (,)
6263

@@ -138,6 +139,7 @@ main = do
138139

139140
hspec $ do
140141
ImplicitUuidSpec.spec
142+
MigrationReferenceSpec.spec
141143
RenameTest.specsWith runConnAssert
142144
DataTypeTest.specsWith runConnAssert
143145
(Just (runMigrationSilent dataTypeMigrate))

0 commit comments

Comments
 (0)