Bylaw.Credo.Check.Ecto.ContextOwnsSchemaQueries (bylaw_credo v0.3.1)

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 higher and works with any version of Elixir.

Explanation

Only configured Phoenix context boundary modules may write Ecto queries for schemas owned by their namespace.

Examples

Configure the context boundary modules that own schemas below their namespace:

{Bylaw.Credo.Check.Ecto.ContextOwnsSchemaQueries,
 [
   contexts: [
     MyApp.Conversations,
     MyApp.Branches,
     MyApp.Runs
   ]
 ]}

Avoid outside MyApp.Conversations:

  from(c in Conversation, where: c.id == ^id)
  Conversation |> where([c], c.visible)
  Repo.get_by(Conversation, slug: slug)
  Repo.insert(%Conversation{})

Prefer:

  MyApp.Conversations.fetch_conversation(id)

Plain schema references are allowed. This check is specifically about writing Ecto query or direct Repo CRUD logic for a schema owned by another configured context namespace.

Notes

A schema module is owned by the longest configured context prefix when the schema starts with that context plus one or more extra module segments. For example, MyApp.Conversations.Message is owned by MyApp.Conversations.

Nested modules under a context are not treated as owners by default. Only the exact configured context module may write queries for schemas under that namespace.

This check uses static AST analysis, so it favors clear source-level patterns over runtime behavior.

Options

Configure options in .credo.exs with the check tuple:

%{
  configs: [
    %{
      name: "default",
      checks: [
        {Bylaw.Credo.Check.Ecto.ContextOwnsSchemaQueries,
         [
           contexts: [MyApp.Conversations],
           excluded_modules: [MyApp.Legacy.ReportBuilder],
           excluded_paths: ["lib/my_app/generated/"],
           repo_modules: [MyApp.Repo]
         ]}
      ]
    }
  ]
}
  • :contexts - Context boundary modules that own schemas below their namespace.
  • :excluded_modules - Modules allowed to write queries for owned schemas.
  • :excluded_paths - Paths containing any configured string are skipped.
  • :repo_modules - Repo modules to inspect. When empty, any module whose last segment is Repo is treated as a Repo.
  • :allow_owner_descendants - When true, modules nested under the owner context may also write queries. Defaults to false.

Check-Specific Parameters

Use the following parameters to configure this check:

:contexts

Context boundary modules that own schemas below their namespace.

This parameter defaults to [].

:excluded_modules

Modules allowed to write queries for owned schemas.

This parameter defaults to [].

:excluded_paths

Paths containing any configured string are skipped.

This parameter defaults to [].

:repo_modules

Repo modules to inspect. When empty, any module whose last segment is Repo is treated as a Repo.

This parameter defaults to [].

:allow_owner_descendants

When true, modules nested under the owner context may also write queries.

This parameter defaults to false.

General Parameters

Like with all checks, general params can be applied.

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