PgFlow.Upgrade.SlugAudit (PgFlow v0.7.2)

Copy Markdown View Source

Read-only audit of flow and step slugs against the pgflow 0.17.2 rules.

PgFlow.Slug is the reference implementation. The SQL here must agree with it, and it must not call pgflow.is_valid_slug/1: on core V02 that function still implements the old rule. The same SQL text feeds mix pgflow.audit_slugs, the core V03 pre-flight block, and the tests.

The audit never renames slugs. Fix each finding, then upgrade. See docs/UPGRADING_UPSTREAM_2026_10.md.

Summary

Functions

Selects kind, flow_slug, step_slug, and reason for every 0.17.2 violation.

DO block that raises check_violation listing every offender.

The statement run/2 issues before reading findings.

CASE expression that returns the PgFlow.Slug reason as text, or NULL when valid.

Boolean SQL for one slug expression, in the same order as PgFlow.Slug.validate/1.

Returns slug findings for the given repo.

Types

finding()

@type finding() :: %{
  :kind => kind(),
  :flow_slug => String.t(),
  optional(:step_slug) => String.t(),
  reason: reason()
}

kind()

@type kind() :: :flow | :step | :step_collision

reason()

@type reason() ::
  :empty
  | :too_long
  | :bad_chars
  | :reserved
  | :bad_start
  | :bad_end
  | :double_underscore
  | :queue_name_too_long
  | :case_insensitive_duplicate

Functions

findings_sql(prefix)

@spec findings_sql(String.t()) :: String.t()

Selects kind, flow_slug, step_slug, and reason for every 0.17.2 violation.

prefix must match ~r/\A[a-z_][a-z0-9_]*\z/ and is inserted as a quoted identifier.

preflight_sql(prefix)

@spec preflight_sql(String.t()) :: String.t()

DO block that raises check_violation listing every offender.

Installed databases are never rewritten. The hint points at mix pgflow.audit_slugs and docs/UPGRADING_UPSTREAM_2026_10.md.

read_only_statement()

@spec read_only_statement() :: String.t()

The statement run/2 issues before reading findings.

reason_sql(expr)

@spec reason_sql(String.t()) :: String.t()

CASE expression that returns the PgFlow.Slug reason as text, or NULL when valid.

rule_sql(expr)

@spec rule_sql(String.t()) :: String.t()

Boolean SQL for one slug expression, in the same order as PgFlow.Slug.validate/1.

expr is interpolated, so it must be a trusted SQL expression such as $1 or a column name. This does not call pgflow.is_valid_slug/1.

run(repo, opts \\ [])

@spec run(module(), keyword()) :: [finding()]

Returns slug findings for the given repo.

Issues SET TRANSACTION READ ONLY as the first statement of a transaction, then reads findings_sql/1. A caller already inside a transaction that has run statements (the Ecto SQL sandbox) makes PostgreSQL reject that command with 25001. The savepoint is rolled back and the same read-only SELECT runs in the caller's transaction. mix pgflow.audit_slugs opens a fresh transaction, so that path stays read only.

Flow rows omit a nil step_slug so the finding map matches the audit tests.