From e87fdfa4d0209d59446cb68074e834727cfc17dd Mon Sep 17 00:00:00 2001 From: Montmorency Date: Tue, 10 Oct 2023 14:28:29 +0100 Subject: [PATCH] Bringing Test Suite sqlImport in line with IDE. (#1840) * Bringing test sqlImport in line with IDE. Solves import issue for complex schema. * Database url not relative to current dir. * Adding comment to importSql explaining reason for not using raw bytestring with PG.Query. --- IHP/Test/Database.hs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/IHP/Test/Database.hs b/IHP/Test/Database.hs index 1ed15ffa4..607441481 100644 --- a/IHP/Test/Database.hs +++ b/IHP/Test/Database.hs @@ -10,6 +10,9 @@ import qualified Data.ByteString as ByteString import qualified IHP.LibDir as LibDir import qualified Control.Exception as Exception +import qualified System.Process as Process +import qualified System.Directory as Directory + data TestDatabase = TestDatabase { name :: Text , url :: ByteString @@ -22,6 +25,7 @@ data TestDatabase = TestDatabase -- createTestDatabase :: ByteString -> IO TestDatabase createTestDatabase databaseUrl = do +-- currentDir <- Directory.getCurrentDirectory databaseId <- UUID.nextRandom let databaseName = "test-" <> UUID.toText databaseId @@ -34,8 +38,13 @@ createTestDatabase databaseUrl = do |> cs libDir <- LibDir.findLibDirectory - importSql newUrl (cs libDir <> "/IHPSchema.sql") - importSql newUrl "Application/Schema.sql" + + -- We use the system psql to handle the initial Schema Import as it can handle + -- complex Schema including variations in formatting, custom types, functions, and table definitions. + let importSql file = Process.callCommand ("psql " <> (cs newUrl) <> " < " <> file) + + importSql (cs libDir <> "/IHPSchema.sql") + importSql "Application/Schema.sql" pure TestDatabase { name = databaseName, url = newUrl } @@ -54,10 +63,5 @@ deleteDatabase masterDatabaseUrl testDatabase = do PG.execute connection "DROP DATABASE ? WITH (FORCE)" [PG.Identifier (testDatabase.name)] pure () -importSql url file = do - schemaSql <- ByteString.readFile file - - withConnection url \connection -> do - PG.execute connection (PG.Query schemaSql) () withConnection databaseUrl = Exception.bracket (PG.connectPostgreSQL databaseUrl) PG.close