Skip to content

Commit ae6caef

Browse files
committed
a pause point
1 parent 82daf36 commit ae6caef

File tree

5 files changed

+69
-10
lines changed

5 files changed

+69
-10
lines changed

src/Database/Postgres/Temp/Internal.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ defaultConfig :: Config
8484
defaultConfig = mempty
8585
{ configPlan = mempty
8686
{ partialPlanLogger = pure mempty
87-
, partialPlanConfig = Mappend defaultPostgresConfig
88-
, partialPlanInitDb = Mappend $ pure mempty
87+
, partialPlanConfig = Mappend defaultPostgresConfig
88+
, partialPlanCreateDb = Replace mempty
89+
, partialPlanInitDb = Mappend $ pure mempty
8990
{ partialProcessConfigCmdLine = Mappend $ mempty
9091
{ partialCommandLineArgsKeyBased = Map.singleton "--no-sync" Nothing
9192
}

src/Database/Postgres/Temp/Internal/Core.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ data StartError
4040
| CreateDbFailed ExitCode
4141
-- ^ @createdb@ failed. This can be from invalid configuration or
4242
-- the database might already exist.
43-
| CompletePlanFailed [String]
43+
| CompletePlanFailed String [String]
4444
-- ^ The 'Database.Postgres.Temp.Partial.PartialPlan' was missing info and a complete 'Plan' could
4545
-- not be created.
4646
deriving (Show, Eq, Ord, Typeable)

src/Database/Postgres/Temp/Internal/Partial.hs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ data PartialCommandLineArgs = PartialCommandLineArgs
8484
deriving stock (Generic, Show, Eq)
8585
deriving Monoid via GenericMonoid PartialCommandLineArgs
8686

87+
instance (Show a, Show b) => Pretty (Map a b)
88+
instance Pretty PartialCommandLineArgs
89+
8790
instance Semigroup PartialCommandLineArgs where
8891
x <> y = PartialCommandLineArgs
8992
{ partialCommandLineArgsKeyBased =
@@ -313,6 +316,16 @@ data PartialPostgresPlan = PartialPostgresPlan
313316
deriving Semigroup via GenericSemigroup PartialPostgresPlan
314317
deriving Monoid via GenericMonoid PartialPostgresPlan
315318

319+
instance Pretty Client.Options
320+
instance Pretty Char
321+
instance Show a => Pretty (Last a)
322+
323+
instance Pretty PartialPostgresPlan where
324+
pretty PartialPostgresPlan {..} = unlines
325+
[ "partialPostgresPlanProcessConfig: " <> pretty partialPostgresPlanProcessConfig
326+
, "partialPostgresPlanClientConfig: " <> pretty partialPostgresPlanClientConfig
327+
]
328+
316329
-- | Turn a 'PartialPostgresPlan' into a 'PostgresPlan'. Fails if any
317330
-- values are missing.
318331
completePostgresPlan :: PartialPostgresPlan -> Either [String] PostgresPlan
@@ -340,6 +353,40 @@ data PartialPlan = PartialPlan
340353
deriving Semigroup via GenericSemigroup PartialPlan
341354
deriving Monoid via GenericMonoid PartialPlan
342355

356+
class Pretty a where
357+
pretty :: a -> String
358+
359+
default pretty :: Show a => a -> String
360+
pretty = show
361+
362+
instance Pretty a => Pretty (Maybe a) where
363+
pretty = maybe "" pretty
364+
365+
instance Pretty a => Pretty [a] where
366+
pretty = unlines . map pretty
367+
368+
instance Pretty a => Pretty (Lastoid a) where
369+
pretty = \case
370+
Replace x -> "Replace: " <> pretty x
371+
Mappend x -> "Mappend: " <> pretty x
372+
373+
instance Pretty PartialProcessConfig where
374+
pretty PartialProcessConfig {..} = unlines
375+
[ "partialProcessConfigEnvVars: " <> pretty partialProcessConfigEnvVars
376+
, "partialProcessConfigCmdLine: " <> pretty partialProcessConfigCmdLine
377+
]
378+
379+
instance Pretty PartialPlan where
380+
pretty PartialPlan {..} = unlines
381+
[ "partialPlanInitDb: " <> pretty partialPlanInitDb
382+
, "partialPlanCreateDb: " <> pretty partialPlanCreateDb
383+
, "partialPlanPostgres: " <> pretty partialPlanPostgres
384+
, "partialPlanConfig: " <> pretty partialPlanConfig
385+
, "partialPlanDataDirectory: " <> pretty partialPlanDataDirectory
386+
]
387+
388+
389+
-- TODO treat Mappend in the initdb and createdb as a Nothing
343390
-- | Turn a 'PartialPlan' into a 'Plan'. Fails if any values are missing.
344391
completePlan :: PartialPlan -> Either [String] Plan
345392
completePlan PartialPlan {..} = validationToEither $ do
@@ -387,6 +434,10 @@ data Config = Config
387434
deriving Semigroup via GenericSemigroup Config
388435
deriving Monoid via GenericMonoid Config
389436

437+
-- Instead of interpreting the Replace and Mappend different
438+
-- I can use a different Monoid for Maybe that annilates with
439+
-- Nothing. The I can control create db runs or nothing by
440+
-- combine until it is disabled.
390441
-- | Create a 'PartialPlan' that sets the command line options of all processes
391442
-- (@initdb@, @postgres@ and @createdb@) using a
392443
toPlan
@@ -446,8 +497,9 @@ initConfig Config {..} = evalContT $ do
446497
(initDirectoryType "tmp-postgres-data" configDataDir) shutdownDirectoryType
447498
let hostAndDirPartial = toPlan port resourcesSocket $
448499
toFilePath resourcesDataDir
449-
resourcesPlan <- lift $ either (throwIO . CompletePlanFailed) pure $
450-
completePlan $ hostAndDirPartial <> configPlan
500+
combinedPlan = hostAndDirPartial <> configPlan
501+
resourcesPlan <- lift $ either (throwIO . CompletePlanFailed (pretty combinedPlan)) pure $
502+
completePlan combinedPlan
451503
pure Resources {..}
452504

453505
-- | Free the temporary resources created by 'initConfig'

test/Spec.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,27 +211,30 @@ spec = do
211211
{ configSocket = PIpSocket $ pure "localhost"
212212
}
213213

214+
specificDbPlan = defaultConfig <> mempty
215+
{ configPlan = mempty
216+
{ partialPlanCreateDb = Replace $ Just mempty
217+
}
218+
}
219+
214220
describe "start" $ do
215221
let startAction = bracket (either throwIO pure =<< start) stop (const $ pure ())
216222
throwsIfInitDbIsNotOnThePath startAction
217-
throwsIfCreateDbIsNotOnThePath startAction
218223
describe "startWith" $ do
219224
let startAction plan = bracket (either throwIO pure =<< startWith plan) stop pure
220225
throwsIfInitDbIsNotOnThePath $ startAction defaultConfig
221-
throwsIfCreateDbIsNotOnThePath $ startAction defaultConfig
222226
invalidConfigFailsQuickly $ void . startAction
223227
customConfigWork $ \plan f ->
224228
bracket (either throwIO pure =<< startWith plan) stop f
225229
describe "with" $ do
226230
let startAction = either throwIO pure =<< with (const $ pure ())
227231
throwsIfInitDbIsNotOnThePath startAction
228-
throwsIfCreateDbIsNotOnThePath startAction
229232
describe "withPlan" $ do
230233
let startAction plan = either throwIO pure =<<
231234
withPlan plan pure
232235

233236
throwsIfInitDbIsNotOnThePath $ startAction defaultConfig
234-
throwsIfCreateDbIsNotOnThePath $ startAction defaultConfig
237+
throwsIfCreateDbIsNotOnThePath $ startAction specificDbPlan
235238
invalidConfigFailsQuickly $ void . startAction
236239
customConfigWork $ \plan f -> either throwIO pure =<<
237240
withPlan plan f
@@ -316,7 +319,7 @@ spec = do
316319
, Client.port = pure thePort
317320
}
318321

319-
before (pure $ Runner $ \f -> bracket (either throwIO pure =<< startWith planFromCustomUserDbConnection) stop f) $
322+
before (pure $ Runner $ \f -> bracket (either throwIO pure =<< startWith planFromCustomUserDbConnection) stop f) $ do
320323
someStandardTests "fancy"
321324

322325
before (createTempDirectory "/tmp" "tmp-postgres-test") $ after rmDirIgnoreErrors $ do

tmp-postgres.cabal

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ library
5151
, ApplicativeDo
5252
, DeriveFunctor
5353
, ViewPatterns
54+
, DefaultSignatures
55+
, TypeSynonymInstances
56+
, FlexibleInstances
5457
build-depends: base >= 4.6 && < 5
5558
, temporary
5659
, process >= 1.2.0.0

0 commit comments

Comments
 (0)