我正在尝试开始使用postgresql-simple-migration
库,但是在编译列出的示例程序时遇到了问题。
示例程序(如下所示)是postgresql-simple-migration
hackage page上列出的“初始化数据库”示例。
main :: IO ()
main = do
let url = "host=$host dbname=$db user=$user password=$pw"
con <- connectPostgreSQL (BS8.pack url)
withTransaction con $ runMigration $
MigrationContext MigrationInitialization True con
似乎我需要做的就是编译导入正确的模块。导入包含这些术语的postgresql-simple-migration
模块以及Data.ByteString.Char8
使pack
具有预期类型的模块,将提供以下内容。
import Database.PostgreSQL.Simple.Migration
import Database.PostgreSQL.Simple
import Data.ByteString.Char8 as BS8
main :: IO ()
main = do
let url = "host=$host dbname=$db user=$user password=$pw"
con <- connectPostgreSQL (BS8.pack url)
withTransaction con $ runMigration $
MigrationContext MigrationInitialization True con
但是,尝试编译会导致类型错误。
$ stack build
Building all executables for `db' once. After a successful build of all of them, only specified executables will be rebuilt.
db-0.1.0.0: build (lib + exe)
Preprocessing library db-0.1.0.0...
Preprocessing executable 'db-exe' for db-0.1.0.0...
[1 of 1] Compiling Main ( app/Main.hs, .stack-work/dist/x86_64-linux-nix/Cabal-1.24.2.0/build/db-exe/db-exe-tmp/Main.o )
/home/matthew/backup/organizeactions/taskmasterplatform/db/app/Main.hs:12:5: error:
• Couldn't match type ‘MigrationResult String’ with ‘()’
Expected type: IO ()
Actual type: IO (MigrationResult String)
• In a stmt of a 'do' block:
withTransaction con
$ runMigration $ MigrationContext MigrationInitialization True con
In the expression:
do { let url = "host=$host dbname=$db user=$user password=$pw";
con <- connectPostgreSQL (pack url);
withTransaction con
$ runMigration
$ MigrationContext MigrationInitialization True con }
In an equation for ‘main’:
main
= do { let url = ...;
con <- connectPostgreSQL (pack url);
withTransaction con
$ runMigration
$ MigrationContext MigrationInitialization True con }
-- While building custom Setup.hs for package db-0.1.0.0 using:
/home/matthew/.stack/setup-exe-cache/x86_64-linux-nix/Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.2 --builddir=.stack-work/dist/x86_64-linux-nix/Cabal-1.24.2.0 build lib:db exe:db-exe --ghc-options " -ddump-hi -ddump-to-file"
Process exited with code: ExitFailure 1
runMigration
(Database.PostgreSQL.Simple.Migration
)的类型为
`runMigration :: MigrationContext -> IO (MigrationResult String)`
且(MigrationResult String)
与()
的类型签名中标记的main
不符。
为什么示例不编译?