packages feed

postgresql-simple-migration 0.1.3.0 → 0.1.5.0

raw patch · 8 files changed

+104/−29 lines, 8 filesdep +textdep ~hspecdep ~postgresql-simpledep ~timePVP: major bump suggested

API removals or changes: PVP suggests a major version bump

Dependencies added: text

Dependency ranges changed: hspec, postgresql-simple, time

API changes (from Hackage documentation)

- Database.PostgreSQL.Simple.Migration: instance Eq CheckScriptResult
- Database.PostgreSQL.Simple.Migration: instance Eq MigrationCommand
- Database.PostgreSQL.Simple.Migration: instance Eq SchemaMigration
- Database.PostgreSQL.Simple.Migration: instance Eq a => Eq (MigrationResult a)
- Database.PostgreSQL.Simple.Migration: instance FromRow SchemaMigration
- Database.PostgreSQL.Simple.Migration: instance Ord CheckScriptResult
- Database.PostgreSQL.Simple.Migration: instance Ord MigrationCommand
- Database.PostgreSQL.Simple.Migration: instance Ord SchemaMigration
- Database.PostgreSQL.Simple.Migration: instance Ord a => Ord (MigrationResult a)
- Database.PostgreSQL.Simple.Migration: instance Read CheckScriptResult
- Database.PostgreSQL.Simple.Migration: instance Read MigrationCommand
- Database.PostgreSQL.Simple.Migration: instance Read SchemaMigration
- Database.PostgreSQL.Simple.Migration: instance Read a => Read (MigrationResult a)
- Database.PostgreSQL.Simple.Migration: instance Show CheckScriptResult
- Database.PostgreSQL.Simple.Migration: instance Show MigrationCommand
- Database.PostgreSQL.Simple.Migration: instance Show SchemaMigration
- Database.PostgreSQL.Simple.Migration: instance Show a => Show (MigrationResult a)
- Database.PostgreSQL.Simple.Migration: instance ToRow SchemaMigration
- Database.PostgreSQL.Simple.Migration: migrationContextCommand :: MigrationContext -> MigrationCommand
- Database.PostgreSQL.Simple.Migration: migrationContextConnection :: MigrationContext -> Connection
- Database.PostgreSQL.Simple.Migration: migrationContextVerbose :: MigrationContext -> Bool
- Database.PostgreSQL.Simple.Migration: schemaMigrationChecksum :: SchemaMigration -> Checksum
- Database.PostgreSQL.Simple.Migration: schemaMigrationExecutedAt :: SchemaMigration -> LocalTime
- Database.PostgreSQL.Simple.Migration: schemaMigrationName :: SchemaMigration -> ByteString
+ Database.PostgreSQL.Simple.Migration: [migrationContextCommand] :: MigrationContext -> MigrationCommand
+ Database.PostgreSQL.Simple.Migration: [migrationContextConnection] :: MigrationContext -> Connection
+ Database.PostgreSQL.Simple.Migration: [migrationContextVerbose] :: MigrationContext -> Bool
+ Database.PostgreSQL.Simple.Migration: [schemaMigrationChecksum] :: SchemaMigration -> Checksum
+ Database.PostgreSQL.Simple.Migration: [schemaMigrationExecutedAt] :: SchemaMigration -> LocalTime
+ Database.PostgreSQL.Simple.Migration: [schemaMigrationName] :: SchemaMigration -> ByteString
+ Database.PostgreSQL.Simple.Migration: instance Database.PostgreSQL.Simple.FromRow.FromRow Database.PostgreSQL.Simple.Migration.SchemaMigration
+ Database.PostgreSQL.Simple.Migration: instance Database.PostgreSQL.Simple.ToRow.ToRow Database.PostgreSQL.Simple.Migration.SchemaMigration
+ Database.PostgreSQL.Simple.Migration: instance GHC.Classes.Eq Database.PostgreSQL.Simple.Migration.CheckScriptResult
+ Database.PostgreSQL.Simple.Migration: instance GHC.Classes.Eq Database.PostgreSQL.Simple.Migration.MigrationCommand
+ Database.PostgreSQL.Simple.Migration: instance GHC.Classes.Eq Database.PostgreSQL.Simple.Migration.SchemaMigration
+ Database.PostgreSQL.Simple.Migration: instance GHC.Classes.Eq a => GHC.Classes.Eq (Database.PostgreSQL.Simple.Migration.MigrationResult a)
+ Database.PostgreSQL.Simple.Migration: instance GHC.Classes.Ord Database.PostgreSQL.Simple.Migration.CheckScriptResult
+ Database.PostgreSQL.Simple.Migration: instance GHC.Classes.Ord Database.PostgreSQL.Simple.Migration.MigrationCommand
+ Database.PostgreSQL.Simple.Migration: instance GHC.Classes.Ord Database.PostgreSQL.Simple.Migration.SchemaMigration
+ Database.PostgreSQL.Simple.Migration: instance GHC.Classes.Ord a => GHC.Classes.Ord (Database.PostgreSQL.Simple.Migration.MigrationResult a)
+ Database.PostgreSQL.Simple.Migration: instance GHC.Read.Read Database.PostgreSQL.Simple.Migration.CheckScriptResult
+ Database.PostgreSQL.Simple.Migration: instance GHC.Read.Read Database.PostgreSQL.Simple.Migration.MigrationCommand
+ Database.PostgreSQL.Simple.Migration: instance GHC.Read.Read Database.PostgreSQL.Simple.Migration.SchemaMigration
+ Database.PostgreSQL.Simple.Migration: instance GHC.Read.Read a => GHC.Read.Read (Database.PostgreSQL.Simple.Migration.MigrationResult a)
+ Database.PostgreSQL.Simple.Migration: instance GHC.Show.Show Database.PostgreSQL.Simple.Migration.CheckScriptResult
+ Database.PostgreSQL.Simple.Migration: instance GHC.Show.Show Database.PostgreSQL.Simple.Migration.MigrationCommand
+ Database.PostgreSQL.Simple.Migration: instance GHC.Show.Show Database.PostgreSQL.Simple.Migration.SchemaMigration
+ Database.PostgreSQL.Simple.Migration: instance GHC.Show.Show a => GHC.Show.Show (Database.PostgreSQL.Simple.Migration.MigrationResult a)

