Skip to content

Commit

Permalink
Merge pull request #748 from signalvine/migrateEnableExtension
Browse files Browse the repository at this point in the history
Allow Postgres migrations to enable extensions
  • Loading branch information
MaxGabriel committed Jan 18, 2018
2 parents 08635a8 + 50111eb commit bad9ea9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions persistent-postgresql/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.6.3

* Added new function `migrateEnableExtension`, to enable Postgres extensions in migrations.

## 2.6.2.2

* Because `text` and `varchar` are synonyms in Postgresql, don't attempt to migrate between them. [#762](https://github.com/yesodweb/persistent/pull/762)
Expand Down
14 changes: 13 additions & 1 deletion persistent-postgresql/Database/Persist/Postgresql.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ViewPatterns #-}
Expand All @@ -26,6 +27,7 @@ module Database.Persist.Postgresql
, tableName
, fieldName
, mockMigration
, migrateEnableExtension
) where

import Database.Persist.Sql
Expand Down Expand Up @@ -74,7 +76,7 @@ import Data.Aeson
import Data.Aeson.Types (modifyFailure)
import Control.Monad (forM)
import Control.Monad.Trans.Reader (runReaderT)
import Control.Monad.Trans.Writer (runWriterT)
import Control.Monad.Trans.Writer (WriterT(..), runWriterT)
import Data.Acquire (Acquire, mkAcquire, with)
import System.Environment (getEnvironment)
import Data.Int (Int64)
Expand Down Expand Up @@ -1185,3 +1187,13 @@ mockMigration mig = do
result = runReaderT $ runWriterT $ runWriterT mig
resp <- result sqlbackend
mapM_ T.putStrLn $ map snd $ snd resp

-- | Enable a Postgres extension. See https://www.postgresql.org/docs/10/static/contrib.html
-- for a list.
migrateEnableExtension :: Text -> Migration
migrateEnableExtension extName = WriterT $ WriterT $ do
res :: [Single Int] <-
rawSql "SELECT COUNT(*) FROM pg_catalog.pg_extension WHERE extname = ?" [PersistText extName]
if res == [Single 0]
then return (((), []) , [(False, "CREATe EXTENSION \"" <> extName <> "\"")])
else return (((), []), [])
2 changes: 1 addition & 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.6.2.2
version: 2.6.3
license: MIT
license-file: LICENSE
author: Felipe Lessa, Michael Snoyman <michael@snoyman.com>
Expand Down

0 comments on commit bad9ea9

Please sign in to comment.