sandwich-contexts-0.3.0.1: Contexts for the Sandwich test library
Safe HaskellSafe-Inferred
LanguageHaskell2010

Test.Sandwich.Contexts.PostgreSQL

Description

This module provides tools for introducing PostgreSQL databases, either via a container (Docker or Podman) or via a raw process (typically obtaining the binary from Nix).

The container method is traditional, but the raw method can be nice because it tends to leave less junk on the system such as container images, networks, and volumes.

A note about raw processes and random TCP ports: starting a Postgres process on a randomly chosen port is tricky, because Postgres currently lacks a setting for choosing its own port and reporting it back to us. So, the only way to start it on a random TCP port is to first manually find a free port on the system and then start Postgres with it. Since this procedure is inherently racy, it can cause failures if your tests are starting lots of Postgres instances (or other network-using processes) in parallel. This module takes a different approach: it starts the Postgres instance on a Unix socket, which can never fail. You can connect to it via the Unix socket directly if you like. If you use the TCP-based methods like introducePostgresViaNix, they will open a TCP socket inside the test process and then run a proxy to forward packets to the Postgres server's Unix socket.

Synopsis

Raw PostgreSQL via Nix (TCP socket)

introducePostgresViaNix Source #

Arguments

:: (HasBaseContext context, HasNixContext context, MonadUnliftIO m, MonadMask m) 
=> PostgresNixOptions

Options

-> SpecFree (LabelValue "postgres" PostgresContext :> context) m () 
-> SpecFree context m () 

Introduce a PostgreSQL instance, using a suitable package from Nix.

withPostgresViaNix Source #

Arguments

:: (HasBaseContextMonad context m, HasNixContext context, MonadUnliftIO m, MonadMask m, MonadFail m, MonadLogger m) 
=> PostgresNixOptions

Options

-> (PostgresContext -> m a) 
-> m a 

Bracket-style variant of introducePostgresViaNix.

Raw PostgreSQL via Nix (Unix socket)

introducePostgresUnixSocketViaNix Source #

Arguments

:: (HasBaseContext context, HasNixContext context, MonadUnliftIO m, MonadMask m) 
=> PostgresNixOptions

Options

-> SpecFree (LabelValue "postgres" PostgresContext :> context) m () 
-> SpecFree context m () 

Same as introducePostgresViaNix, but the postgresAddress of the PostgresContext will be a Unix socket.

withPostgresUnixSocketViaNix Source #

Arguments

:: (HasBaseContextMonad context m, HasNixContext context, MonadUnliftIO m, MonadFail m, MonadMask m, MonadLogger m) 
=> PostgresNixOptions

Options

-> (FilePath -> m a) 
-> m a 

Bracket-style variant of introducePostgresUnixSocketViaNix.

Containerized PostgreSQL

introducePostgresViaContainer Source #

Arguments

:: (HasBaseContext context, MonadUnliftIO m, MonadMask m) 
=> PostgresContainerOptions

Options

-> SpecFree (LabelValue "postgres" PostgresContext :> context) m () 
-> SpecFree context m () 

Introduce a PostgresSQL instance via a container (either Docker or Podman).

Types

data PostgresNixOptions Source #

Constructors

PostgresNixOptions 

Fields

  • postgresNixPostgres :: Text

    Postgres version to use within the Nixpkgs snapshot of your NixContext. Defaults to "postgresql", but you can pick specific versions like postgresql_15. See <nixpkgs>/top-level/all-packages.nix for the available versions in your snapshot.

  • postgresNixUsername :: Text

    Postgres username. Default to postgres.

  • postgresNixPassword :: Text

    Postgres password. Default to postgres.

  • postgresNixDatabase :: Text

    Postgres default database. The postgres database is always created, but you can create an additional one here. Defaults to test.

data PostgresContext Source #

Constructors

PostgresContext 

Fields

Instances

Instances details
Show PostgresContext Source # 
Instance details

Defined in Test.Sandwich.Contexts.PostgreSQL

Re-exports