Skip to content

Commit

Permalink
Bringing Test Suite sqlImport in line with IDE. (#1840)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Montmorency authored Oct 10, 2023
1 parent 776b19b commit e87fdfa
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions IHP/Test/Database.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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 }

Expand All @@ -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

0 comments on commit e87fdfa

Please sign in to comment.