From 3815a51f5418c744ca1bc464eb591a7c10ff0ba7 Mon Sep 17 00:00:00 2001 From: Paul Rouse Date: Fri, 24 Nov 2017 11:28:00 +0000 Subject: [PATCH 1/4] Tests: pass WITH_SQLITE to main.hs, to enable MigrationTest and cleanup! --- persistent-test/persistent-test.cabal | 2 ++ 1 file changed, 2 insertions(+) diff --git a/persistent-test/persistent-test.cabal b/persistent-test/persistent-test.cabal index d8b84e8d2..8d356f519 100644 --- a/persistent-test/persistent-test.cabal +++ b/persistent-test/persistent-test.cabal @@ -187,6 +187,8 @@ executable persistent-test , resourcet , scientific + if !flag(postgresql) && !flag(mysql) && !flag(mongodb) && !flag(zookeeper) + cpp-options: -DWITH_SQLITE -DDEBUG if flag(zookeeper) cpp-options: -DWITH_NOSQL -DWITH_ZOOKEEPER -DDEBUG if flag(mongodb) From cac89e1e887153bd10bfa9f89a642ac2af7bd0f6 Mon Sep 17 00:00:00 2001 From: Paul Rouse Date: Fri, 24 Nov 2017 11:30:06 +0000 Subject: [PATCH 2/4] Tests: failing case for #735 --- persistent-test/src/MigrationTest.hs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/persistent-test/src/MigrationTest.hs b/persistent-test/src/MigrationTest.hs index 98239ff82..b917b803d 100644 --- a/persistent-test/src/MigrationTest.hs +++ b/persistent-test/src/MigrationTest.hs @@ -25,6 +25,23 @@ Source field4 TargetId |] +#ifdef WITH_NOSQL +mkPersist persistSettings [persistUpperCase| +#else +share [mkPersist sqlSettings, mkMigrate "migrationAddCol", mkDeleteCascade sqlSettings] [persistLowerCase| +#endif +Target1 sql=target + field1 Int + field2 T.Text + UniqueTarget1 field1 field2 + deriving Eq Show + +Source1 sql=source + field3 Int + extra Int + field4 Target1Id +|] + #ifndef WITH_NOSQL specs :: Spec specs = describe "Migration" $ do @@ -35,4 +52,11 @@ specs = describe "Migration" $ do runMigration migrationMigrate again <- getMigration migrationMigrate liftIO $ again @?= [] + it "can add an extra column" $ db $ do + -- Failing test case for #735. Foreign-key checking, switched on in + -- version 2.6.1, caused persistent-sqlite to generate a `references` + -- constraint in a *temporary* table during migration, which fails. + _ <- runMigration migrationAddCol + again <- getMigration migrationAddCol + liftIO $ again @?= [] #endif From ebad2e28e4a2e10f20f7e076ab832b7c66c905f5 Mon Sep 17 00:00:00 2001 From: Paul Rouse Date: Fri, 24 Nov 2017 11:43:16 +0000 Subject: [PATCH 3/4] Sqlite: do not add foreign key constraints to columns of temporary tables --- persistent-sqlite/Database/Persist/Sqlite.hs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/persistent-sqlite/Database/Persist/Sqlite.hs b/persistent-sqlite/Database/Persist/Sqlite.hs index bd1cccd20..17a09f8be 100644 --- a/persistent-sqlite/Database/Persist/Sqlite.hs +++ b/persistent-sqlite/Database/Persist/Sqlite.hs @@ -429,7 +429,7 @@ mkCreateTable isTemp entity (cols, uniqs) = , " TABLE " , escape $ entityDB entity , "(" - , T.drop 1 $ T.concat $ map sqlColumn cols + , T.drop 1 $ T.concat $ map (sqlColumn isTemp) cols , ", PRIMARY KEY " , "(" , T.intercalate "," $ map (escape . fieldDB) $ compositeFields pdef @@ -447,7 +447,7 @@ mkCreateTable isTemp entity (cols, uniqs) = , showSqlType $ fieldSqlType $ entityId entity ," PRIMARY KEY" , mayDefault $ defaultAttribute $ fieldAttrs $ entityId entity - , T.concat $ map sqlColumn cols + , T.concat $ map (sqlColumn isTemp) cols , T.concat $ map sqlUnique uniqs , ")" ] @@ -457,8 +457,8 @@ mayDefault def = case def of Nothing -> "" Just d -> " DEFAULT " <> d -sqlColumn :: Column -> Text -sqlColumn (Column name isNull typ def _cn _maxLen ref) = T.concat +sqlColumn :: Bool -> Column -> Text +sqlColumn noRef (Column name isNull typ def _cn _maxLen ref) = T.concat [ "," , escape name , " " @@ -467,7 +467,7 @@ sqlColumn (Column name isNull typ def _cn _maxLen ref) = T.concat , mayDefault def , case ref of Nothing -> "" - Just (table, _) -> " REFERENCES " <> escape table + Just (table, _) -> if noRef then "" else " REFERENCES " <> escape table ] sqlUnique :: UniqueDef -> Text From e385fdfc3d877e4281f452c3a5de37c715a824c5 Mon Sep 17 00:00:00 2001 From: Paul Rouse Date: Fri, 24 Nov 2017 14:10:01 +0000 Subject: [PATCH 4/4] Version bump and changelog --- persistent-sqlite/ChangeLog.md | 4 ++++ persistent-sqlite/persistent-sqlite.cabal | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/persistent-sqlite/ChangeLog.md b/persistent-sqlite/ChangeLog.md index 772175e26..a1586feb2 100644 --- a/persistent-sqlite/ChangeLog.md +++ b/persistent-sqlite/ChangeLog.md @@ -1,3 +1,7 @@ +## 2.6.3.1 + +* Fix migration to avoid creating foreign-key constraints in temporary tables [#736](https://github.com/yesodweb/persistent/pull/736) + ## 2.6.3 * Add 'use-pkgconfig' flag to use pkg-config to find system SQLite library. diff --git a/persistent-sqlite/persistent-sqlite.cabal b/persistent-sqlite/persistent-sqlite.cabal index 3f038ccf8..36d96e7a1 100644 --- a/persistent-sqlite/persistent-sqlite.cabal +++ b/persistent-sqlite/persistent-sqlite.cabal @@ -1,5 +1,5 @@ name: persistent-sqlite -version: 2.6.3 +version: 2.6.3.1 license: MIT license-file: LICENSE author: Michael Snoyman