Skip to content

Commit

Permalink
Added :only option
Browse files Browse the repository at this point in the history
  • Loading branch information
akoutmos committed Jan 29, 2023
1 parent b86ca51 commit 5a33842
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 50 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.2.0] - 2023-01-28

### Added

- New `:only` option to the `use` macro to enable the debugger only in desired `Mix.env()` environments.

## [0.1.0] - 2023-01-25

### Added

- Initial release for EctoDbg to format SQL queries
- Initial release for EctoDbg to format SQL queries.
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,16 @@ Checkout my [GitHub Sponsorship page](https://github.com/sponsors/akoutmos) if y

## Setting Up EctoDbg

After adding `{:ecto_dbg, "~> 0.1.0", only: [:test, :dev]}` in your `mix.exs` file and running
`mix deps.get`, open your `repo.ex` file and add the following contents:
After adding `{:ecto_dbg, "~> 0.1.0"}` in your `mix.exs` file and running `mix deps.get`, open your `repo.ex` file and
add the following contents:

```elixir
defmodule MyApp.Repo do
use Ecto.Repo,
otp_app: :my_app,
adapter: Ecto.Adapters.Postgres

unless Mix.env() == :prod do
use EctoDbg
end
use EctoDbg
end
```

Expand All @@ -101,9 +99,7 @@ defmodule MyApp.Repo do
otp_app: :my_app,
adapter: Ecto.Adapters.Postgres

unless Mix.env() == :prod do
use EctoDbg, level: :info
end
use EctoDbg, level: :info
end
```

Expand All @@ -116,6 +112,20 @@ query = from user in User
Repo.all_and_log(query)
```

By default the `use EctoDbg` macro will inject the debug functions into your repo module for only the `:test` and `:dev`
`Mix.env()` environments. If you would like to override this default behaviour, you can do that by providing the `:only`
option (this value should be a subset of the environments that you passed in your `mix.exs` file):

```elixir
defmodule MyApp.Repo do
use Ecto.Repo,
otp_app: :my_app,
adapter: Ecto.Adapters.Postgres

use EctoDbg, only: :dev
end
```

## Attribution

- EctoDbg builds upon the [EctoDevLogger](https://github.com/fuelen/ecto_dev_logger) package and has reused some of the
Expand Down
107 changes: 67 additions & 40 deletions lib/ecto_dbg.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ defmodule EctoDbg do
to your repo module. This functions are:
* `all_and_log/1` and `all_and_log/2`
* `one_and_log/1` and `one_and_log/2`
* `update_all_and_log/1` and `update_all_and_log/2`
* `delete_all_and_log/1` and `delete_all_and_log/2`
* `log_all_query/1`
* `log_one_query/1`
* `log_update_all_query/1`
* `log_delete_all_query/1`
Expand All @@ -51,60 +53,85 @@ defmodule EctoDbg do
defmacro __using__(opts \\ []) do
default_opts = [
level: :debug,
logger_function: {EctoDbg, :default_logger}
logger_function: {EctoDbg, :default_logger},
only: [:test, :dev]
]

dbg_opts = Keyword.merge(default_opts, opts)
dbg_opts =
default_opts
|> Keyword.merge(opts)
|> Keyword.update!(:only, fn
envs when is_list(envs) -> envs
env when is_atom(env) -> [env]
end)

quote do
@doc """
Run the `Repo.all` function and also log the raw SQL query.
"""
def all_and_log(query, query_opts \\ []) do
EctoDbg.run_and_log_query(__MODULE__, :all, unquote(dbg_opts), query, query_opts)
end
if Mix.env() in dbg_opts[:only] do
quote do
@doc """
Run the `Repo.all` function and also log the raw SQL query.
"""
def all_and_log(query, query_opts \\ []) do
EctoDbg.run_and_log_query(__MODULE__, :all, unquote(dbg_opts), query, query_opts)
end

@doc """
Run the `Repo.update_all` function and also log the raw SQL query.
"""
def update_all_and_log(query, query_opts \\ []) do
EctoDbg.run_and_log_query(__MODULE__, :update_all, unquote(dbg_opts), query, query_opts)
end
@doc """
Run the `Repo.one` function and also log the raw SQL query.
"""
def one_and_log(query, query_opts \\ []) do
EctoDbg.run_and_log_query(__MODULE__, :one, unquote(dbg_opts), query, query_opts)
end

@doc """
Run the `Repo.delete_all` function and also log the raw SQL query.
"""
def delete_all_and_log(query, query_opts \\ []) do
EctoDbg.run_and_log_query(__MODULE__, :delete_all, unquote(dbg_opts), query, query_opts)
end
@doc """
Run the `Repo.update_all` function and also log the raw SQL query.
"""
def update_all_and_log(query, query_opts \\ []) do
EctoDbg.run_and_log_query(__MODULE__, :update_all, unquote(dbg_opts), query, query_opts)
end

@doc """
Log the raw SQL query that would be passed to the `Repo.all` function.
"""
# The following functions only log the query and do not run it
def log_all_query(query) do
EctoDbg.log_query(__MODULE__, :all, unquote(dbg_opts), query)
end
@doc """
Run the `Repo.delete_all` function and also log the raw SQL query.
"""
def delete_all_and_log(query, query_opts \\ []) do
EctoDbg.run_and_log_query(__MODULE__, :delete_all, unquote(dbg_opts), query, query_opts)
end

@doc """
Log the raw SQL query that would be passed to the `Repo.update_all` function.
"""
def log_update_all_query(query) do
EctoDbg.log_query(__MODULE__, :update_all, unquote(dbg_opts), query)
end
@doc """
Log the raw SQL query that would be passed to the `Repo.all` function.
"""
# The following functions only log the query and do not run it
def log_all_query(query) do
EctoDbg.log_query(__MODULE__, :all, unquote(dbg_opts), query)
end

@doc """
Log the raw SQL query that would be passed to the `Repo.delete_all` function.
"""
def log_delete_all_query(query) do
EctoDbg.log_query(__MODULE__, :delete_all, unquote(dbg_opts), query)
@doc """
Log the raw SQL query that would be passed to the `Repo.one` function.
"""
# The following functions only log the query and do not run it
def log_one_query(query) do
EctoDbg.log_query(__MODULE__, :one, unquote(dbg_opts), query)
end

@doc """
Log the raw SQL query that would be passed to the `Repo.update_all` function.
"""
def log_update_all_query(query) do
EctoDbg.log_query(__MODULE__, :update_all, unquote(dbg_opts), query)
end

@doc """
Log the raw SQL query that would be passed to the `Repo.delete_all` function.
"""
def log_delete_all_query(query) do
EctoDbg.log_query(__MODULE__, :delete_all, unquote(dbg_opts), query)
end
end
end
end

@doc false
def log_query(repo, action, dbg_opts, query) do
{binary_query, params} = SQL.to_sql(action, repo, query)
normalized_action = if action == :one, do: :all, else: action
{binary_query, params} = SQL.to_sql(normalized_action, repo, query)

# Generate a formatted query
formatted_sql =
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule EctoDbg.MixProject do
def project do
[
app: :ecto_dbg,
version: "0.1.0",
version: "0.2.0",
elixir: "~> 1.12",
name: "EctoDbg",
source_url: "https://github.com/akoutmos/ecto_dbg",
Expand Down

0 comments on commit 5a33842

Please sign in to comment.