Files

Changelog.markdown view
@@ -1,5 +1,15 @@ # Changelog +## 0.1.5.0+* Bumped dependencies++## 0.1.4.0+* Improved error logging in standalone binary++## 0.1.3.0+* Better transaction handling+* Improved documentation+ ## 0.1.2.0 * Moved Util module * Improved documentation
Readme.markdown view
@@ -72,7 +72,7 @@ main = do     let url = "host=$host dbname=$db user=$user password=$pw"     con <- connectPostgreSQL (BS8.pack url)-    withTransaction con $ runMigration $ +    withTransaction con $ runMigration $         MigrationContext MigrationInitialization True con ``` @@ -84,7 +84,7 @@     let url = "host=$host dbname=$db user=$user password=$pw"     let dir = "."     con <- connectPostgreSQL (BS8.pack url)-    withTransaction con $ runMigration $ +    withTransaction con $ runMigration $         MigrationContext (MigrationDirectory dir) True con ``` @@ -97,7 +97,7 @@     let name = "my script"     let script = "create table users (email varchar not null)";     con <- connectPostgreSQL (BS8.pack url)-    withTransaction con $ runMigration $ +    withTransaction con $ runMigration $         MigrationContext (MigrationScript name script) True con ``` @@ -112,19 +112,21 @@ main = do     let url = "host=$host dbname=$db user=$user password=$pw"     con <- connectPostgreSQL (BS8.pack url)-    withTransaction con $ runMigration $ MigrationContext +    withTransaction con $ runMigration $ MigrationContext         (MigrationValidation (MigrationDirectory dir)) True con ``` -Database migrations should always be performed in a transactional context. +Database migrations should always be performed in a transactional context.  The standalone binary takes care of proper transaction handling automatically.  The library does not make any assumptions about the current transactional state-of the system. This way you can execute multiple migration-commands or-validations in sequence while still staying in the same transaction.+of the system. This means that the caller of the library has to take care of+opening/closing/rolling-back transactions. This way you can execute multiple+migration-commands or validations in sequence while still staying in the+transaction you opened. -The tests work in a similar way. After executing all migration-tests, the +The tests make use of this. After executing all migration-tests, the transaction is rolled back.  ## Compilation and Tests@@ -140,6 +142,20 @@  ```bash cabal configure --enable-tests && cabal test+```++To build the project in a cabal sandbox, use the following code:++```bash+cabal sandbox init+cabal install -j --only-dependencies --enable-tests --disable-documentation+cabal configure --enable-tests+cabal test+```++To remove the generated cabal sandbox, use:+```bash+cabal sandbox delete ```  ## To Do
postgresql-simple-migration.cabal view
@@ -1,5 +1,5 @@ name:                       postgresql-simple-migration-version:                    0.1.3.0+version:                    0.1.5.0 synopsis:                   PostgreSQL Schema Migrations homepage:                   https://github.com/ameingast/postgresql-simple-migration Bug-reports:                https://github.com/ameingast/postgresql-simple-migration/issues@@ -7,7 +7,7 @@ license-file:               License author:                     Andreas Meingast <ameingast@gmail.com> maintainer:                 Andreas Meingast <ameingast@gmail.com>-copyright:                  2014, Andreas Meingast+copyright:                  2014-2016, Andreas Meingast category:                   Database build-type:                 Simple cabal-version:              >= 1.10@@ -22,7 +22,7 @@                              test/*.hs                             test/Database/PostgreSQL/Simple/*.hs-                            +                             share/test/*.sql                             share/test/scripts/*.sql @@ -35,39 +35,40 @@                             Database.PostgreSQL.Simple.Util     hs-source-dirs:         src     ghc-options:            -Wall -fwarn-tabs -fwarn-incomplete-uni-patterns-    default-extensions:     OverloadedStrings+    default-extensions:     OverloadedStrings, CPP     default-language:       Haskell2010     build-depends:          base                        >= 4.6      && < 5.0,                             base64-bytestring           >= 1.0      && < 1.1,                             bytestring                  >= 0.10     && < 0.11,                             cryptohash                  >= 0.11     && < 0.12,-                            directory                   >= 1.2      && < 1.3,    -                            postgresql-simple           >= 0.4      && < 0.5,-                            time                        >= 1.4      && < 1.5+                            directory                   >= 1.2      && < 1.3,+                            postgresql-simple           >= 0.4      && < 0.6,+                            time                        >= 1.4      && < 1.6  Executable migrate     main-is:                Main.hs     hs-source-dirs:         src     ghc-options:            -Wall -fwarn-tabs -fwarn-incomplete-uni-patterns-    default-extensions:     OverloadedStrings+    default-extensions:     OverloadedStrings, CPP     default-language:       Haskell2010     build-depends:          base                        >= 4.6      && < 5.0,                             base64-bytestring           >= 1.0      && < 1.1,                             bytestring                  >= 0.10     && < 0.11,                             cryptohash                  >= 0.11     && < 0.12,-                            directory                   >= 1.2      && < 1.3,    -                            postgresql-simple           >= 0.4      && < 0.5,-                            time                        >= 1.4      && < 1.5+                            directory                   >= 1.2      && < 1.3,+                            postgresql-simple           >= 0.4      && < 0.6,+                            time                        >= 1.4      && < 1.6,+                            text                        >= 1.2      && < 1.3  test-suite tests     main-is:                Main.hs     hs-source-dirs:         test     ghc-options:            -Wall -fwarn-tabs -fwarn-incomplete-uni-patterns-    default-extensions:     OverloadedStrings+    default-extensions:     OverloadedStrings, CPP     default-language:       Haskell2010     type:                   exitcode-stdio-1.0     build-depends:          base                        >= 4.6      && < 5.0,                             bytestring                  >= 0.10     && < 0.11,-                            postgresql-simple           >= 0.4      && < 0.5,-                            hspec                       >= 1.10     && < 1.11,+                            postgresql-simple           >= 0.4      && < 0.6,+                            hspec                       >= 2.2      && < 2.3,                             postgresql-simple-migration >= 0.1      && < 0.2
src/Database/PostgreSQL/Simple/Migration.hs view
@@ -11,6 +11,9 @@ -- -- For usage, see Readme.markdown. +{-# LANGUAGE CPP               #-}+{-# LANGUAGE OverloadedStrings #-}+ module Database.PostgreSQL.Simple.Migration     (     -- * Migration actions@@ -30,13 +33,17 @@     , SchemaMigration(..)     ) where +#if __GLASGOW_HASKELL__ < 710 import           Control.Applicative                ((<$>), (<*>))-import           Control.Monad                      (liftM, void, when)+#endif+import           Control.Monad                      (void, when) import qualified Crypto.Hash.MD5                    as MD5 (hash) import qualified Data.ByteString                    as BS (ByteString, readFile) import qualified Data.ByteString.Base64             as B64 (encode) import           Data.List                          (isPrefixOf, sort)+#if __GLASGOW_HASKELL__ < 710 import           Data.Monoid                        (mconcat)+#endif import           Data.Time                          (LocalTime) import           Database.PostgreSQL.Simple         (Connection, Only (..),                                                      execute, execute_, query,@@ -86,7 +93,7 @@ -- | Lists all files in the given 'FilePath' 'dir' in alphabetical order. scriptsInDirectory :: FilePath -> IO [String] scriptsInDirectory dir =-    liftM (sort . filter (\x -> not $ "." `isPrefixOf` x))+    fmap (sort . filter (\x -> not $ "." `isPrefixOf` x))         (getDirectoryContents dir)  -- | Executes a generic SQL migration for the provided script 'name' with
src/Database/PostgreSQL/Simple/Util.hs view
@@ -9,20 +9,22 @@ -- -- A collection of utilites for database migrations. +{-# LANGUAGE CPP               #-}+{-# LANGUAGE OverloadedStrings #-}+ module Database.PostgreSQL.Simple.Util     ( existsTable     , withTransactionRolledBack     ) where  import           Control.Exception          (finally)-import           Control.Monad              (liftM) import           Database.PostgreSQL.Simple (Connection, Only (..), begin,                                              query, rollback)  -- | Checks if the table with the given name exists in the database. existsTable :: Connection -> String -> IO Bool existsTable con table =-    liftM (not . null) (query con q (Only table) :: IO [[Int]])+    fmap (not . null) (query con q (Only table) :: IO [[Int]])     where         q = "select count(relname) from pg_class where relname = ?" 
src/Main.hs view
@@ -9,13 +9,21 @@ -- -- A standalone program for the postgresql-simple-migration library. +{-# LANGUAGE CPP               #-}+{-# LANGUAGE OverloadedStrings #-}+ module Main (     main     ) where +#if __GLASGOW_HASKELL__ < 710+import           Control.Applicative+#endif+import           Control.Exception import           Control.Monad                        (void) import qualified Data.ByteString.Char8                as BS8 (pack)-import           Database.PostgreSQL.Simple           (connectPostgreSQL,+import           Database.PostgreSQL.Simple           (SqlError (..),+                                                       connectPostgreSQL,                                                        withTransaction) import           Database.PostgreSQL.Simple.Migration (MigrationCommand (..),                                                        MigrationContext (..),@@ -23,14 +31,39 @@ import           System.Environment                   (getArgs) import           System.Exit                          (exitFailure) +import qualified Data.Text                            as T+import qualified Data.Text.Encoding                   as T+ main :: IO () main = getArgs >>= \args -> case args of     "-h":_ ->         printUsage     "-q":xs ->-        run (parseCommand xs) False+        ppException $ run (parseCommand xs) False     xs ->-        run (parseCommand xs) True+        ppException $ run (parseCommand xs) True++-- | Pretty print postgresql-simple exceptions to see whats going on+ppException :: IO a -> IO a+ppException a = catch a ehandler+  where+    ehandler e = maybe (throw e) (*> exitFailure)+                 (pSqlError <$> fromException e)+            -- <|> (pOtherError <$> fromException e)+    bsToString = T.unpack . T.decodeUtf8+    pSqlError e = mapM_ putStrLn+                  [ "SqlError:"+                  , "  sqlState: "+                  , bsToString $ sqlState e+                  , "  sqlExecStatus: "+                  , show $ sqlExecStatus e+                  , "  sqlErrorMsg: "+                  , bsToString $ sqlErrorMsg e+                  , "  sqlErrorDetail: "+                  , bsToString $ sqlErrorDetail e+                  , "  sqlErrorHint: "+                  , bsToString $ sqlErrorHint e+                  ]  run :: Maybe Command -> Bool-> IO () run Nothing  _ = printUsage >> exitFailure
test/Database/PostgreSQL/Simple/MigrationTest.hs view
@@ -9,6 +9,9 @@ -- -- A collection of postgresql-simple-migration specifications. +{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE CPP #-}+ module Database.PostgreSQL.Simple.MigrationTest where  import           Database.PostgreSQL.Simple           (Connection)
test/Main.hs view
@@ -9,6 +9,9 @@ -- -- The test entry-point for postgresql-simple-migration. +{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE CPP #-}+ module Main     ( main     ) where