Skip to content

Commit

Permalink
Upgrade to hasql 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
sumo committed Jun 2, 2019
1 parent 7b3508e commit dcc560b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
7 changes: 3 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.2.1
version: 0.3.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
tested-with: GHC==8.0.1, GHC==8.2.2, GHC==8.4.3, GHC==8.6.5
extra-source-files: License
Readme.markdown

Expand All @@ -36,9 +36,8 @@ library
bytestring >= 0.10,
contravariant >= 1.3,
cryptonite >= 0.11,
data-default-class >= 0.0.1,
directory >= 1.2,
hasql > 1.3 && < 1.4,
hasql >= 1.4,
hasql-transaction >= 0.7,
memory,
text >= 1.2,
Expand Down
16 changes: 8 additions & 8 deletions src/Hasql/Migration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ module Hasql.Migration
, SchemaMigration(..)
) where

import Control.Arrow
import Crypto.Hash (hashWith, MD5(..))
import Data.ByteArray.Encoding
import Data.Default.Class
import Data.Functor.Contravariant
import Data.List (isPrefixOf, sort)
import Data.Time (LocalTime)
Expand Down Expand Up @@ -95,12 +93,13 @@ executeMigration name contents = do
return Nothing
ScriptNotExecuted -> do
sql contents
statement (name, checksum) (Statement q (contramap (first T.pack) def) Decoders.unit False)
statement (name, checksum) (Statement q enc Decoders.noResult False)
return Nothing
ScriptModified _ -> do
return (Just $ ScriptChanged name)
where
q = "insert into schema_migrations(filename, checksum) values($1, $2)"
enc = ((T.pack . fst) >$< Encoders.param (Encoders.nonNullable Encoders.text)) <> (snd >$< Encoders.param (Encoders.nonNullable Encoders.text))

-- | Initializes the database schema with a helper table containing
-- meta-information about executed migrations.
Expand Down Expand Up @@ -147,7 +146,8 @@ executeValidation cmd = case cmd of
-- will be executed and its meta-information will be recorded.
checkScript :: ScriptName -> Checksum -> Transaction CheckScriptResult
checkScript name checksum =
statement name (Statement q (contramap T.pack (Encoders.param def)) (Decoders.rowMaybe (Decoders.column def)) False) >>= \case
statement name (Statement q (contramap T.pack (Encoders.param (Encoders.nonNullable Encoders.text)))
(Decoders.rowMaybe (Decoders.column (Decoders.nonNullable Decoders.text))) 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 =
statement () $ Statement q def (Decoders.rowList decodeSchemaMigration) False
statement () $ Statement q Encoders.noParams (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.column def
<*> Decoders.column def
<*> Decoders.column def
<$> Decoders.column (Decoders.nonNullable Decoders.bytea)
<*> Decoders.column (Decoders.nonNullable Decoders.text)
<*> Decoders.column (Decoders.nonNullable Decoders.timestamp)
3 changes: 1 addition & 2 deletions src/Hasql/Migration/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ import qualified Hasql.Encoders as Encoders
import qualified Hasql.Decoders as Decoders
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) $ statement table q
where
q = Statement sql (Encoders.param def) (Decoders.rowList (Decoders.column Decoders.int8)) False
q = Statement sql (Encoders.param (Encoders.nonNullable Encoders.text)) (Decoders.rowList (Decoders.column (Decoders.nullable Decoders.int8))) False
sql = "select relname from pg_class where relname = $1"
3 changes: 2 additions & 1 deletion test/Hasql/MigrationTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ migrationSpec :: Connection -> Spec
migrationSpec con = describe "Migrations" $ do
let migrationScript = MigrationScript "test.sql" q
let migrationScriptAltered = MigrationScript "test.sql" ""
[migrationDir] <- runIO $ loadMigrationsFromDirectory "share/test/scripts"
mds <- runIO $ loadMigrationsFromDirectory "share/test/scripts"
let migrationDir = head mds
migrationFile <- runIO $ loadMigrationFromFile "s.sql" "share/test/script.sql"

it "initializes a database" $ do
Expand Down

0 comments on commit dcc560b

Please sign in to comment.