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

Update to LTS-13.24 #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions dbmigrations.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Library

Build-Depends:
base >= 4 && < 5,
hasql == 1.1.1,
hasql,
time >= 1.4,
random >= 1.0,
containers >= 0.2,
Expand Down Expand Up @@ -122,7 +122,7 @@ test-suite dbmigrations-tests
yaml-light >= 0.1,
bytestring >= 0.9,
MissingH,
hasql == 1.1.1,
hasql,
HUnit >= 1.2,
process >= 1.1,
configurator >= 0.2,
Expand Down Expand Up @@ -159,7 +159,7 @@ Executable moo
base >= 4 && < 5,
configurator >= 0.2,
dbmigrations,
hasql == 1.1.1,
hasql,
bytestring

if impl(ghc >= 6.12.0)
Expand Down
67 changes: 41 additions & 26 deletions src/Database/Schema/Migrations/Backend/Hasql.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@ module Database.Schema.Migrations.Backend.Hasql
)
where

import Hasql.Session
import Hasql.Query (statement)
import qualified Hasql.Encoders as H.Encode
import Hasql.Decoders
import Hasql.Connection
import Hasql.Connection
import Hasql.Decoders
import qualified Hasql.Encoders as H.Encode
import Hasql.Session
import Hasql.Statement

import Database.Schema.Migrations.Backend
( Backend(..)
, rootMigrationName
)
import Database.Schema.Migrations.Migration
( Migration(..)
, newMigration
)
import Database.Schema.Migrations.Backend (Backend (..),
rootMigrationName)
import Database.Schema.Migrations.Migration (Migration (..),
newMigration)

import Data.ByteString (ByteString)
import Data.Monoid ( (<>) )
import Data.Time.Clock (getCurrentTime)
import System.Exit (ExitCode (ExitFailure), exitWith)
import Data.ByteString (ByteString)
import Data.Monoid ((<>))
import Data.Time.Clock (getCurrentTime)
import System.Exit (ExitCode (ExitFailure),
exitWith)

migrationTableName :: ByteString
migrationTableName = "installed_migrations"
Expand All @@ -37,9 +34,18 @@ revertSql = "DROP TABLE " <> migrationTableName
hasqlBackend :: Connection -> Backend
hasqlBackend conn =
Backend { isBootstrapped = do
exists <- run (query () (statement "SELECT migration_id FROM installed_migrations WHERE FALSE" H.Encode.unit unit True)) conn
exists <- run (
statement
()
(Statement
"SELECT migration_id FROM installed_migrations WHERE FALSE"
H.Encode.unit
unit
True
)
) conn
return $ case exists of
Left _ -> False
Left _ -> False
Right _ -> True
, getBootstrapMigration = do
ts <- getCurrentTime
Expand All @@ -53,12 +59,12 @@ hasqlBackend conn =
, applyMigration = \m -> do
reportAction <- run (sql $ mApply m) conn
case reportAction of
Left e -> reportSqlError e
Left e -> reportSqlError e
Right i -> return i
register <- run (sql $ "INSERT INTO " <> migrationTableName <>
" (migration_id) VALUES ('" <> mId m <> "')") conn
case register of
Left e -> reportSqlError e
Left e -> reportSqlError e
Right i -> return i
return ()

Expand All @@ -68,27 +74,36 @@ hasqlBackend conn =
Just revQ -> do
action <- run (sql revQ) conn
case action of
Left e -> reportSqlError e
Left e -> reportSqlError e
Right i -> return i
-- Remove migration from installed_migrations in either case.
deleteAction <- run (sql $ "DELETE FROM " <> migrationTableName <>
" WHERE migration_id = '" <> mId m <> "'") conn
case deleteAction of
Left e -> reportSqlError e
Left e -> reportSqlError e
Right i -> return i
return ()

, getMigrations = do
selectNames <- run (query () (statement "SELECT migration_id FROM installed_migrations" H.Encode.unit (rowsList $ value bytea) False)) conn
selectNames <- run (
statement
()
(Statement
"SELECT migration_id FROM installed_migrations"
H.Encode.unit
(rowList $ column bytea)
False
)
) conn
results <- case selectNames of
Left e -> reportSqlError e
Left e -> reportSqlError e
Right names -> return names
return results

, disconnectBackend = release conn
}

reportSqlError :: Error -> IO a
reportSqlError :: QueryError -> IO a
reportSqlError e = do
putStrLn $ "\n" <> "A database error occurred: " <> show e
exitWith (ExitFailure 1)
3 changes: 1 addition & 2 deletions stack.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
resolver: lts-12.8
resolver: lts-13.24

packages:
- .

extra-deps:
- HsSyck-0.53
- yaml-light-0.1.4
- hasql-1.1.1