Bylaw.Db.Adapters.Postgres.Checks.ForbiddenColumnTypes
(bylaw_postgres v0.2.0)
Copy Markdown
View Source
Validates that Postgres columns do not use configured forbidden types.
Examples
With rules: [types: [[type: "json", prefer: "jsonb"]]], before:
CREATE TABLE webhook_events (
id uuid PRIMARY KEY,
payload json NOT NULL
);The project has decided this type is limiting or unsafe for its use case. For
example, plain json is less useful for common indexing and containment
queries than jsonb.
After, use the preferred type:
CREATE TABLE webhook_events (
id uuid PRIMARY KEY,
payload jsonb NOT NULL
);The column now follows the project convention and avoids repeating the same migration decision in future tables.
Notes
This check is policy-driven and has no built-in opinion about which types are
bad. Type matchers compare against pg_catalog.format_type, so use exact
strings such as "json" or regexes such as ~r/^character\(/.
Options
:validate- explicitfalsedisables this check.:rules- rule keyword list or non-empty list of rule keyword lists.:types- required non-empty list of forbidden type rules inside each rule. Each type rule can be a string, regex, or keyword list with:type, optional:prefer, and optional:reason.
This check requires :types, so bare-module configuration is not valid.
Run globally:
{Bylaw.Db.Adapters.Postgres.Checks.ForbiddenColumnTypes,
rules: [
types: [
[type: "json", prefer: "jsonb"],
[type: ~r/^character\(/, prefer: "text"]
]
]}Run only for matching rule scopes:
{Bylaw.Db.Adapters.Postgres.Checks.ForbiddenColumnTypes,
rules: [
[
where: [schemas: ["public"], tables: ["webhook_events"]],
types: [[type: "json", prefer: "jsonb"]],
except: [[columns: ["raw_payload"]]]
]
]}Usage
Add this module to the checks passed to
Bylaw.Db.Adapters.Postgres.validate/2. See the
README usage section for the full ExUnit setup.
Summary
Functions
Implements the Bylaw.Db.Check validation callback.
Types
@type check_opt() :: {:validate, boolean()} | {:rules, scope_rule() | [scope_rule()]}
@type check_opts() :: [check_opt()]
@type matcher() :: [ schema: matcher_values(), table: matcher_values(), column: matcher_values(), type: matcher_values() ]
@type matcher_values() :: [matcher_value()]
@type normalized_rule() :: %{ type: type_matcher(), prefer: String.t() | nil, reason: String.t() | nil }
@type type_rule() :: type_matcher() | [type: type_matcher(), prefer: String.t(), reason: String.t()]
Functions
@spec validate(target :: Bylaw.Db.Target.t(), opts :: check_opts()) :: Bylaw.Db.Check.result()
Implements the Bylaw.Db.Check validation callback.