Skip to content

Releases

2.0.0

Choose a tag to compare

@github-actions github-actions released this 25 Nov 00:25
998fdc5

dream_postgres 2.0.0 Release Notes

Release Date: 2026-11-24

This release makes postgres.from_url() read a connection URL correctly and tell you when it cannot. Before, it read the user and the password wrongly, kept the query string in the database name, and stopped the calling process when the pool did not start.

It is a major version because from_url() now returns a Result, the package now uses dream_postgres_client in place of pog, and sslmode=verify-ca now has the meaning of libpq. Read the Migration Guide before you upgrade. The release also adds query recording, and the package moved to its own repository.

Key Highlights

  • from_url() returns a Result (breaking): a pool that does not start, or a URL that cannot be read, is an error that your code can handle
  • The user and the password come from the URL: postgresql://user:pass@localhost:5432/myapp now connects as user with the password pass
  • sslmode, application_name and server parameters work: the query string no longer becomes a part of the database name, and a parameter such as search_path goes to the server
  • New parse_config: read a URL into a Config and do not connect
  • New check: stop at startup when the database does not answer
  • Query recording (new): record the queries of a test once against PostgreSQL, then play them back with no database
  • dream_postgres_client (breaking): the package uses dream_postgres_client, a fork of Pog. Import it as import dream_postgres_client, and call dream_postgres_client.query where you called pog.query
  • sslmode as in libpq (breaking for verify-ca): verify-ca checks the certificate chain but not the host name, and sslrootcert trusts the CA file of one pool
  • Its own repository: dream_postgres now lives at TrustBound/dream_postgres, with its own version and its own releases

What's New

Query recording

The new module dream_postgres/recorder records each query of a test, with its parameters and its result, to one JSON file, and plays the file back with no database. It has the three modes of the HTTP recorder of dream_http_client, the constructors of the type recorder.Mode:

  • Record: each query runs on the database. The recorder writes the query and its result (the count and the rows, or the error) to the file before the query returns. A query in dream_postgres_client.transaction is recorded too.
  • Playback: no pool starts and nothing connects. Each query gets the result of its recording. A query with no recording fails with dream_postgres_client.InterceptorFailed, and never reaches a database. recorder.error reads the recorder.NoRecording(sql:, parameters:, path:, nearest:) error back from it, and recorder.describe_error shows the nearest recordings in the file and what differs (see below).
  • Passthrough: each query runs on the database, and nothing is recorded.

A query with no recording Tested snippet

No recording matches SELECT id, name, email FROM users WHERE id = $1 with [2]
Nearest recording in test/fixtures/recordings/create_and_find.json:
  parameters:
  - [1]
  + [2]

The nearest recordings are those that the key would match with their parameters, fewest differing parameters first, at most 3. The recorder compares after the query transformer, so a hidden value never shows. When no recording has the SQL, the text names the file that the recorder searched.

Set the mode with recorder.mode(recorder.Playback). A wrong mode name is a compile error. For a mode in a text, for example from an environment variable, recorder.parse_mode(text) accepts "record", "playback" and "passthrough", and gives an Error that names any other text.

Record a test once, then play it back Tested snippet

pub fn run_recorded(
  mode: recorder.Mode,
  database_url: String,
  directory: String,
) -> Result(User, String) {
  use recording <- result.try(start_recorder(mode, directory, "create_and_find"))
  use db <- result.try(connect(recording, database_url))
  let user = create_and_find(db) |> result.map_error(string.inspect)
  // In record mode, each query was on disk when it returned
  use Nil <- result.try(recorder.stop(recording))
  user
}

A query gets the first recording with its key that it was not given yet. So the same query run two times gets its two results in recorded order, and queries from two processes get their own recordings in any order. The keys are sql_and_parameters (the default), normalized_sql_and_parameters, sql_only, or a function of your own. A query transformer and a result transformer hide secrets before the file is written. Every kind of value that the driver returns comes back the same in playback, and a test against PostgreSQL 16 proves each one. The module dream_postgres/recording documents the file format (version 1, one key order, one line for each row) and reads and writes it.

