Skip to content

Commit 90b3408

Browse files
authored
Use new function `Repo.with_adapter/1' (#56)
This change adds `Repo.with_adapter/1` as suggested in #51 (comment)
1 parent 657f753 commit 90b3408

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

lib/error_tracker/migration.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ defmodule ErrorTracker.Migration do
109109
end
110110

111111
defp migrator do
112-
case ErrorTracker.Repo.__adapter__() do
113-
Ecto.Adapters.Postgres -> ErrorTracker.Migration.Postgres
114-
Ecto.Adapters.SQLite3 -> ErrorTracker.Migration.SQLite
112+
ErrorTracker.Repo.with_adapter(fn
113+
:postgres -> ErrorTracker.Migration.Postgres
114+
:sqlite -> ErrorTracker.Migration.SQLite
115115
adapter -> raise "ErrorTracker does not support #{adapter}"
116-
end
116+
end)
117117
end
118118
end

lib/error_tracker/migration/sql_migrator.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ defmodule ErrorTracker.Migration.SQLMigrator do
6363
defp record_version(opts, version) do
6464
timestamp = DateTime.utc_now() |> DateTime.to_unix()
6565

66-
case repo().__adapter__() do
67-
Ecto.Adapters.Postgres ->
66+
ErrorTracker.Repo.with_adapter(fn
67+
:postgres ->
6868
prefix = opts[:prefix]
6969

7070
execute """
@@ -79,12 +79,12 @@ defmodule ErrorTracker.Migration.SQLMigrator do
7979
VALUES ('migration_version', '#{version}'), ('migration_timestamp', '#{timestamp}')
8080
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value
8181
"""
82-
end
82+
end)
8383
end
8484

8585
defp meta_table_exists?(repo, opts) do
86-
case repo.__adapter__() do
87-
Ecto.Adapters.Postgres ->
86+
ErrorTracker.Repo.with_adapter(fn
87+
:postgres ->
8888
Ecto.Adapters.SQL.query!(
8989
repo,
9090
"SELECT TRUE FROM information_schema.tables WHERE table_name = 'error_tracker_meta' AND table_schema = $1",
@@ -96,6 +96,6 @@ defmodule ErrorTracker.Migration.SQLMigrator do
9696

9797
_other ->
9898
Ecto.Adapters.SQL.table_exists?(repo, "error_tracker_meta", log: false)
99-
end
99+
end)
100100
end
101101
end

lib/error_tracker/repo.ex

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,24 @@ defmodule ErrorTracker.Repo do
2929
dispatch(:aggregate, [queryable, aggregate], opts)
3030
end
3131

32-
def __adapter__, do: repo().__adapter__()
32+
def with_adapter(fun) do
33+
adapter =
34+
case repo().__adapter__() do
35+
Ecto.Adapters.Postgres -> :postgres
36+
Ecto.Adapters.SQLite3 -> :sqlite
37+
end
38+
39+
fun.(adapter)
40+
end
3341

3442
defp dispatch(action, args, opts) do
3543
repo = repo()
3644

3745
defaults =
38-
case repo.__adapter__() do
39-
Ecto.Adapters.Postgres ->
40-
[prefix: Application.get_env(:error_tracker, :prefix, "public")]
41-
42-
_ ->
43-
[]
44-
end
46+
with_adapter(fn
47+
:postgres -> [prefix: Application.get_env(:error_tracker, :prefix, "public")]
48+
_ -> []
49+
end)
4550

4651
opts_w_defaults = Keyword.merge(defaults, opts)
4752

0 commit comments

Comments
 (0)