Rbtz.CredoChecks.Refactor.PreferEctoMigrationHelper (rbtz_credo_checks v0.3.0)

Copy Markdown View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

This check has a base priority of normal and works with any version of Elixir.

Explanation

Discourages raw SQL execute("...") in Ecto migrations when an equivalent migration helper exists.

Ecto's migration helpers (create, alter, add, modify, remove, rename, unique_index, references, ...) are reversible, get formatted into the down/0 reverse migration automatically, and are portable across the Ecto-supported adapters.

Reach for raw execute/1 only when no helper covers your case (e.g. backfilling rows, vendor-specific DDL like CREATE EXTENSION); when you do, prefer execute/2 so the reverse SQL is also captured.

Bad

execute("ALTER TABLE users ADD COLUMN status text NOT NULL DEFAULT 'pending'")

Good

alter table(:users) do
  add :status, :text, null: false, default: "pending"
end

# ...or, when raw SQL is genuinely required, capture both directions:
execute(
  "UPDATE users SET status = 'pending' WHERE status IS NULL",
  "UPDATE users SET status = NULL WHERE status = 'pending'"
)

Check-Specific Parameters

There are no specific parameters for this check.

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.