Skip to content

Commit

Permalink
Merge pull request #5 from tvh/upgrade-hasql
Browse files Browse the repository at this point in the history
Port to new hasql interface
  • Loading branch information
tvh authored Jun 26, 2018
2 parents 4eb815c + 4b91480 commit bba0b3a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ before_script:
- psql -c 'create database test;' -U postgres

env:
- CABALVER=1.22 GHCVER=7.10.3
- CABALVER=1.24 GHCVER=8.0.1
- CABALVER=2.0 GHCVER=8.2.2
- CABALVER=2.2 GHCVER=8.4.3

before_install:
- travis_retry sudo add-apt-repository -y ppa:hvr/ghc
Expand Down
8 changes: 4 additions & 4 deletions hasql-migration.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: hasql-migration
version: 0.1.4
version: 0.2.0
synopsis: PostgreSQL Schema Migrations
homepage: https://github.com/tvh/hasql-migration
Bug-reports: https://github.com/tvh/hasql-migration/issues
Expand All @@ -12,7 +12,7 @@ category: Database
build-type: Simple
cabal-version: >= 1.10
description: A PostgreSQL-simple schema migration utility

tested-with: GHC==8.0.1, GHC==8.2.2, GHC==8.4.3
extra-source-files: License
Readme.markdown

Expand All @@ -38,8 +38,8 @@ library
cryptonite >= 0.11,
data-default-class >= 0.0.1,
directory >= 1.2,
hasql >= 0.19,
hasql-transaction >= 0.5,
hasql >= 1.3,
hasql-transaction >= 0.7,
memory,
text >= 1.2,
time >= 1.4
Expand Down
14 changes: 7 additions & 7 deletions src/Hasql/Migration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import Data.List (isPrefixOf, sort)
import Data.Time (LocalTime)
import Data.Traversable (forM)
import Hasql.Migration.Util (existsTable)
import Hasql.Query
import Hasql.Statement
import Hasql.Transaction
import System.Directory (getDirectoryContents)
import qualified Data.ByteString as BS (ByteString, readFile)
Expand Down Expand Up @@ -95,7 +95,7 @@ executeMigration name contents = do
return Nothing
ScriptNotExecuted -> do
sql contents
query (name, checksum) (statement q (contramap (first T.pack) def) Decoders.unit False)
statement (name, checksum) (Statement q (contramap (first T.pack) def) Decoders.unit False)
return Nothing
ScriptModified _ -> do
return (Just $ ScriptChanged name)
Expand Down Expand Up @@ -147,7 +147,7 @@ executeValidation cmd = case cmd of
-- will be executed and its meta-information will be recorded.
checkScript :: ScriptName -> Checksum -> Transaction CheckScriptResult
checkScript name checksum =
query name (statement q (contramap T.pack (Encoders.value def)) (Decoders.maybeRow (Decoders.value def)) False) >>= \case
statement name (Statement q (contramap T.pack (Encoders.param def)) (Decoders.rowMaybe (Decoders.column def)) False) >>= \case
Nothing ->
return ScriptNotExecuted
Just actualChecksum | checksum == actualChecksum ->
Expand Down Expand Up @@ -201,7 +201,7 @@ data MigrationError = ScriptChanged String | NotInitialised | ScriptMissing Stri
-- | Produces a list of all executed 'SchemaMigration's.
getMigrations :: Transaction [SchemaMigration]
getMigrations =
query () $ statement q def (Decoders.rowsList decodeSchemaMigration) False
statement () $ Statement q def (Decoders.rowList decodeSchemaMigration) False
where
q = mconcat
[ "select filename, checksum, executed_at "
Expand All @@ -225,6 +225,6 @@ instance Ord SchemaMigration where
decodeSchemaMigration :: Decoders.Row SchemaMigration
decodeSchemaMigration =
SchemaMigration
<$> Decoders.value def
<*> Decoders.value def
<*> Decoders.value def
<$> Decoders.column def
<*> Decoders.column def
<*> Decoders.column def
8 changes: 4 additions & 4 deletions src/Hasql/Migration/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ module Hasql.Migration.Util
( existsTable
) where

import Hasql.Query
import Hasql.Statement
import qualified Hasql.Encoders as Encoders
import qualified Hasql.Decoders as Decoders
import Hasql.Transaction (query, Transaction)
import Hasql.Transaction (statement, Transaction)
import Data.Text (Text)
import Data.Default.Class

-- | Checks if the table with the given name exists in the database.
existsTable :: Text -> Transaction Bool
existsTable table =
fmap (not . null) $ query table q
fmap (not . null) $ statement table q
where
q = statement sql (Encoders.value def) (Decoders.rowsList (Decoders.value Decoders.int8)) False
q = Statement sql (Encoders.param def) (Decoders.rowList (Decoders.column Decoders.int8)) False
sql = "select relname from pg_class where relname = $1"
4 changes: 2 additions & 2 deletions test/Hasql/MigrationTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

module Hasql.MigrationTest where

import Hasql.Session (run, Error)
import Hasql.Session (run, QueryError)
import Hasql.Connection
import qualified Hasql.Transaction as Tx
import qualified Hasql.Transaction.Sessions as Tx
Expand All @@ -23,7 +23,7 @@ import Hasql.Migration.Util (existsTable)
import Test.Hspec (Spec, describe, it,
shouldBe, runIO)

runTx :: Connection -> Tx.Transaction a -> IO (Either Error a)
runTx :: Connection -> Tx.Transaction a -> IO (Either QueryError a)
runTx con act = do
run (Tx.transaction Tx.ReadCommitted Tx.Write act) con

Expand Down
2 changes: 1 addition & 1 deletion test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Main

import Hasql.Connection
import Hasql.MigrationTest
import Test.Hspec (hspec)
import Test.Hspec (hspec)

main :: IO ()
main = do
Expand Down

0 comments on commit bba0b3a

Please sign in to comment.