| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Database.Postgres.Temp
Contents
Description
This module provides functions for creating a temporary postgres instance.
By default it will create a temporary data directory and
a temporary directory for a UNIX domain socket for postgres to listen on.
Here is an example using the expection safe with function:
with$ \db ->bracket(connectPostgreSQL(toConnectionStringdb))close$ \conn ->execute_conn "CREATE TABLE foo (id int)"
To extend or override the defaults use withConfig (or startConfig).
tmp-postgres ultimately calls (optionally) initdb, postgres and
(optionally) createdb.
All of the command line, environment variables and configuration files that are generated by default for the respective executables can be extended.
In general tmp-postgres is useful if you want a clean temporary
postgres and do not want to worry about clashing with an existing
postgres instance (or needing to ensure postgres is already running).
Here are some different use cases for tmp-postgres and their respective
configurations:
- The default
withandstartfunctions can be used to make a sandboxed temporary database for testing. - By disabling
initdbone could run a temporary isolated postgres on a base backup to test a migration. - By using the
stopPostgresandwithRestartfunctions one can test backup strategies.
WARNING!!
Ubuntu's PostgreSQL installation does not put initdb on the PATH. We need to add it manually.
The necessary binaries are in the /usr/lib/postgresql/VERSION/bin/ directory, and should be added to the PATH
echo "export PATH=$PATH:/usr/lib/postgresql/VERSION/bin/" >> /home/ubuntu/.bashrc
Synopsis
- with :: (DB -> IO a) -> IO (Either StartError a)
- withConfig :: Config -> (DB -> IO a) -> IO (Either StartError a)
- start :: IO (Either StartError DB)
- startConfig :: Config -> IO (Either StartError DB)
- stop :: DB -> IO ()
- defaultConfig :: Config
- defaultPostgresConf :: [String] -> Config
- standardProcessConfig :: PartialProcessConfig
- optionsToDefaultConfig :: Options -> Config
- restart :: DB -> IO (Either StartError DB)
- stopPostgres :: DB -> IO ExitCode
- withRestart :: DB -> (DB -> IO a) -> IO (Either StartError a)
- reloadConfig :: DB -> IO ()
- data DB
- prettyPrintDB :: DB -> String
- toConnectionString :: DB -> ByteString
- toConnectionOptions :: DB -> Options
- toDataDirectory :: DB -> FilePath
- makeDataDirPermanent :: DB -> DB
- data Config = Config {}
- prettyPrintConfig :: Config -> String
- configPlanL :: Lens' Config PartialPlan
- configSocketL :: Lens' Config PartialSocketClass
- configDataDirL :: Lens' Config PartialDirectoryType
- configPortL :: Lens' Config (Last (Maybe Int))
- data PartialPlan = PartialPlan {}
- partialPlanConfigL :: Lens' PartialPlan [String]
- partialPlanCreateDbL :: Lens' PartialPlan (Maybe PartialProcessConfig)
- partialPlanDataDirectoryL :: Lens' PartialPlan (Last String)
- partialPlanInitDbL :: Lens' PartialPlan (Maybe PartialProcessConfig)
- partialPlanLoggerL :: Lens' PartialPlan (Last Logger)
- partialPlanPostgresL :: Lens' PartialPlan PartialPostgresPlan
- data PartialPostgresPlan = PartialPostgresPlan {}
- partialPostgresPlanClientConfigL :: Lens' PartialPostgresPlan Options
- partialPostgresPlanProcessConfigL :: Lens' PartialPostgresPlan PartialProcessConfig
- data PartialProcessConfig = PartialProcessConfig {}
- partialProcessConfigCmdLineL :: Lens' PartialProcessConfig PartialCommandLineArgs
- partialProcessConfigEnvVarsL :: Lens' PartialProcessConfig PartialEnvVars
- partialProcessConfigStdErrL :: Lens' PartialProcessConfig (Last Handle)
- partialProcessConfigStdInL :: Lens' PartialProcessConfig (Last Handle)
- partialProcessConfigStdOutL :: Lens' PartialProcessConfig (Last Handle)
- data PartialEnvVars = PartialEnvVars {}
- partialEnvVarsInheritL :: Lens' PartialEnvVars (Last Bool)
- partialEnvVarsSpecificL :: Lens' PartialEnvVars (Map String String)
- data PartialCommandLineArgs = PartialCommandLineArgs {}
- partialCommandLineArgsIndexBasedL :: Lens' PartialCommandLineArgs (Map Int String)
- partialCommandLineArgsKeyBasedL :: Lens' PartialCommandLineArgs (Map String (Maybe String))
- data PartialDirectoryType
- data PartialSocketClass
- type Logger = Event -> IO ()
- data Event
- data StartError
Exception safe interface
Arguments
| :: (DB -> IO a) |
|
| -> IO (Either StartError a) |
Default expectation safe interface. Equivalent to withConfig the
defaultConfig
Arguments
| :: Config |
|
| -> (DB -> IO a) |
|
| -> IO (Either StartError a) |
Exception safe default database create. Takes an action continuation
which is given a DB it can use to connect
to (see toConnectionString or postgresProcessClientOptions).
All of the database resources are automatically cleaned up on
completion even in the face of exceptions.
Based on the value of configSocket a "postgresql.conf" is created with
listen_addresses = 'IP_ADDRESS'
if it is IpSocket. If is UnixSocket then the lines
listen_addresses = '' unix_socket_directories = SOCKET_DIRECTORY
are added. This occurs as a side effect of calling withConfig.
Separate start and stop interface.
start :: IO (Either StartError DB) Source #
Default start behavior. Equivalent to calling startConfig with the
defaultConfig
Arguments
| :: Config |
|
| -> IO (Either StartError DB) |
Create temporary resources and use them to make a Config.
The generated Config is combined with the passed in extraConfiguration
to create a Plan that is used to create a database.
The output DB includes references to the temporary resources for
cleanup and the final plan that was used to generate the database and
processes
Stop the postgres process and cleanup any temporary directories that
might have been created.
defaultConfig :: Config Source #
The default configuration. This will create a database called "postgres"
via initdb (it's default behavior).
It will create a temporary directory for the data and a temporary directory
for a unix socket on a random port.
Additionally it will use append the following onto the "postgresql.conf"
which is optimized for performance.
shared_buffers = 12MB fsync = off synchronous_commit = off full_page_writes = off log_min_duration_statement = 0 log_connections = on log_disconnections = on client_min_messages = ERROR
defaultConfig also passes the --no-sync flag to initdb.
If you would like to customize this behavior you can start with the
defaultConfig and overwrite fields or combine a defaultConfig with another Config
using <> (mappend).
Alternatively you can eschew defaultConfig altogether, however
your postgres might start and run faster if you use
defaultConfig.
defaultConfig also sets the partialPlanInitDb to
pure standardProcessConfig and
partialPostgresPlanProcessConfig to standardProcessConfig.
To append additional lines to "postgresql.conf" file create a
custom Config like the following.
custom = defaultConfig <> mempty
{ configPlan = mempty
{ partialPlanConfig =
[ "wal_level = replica"
, "archive_mode = on"
, "max_wal_senders = 2"
, "fsync = on"
, "synchronous_commit = on"
]
}
}
Or using the provided lenses and your favorite lens library
custom = defaultConfig &configPlanL.partialPlanConfigL<>~ [ "wal_level = replica" , "archive_mode = on" , "max_wal_senders = 2" , "fsync = on" , "synchronous_commit = on" ]
This is common enough there is defaultPostgresConf which
is a helper to do this.
As an alternative to using defaultConfig one could create a
config from connections parameters using optionsToDefaultConfig
defaultPostgresConf :: [String] -> Config Source #
mappend the defaultConfig with a Config that provides additional
"postgresql.conf" lines. Equivalent to
defaultPostgresConf extra = defaultConfig <> mempty
{ configPlan = mempty
{ partialPlanConfig = extra
}
}
standardProcessConfig :: PartialProcessConfig Source #
The standardProcessConfig sets the handles to stdin, stdout and
stderr and inherits the environment variables from the calling
process.
Custom Config builder helpers
Starting and Stopping postgres without removing the temporary directory
stopPostgres :: DB -> IO ExitCode Source #
Only stop the postgres process but leave any temporary resources.
Useful for testing backup strategies when used in conjunction with
restart or withRestart.
withRestart :: DB -> (DB -> IO a) -> IO (Either StartError a) Source #
Exception safe version of restart
Reloading the config
reloadConfig :: DB -> IO () Source #
Reload the configuration file without shutting down. Calls
pg_reload_conf().
Main resource handle
Handle for holding temporary resources, the postgres process handle
and postgres connection information. The DB also includes the
final Plan that was used to start initdb, createdb and
postgres. See toConnectionString for converting a DB to
postgresql connection string.
DB manipulation
toConnectionString :: DB -> ByteString Source #
Convert a DB to a connection string. Alternatively one can access the
Options using toConnectionOptions
toDataDirectory :: DB -> FilePath Source #
Access the data directory. This was either generated or
specified explicitly when creating the Config
makeDataDirPermanent :: DB -> DB Source #
Make the data directory permanent. Useful for debugging.
If you are using with or withConfig this function will
not modify the DB that is passed for cleanup. You will
need to setup your own bracket like
bracket (fmapmakeDataDirPermanentstart) (either memptystop)
Monoidial Configuration Types
Config
The high level options for overriding default behavior.
Constructors
| Config | |
Fields
| |
Instances
| Generic Config Source # | |
| Semigroup Config Source # | |
| Monoid Config Source # | |
| Pretty Config Source # | |
Defined in Database.Postgres.Temp.Internal.Partial | |
| type Rep Config Source # | |
Defined in Database.Postgres.Temp.Internal.Partial type Rep Config = D1 (MetaData "Config" "Database.Postgres.Temp.Internal.Partial" "tmp-postgres-1.7.1.0-Fl2f8QVtDW2J2btYcu2iIC" False) (C1 (MetaCons "Config" PrefixI True) ((S1 (MetaSel (Just "configPlan") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 PartialPlan) :*: S1 (MetaSel (Just "configSocket") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 PartialSocketClass)) :*: (S1 (MetaSel (Just "configDataDir") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 PartialDirectoryType) :*: S1 (MetaSel (Just "configPort") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (Last (Maybe Int)))))) | |
Config Lenses
configPlanL :: Lens' Config PartialPlan Source #
Lens for configPlan
configSocketL :: Lens' Config PartialSocketClass Source #
Lens for configSocket
configDataDirL :: Lens' Config PartialDirectoryType Source #
Lens for configDataDir
configPortL :: Lens' Config (Last (Maybe Int)) Source #
Lens for configPort
PartialPlan
data PartialPlan Source #
The monoidial version of Plan. Used to combine overrides with defaults
when creating a plan.
Constructors
| PartialPlan | |
Instances
PartialPlan lenses
partialPlanConfigL :: Lens' PartialPlan [String] Source #
Lens for partialPlanConfig
partialPlanCreateDbL :: Lens' PartialPlan (Maybe PartialProcessConfig) Source #
Lens for partialPlanCreateDb
partialPlanDataDirectoryL :: Lens' PartialPlan (Last String) Source #
Lens for partialPlanDataDirectory
partialPlanInitDbL :: Lens' PartialPlan (Maybe PartialProcessConfig) Source #
Lens for partialPlanInitDb
partialPlanLoggerL :: Lens' PartialPlan (Last Logger) Source #
Lens for partialPlanLogger
PartialPostgresPlan
data PartialPostgresPlan Source #
postgres process config and corresponding client connection
Options.
Constructors
| PartialPostgresPlan | |
Fields
| |
Instances
PartialPostgresPlan lenses
partialPostgresPlanClientConfigL :: Lens' PartialPostgresPlan Options Source #
Lens for partialPostgresPlanClientConfig
partialPostgresPlanProcessConfigL :: Lens' PartialPostgresPlan PartialProcessConfig Source #
Lens for partialPostgresPlanProcessConfig
PartialProcessConfig
data PartialProcessConfig Source #
The monoidial version of ProcessConfig. Used to combine overrides with
defaults when creating a ProcessConfig.
Constructors
| PartialProcessConfig | |
Fields
| |
Instances
PartialProcessConfig Lenses
partialProcessConfigCmdLineL :: Lens' PartialProcessConfig PartialCommandLineArgs Source #
Lens for partialProcessConfigCmdLine
partialProcessConfigEnvVarsL :: Lens' PartialProcessConfig PartialEnvVars Source #
Lens for partialProcessConfigEnvVars
partialProcessConfigStdErrL :: Lens' PartialProcessConfig (Last Handle) Source #
Lens for partialProcessConfigStdErr
partialProcessConfigStdInL :: Lens' PartialProcessConfig (Last Handle) Source #
Lens for partialProcessConfigStdIn
partialProcessConfigStdOutL :: Lens' PartialProcessConfig (Last Handle) Source #
Lens for partialProcessConfigStdOut
PartialEnvVars
data PartialEnvVars Source #
The environment variables can be declared to inherit from the running process or they can be specifically added.
Constructors
| PartialEnvVars | |
Fields | |
Instances
PartialEnvVars Lenses
partialEnvVarsInheritL :: Lens' PartialEnvVars (Last Bool) Source #
Lens for partialEnvVarsInherit
partialEnvVarsSpecificL :: Lens' PartialEnvVars (Map String String) Source #
Lens for partialEnvVarsSpecific
PartialCommandLineArgs
data PartialCommandLineArgs Source #
A type to help combine command line arguments.
Constructors
| PartialCommandLineArgs | |
Fields
| |
Instances
PartialCommandLineArgs Lenses
partialCommandLineArgsIndexBasedL :: Lens' PartialCommandLineArgs (Map Int String) Source #
Lens for partialCommandLineArgsIndexBased
partialCommandLineArgsKeyBasedL :: Lens' PartialCommandLineArgs (Map String (Maybe String)) Source #
Lens for partialCommandLineArgsKeyBased
PartialDirectoryType
data PartialDirectoryType Source #
The monoidial version of DirectoryType. Used to combine overrides with
defaults when creating a DirectoryType. The monoid instance treats
PTemporary as mempty and takes the last PPermanent value.
Constructors
| PPermanent FilePath | A permanent file that should not be generated. |
| PTemporary | A temporary file that needs to generated. |
Instances
PartialSocketClass
data PartialSocketClass Source #
The monoidial version of SocketClass. Used to combine overrides with
defaults when creating a SocketClass. The monoid instance treats
'PUnixSocket mempty' as mempty and combines the
Constructors
| PIpSocket (Last String) | The monoid for combining IP address configuration |
| PUnixSocket PartialDirectoryType | The monoid for combining UNIX socket configuration |
Instances
Logger
Internal events passed to the partialPlanLogger .
Internal events for debugging
Constructors
| StartPostgres | |
| WaitForDB | |
| StartPlan String | |
| TryToConnect |
Errors
data StartError Source #
A list of failures that can occur when starting. This is not and exhaustive list but covers the errors that the system catches for the user.
Constructors
| StartPostgresFailed ExitCode |
|
| InitDbFailed ExitCode |
|
| CreateDbFailed ExitCode |
|
| CompletePlanFailed String [String] | The |
Instances
| Eq StartError Source # | |
Defined in Database.Postgres.Temp.Internal.Core | |
| Ord StartError Source # | |
Defined in Database.Postgres.Temp.Internal.Core Methods compare :: StartError -> StartError -> Ordering # (<) :: StartError -> StartError -> Bool # (<=) :: StartError -> StartError -> Bool # (>) :: StartError -> StartError -> Bool # (>=) :: StartError -> StartError -> Bool # max :: StartError -> StartError -> StartError # min :: StartError -> StartError -> StartError # | |
| Show StartError Source # | |
Defined in Database.Postgres.Temp.Internal.Core Methods showsPrec :: Int -> StartError -> ShowS # show :: StartError -> String # showList :: [StartError] -> ShowS # | |
| Exception StartError Source # | |
Defined in Database.Postgres.Temp.Internal.Core Methods toException :: StartError -> SomeException # fromException :: SomeException -> Maybe StartError # displayException :: StartError -> String # | |