Skip to content

Commit

Permalink
extracted withoutQueryLogging function
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Dec 29, 2024
1 parent c48c8ae commit 806e81e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
18 changes: 17 additions & 1 deletion IHP/ModelSupport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1144,4 +1144,20 @@ copyRecord existingRecord =
in
existingRecord
|> set #id def
|> set #meta meta
|> set #meta meta

-- | Runs sql queries without logging them
--
-- Example:
--
-- > users <- withoutQueryLogging (sqlQuery "SELECT * FROM users" ())
--
withoutQueryLogging :: (?modelContext :: ModelContext) => ((?modelContext :: ModelContext) => result) -> result
withoutQueryLogging callback =
let
modelContext = ?modelContext
nullLogger = modelContext.logger { write = \_ -> pure ()}
in
let ?modelContext = modelContext { logger = nullLogger }
in
callback
24 changes: 11 additions & 13 deletions IHP/SchemaMigration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,17 @@ runMigration migration@Migration { revision, migrationFile } = do
createSchemaMigrationsTable :: (?modelContext :: ModelContext) => IO ()
createSchemaMigrationsTable = do
-- Hide this query from the log
let modelContext = ?modelContext
let ?modelContext = modelContext { logger = (modelContext.logger) { write = \_ -> pure ()} }

-- We don't use CREATE TABLE IF NOT EXISTS as adds a "NOTICE: relation schema_migrations already exists, skipping"
-- This sometimes confuses users as they don't know if the this is an error or not (it's not)
-- https://github.com/digitallyinduced/ihp/issues/818
maybeTableName :: Maybe Text <- sqlQueryScalar "SELECT (to_regclass('schema_migrations')) :: text" ()
let schemaMigrationTableExists = isJust maybeTableName

unless schemaMigrationTableExists do
let ddl = "CREATE TABLE IF NOT EXISTS schema_migrations (revision BIGINT NOT NULL UNIQUE)"
_ <- sqlExec ddl ()
pure ()
withoutQueryLogging do
-- We don't use CREATE TABLE IF NOT EXISTS as adds a "NOTICE: relation schema_migrations already exists, skipping"
-- This sometimes confuses users as they don't know if the this is an error or not (it's not)
-- https://github.com/digitallyinduced/ihp/issues/818
maybeTableName :: Maybe Text <- sqlQueryScalar "SELECT (to_regclass('schema_migrations')) :: text" ()
let schemaMigrationTableExists = isJust maybeTableName

unless schemaMigrationTableExists do
let ddl = "CREATE TABLE IF NOT EXISTS schema_migrations (revision BIGINT NOT NULL UNIQUE)"
_ <- sqlExec ddl ()
pure ()

-- | Returns all migrations that haven't been executed yet. The result is sorted so that the oldest revision is first.
findOpenMigrations :: (?modelContext :: ModelContext) => Int -> IO [Migration]
Expand Down

0 comments on commit 806e81e

Please sign in to comment.