Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Postgres migrations to enable extensions #748

Merged
merged 4 commits into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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