The new guide Test database code with recorded queries walks through it.

In record mode, a query that the recorder cannot record fails: the file cannot be written (WriteFailed), a value cannot be recorded (NotRecordable), or the recorder stopped (RecorderStopped). recorder.error reads the recorder.RecorderError back from the query error, and gives Error(Nil) for an error of the database or of another interceptor. recorder.stop gives an Error only when the recorder had stopped already.

dream_postgres_client in Place of pog

The package uses dream_postgres_client, a fork of Pog 4.1.0, on the driver dream_postgres_driver, a fork of pgo 0.20.0. The API is the same, but the module is dream_postgres_client, so your code imports it as import dream_postgres_client and calls dream_postgres_client.query in place of pog.query. The fork adds the query interceptor that the recorder uses, the chain-only and CA file SSL modes, and a fix for application_name.

from_url() Returns a Result

  • Before: pub fn from_url(url: String) -> Connection
  • After: pub fn from_url(url: String) -> Result(Connection, FromUrlError)

Before, from_url() stopped the calling process with a let assert when the pool did not start, so a caller could not handle the failure.

FromUrlError holds a UrlError or the reason the pool did not start: InvalidUrl and PoolStartExited. No error value holds the password.

Connect from a URL and handle each result Tested snippet

import dream_postgres/client as postgres
import gleam/string

pub fn describe_connection(url: String) -> String {
  case postgres.from_url(url) {
    Ok(_connection) -> "The pool started"
    Error(postgres.InvalidUrl(error)) ->
      "Invalid URL: " <> string.inspect(error)
    Error(error) -> "The pool did not start: " <> string.inspect(error)
  }
}

A URL That Cannot Be Read Is an Error

from_url() returns Error(InvalidUrl(UrlError)) for a URL it cannot read. Before, it used default values for the parts it could not find.

UrlError says what is wrong: MalformedUrl, MissingScheme, UnsupportedScheme, MissingHost, InvalidPort, MissingDatabase, InvalidDatabase, MissingUser, InvalidUserEncoding, InvalidPasswordEncoding, InvalidDatabaseEncoding, UnsupportedParameter, UnknownParameterLooksLike, UnsupportedSslMode and UnsupportedFragment.

parse_config

postgres.parse_config(pool_name, url) -> Result(Config, UrlError) reads a connection URL into a Config and does not connect. Use it to check a URL at startup, or to change a setting with the builder functions before you connect.

check

postgres.check(connection) -> Result(Nil, QueryError) runs SELECT 1 on the connection. It gives Ok(Nil) when the database answers, or the error of the query when it does not. connect and from_url return Ok before the pool connects, so an app calls check after them to stop at startup, not at the first request. When no database listens, the password is wrong, the database does not exist or the pool size is 0, the error is dream_postgres_client.ConnectionUnavailable, after 2 to 3 seconds. The getting started guide shows how to combine the errors of from_url and check in one startup error type.

sslmode, sslrootcert, application_name and IPv6

The sslmode query parameter sets the SSL mode, with the meaning of libpq. The values are disable, require, verify-ca and verify-full. verify-ca gives dream_postgres_client.SslVerifiedChainOnly: it checks that a trusted CA signed the certificate, but not the host name. verify-full gives dream_postgres_client.SslVerified, which also checks the host name. Without sslrootcert, the client trusts the CA certificates of the operating system. sslrootcert=<path> makes the pool trust the CA certificates of that PEM file only: it gives dream_postgres_client.SslVerifiedCaFile(path) with verify-full, and dream_postgres_client.SslVerifiedChainOnlyCaFile(path) with verify-ca or require. sslrootcert=system needs verify-full. These are the rules of `dream_postgres_client.url_conf...

Read more