Skip to content

Commit fd36f60

Browse files
authored
Merge pull request #6 from RiverFinancial/mike/2023-07-19/merge-multi-repo-support
Re-add support for multiple repos, upgrade ecto to 3.10.1
2 parents 18a1424 + 587c2bd commit fd36f60

File tree

11 files changed

+132
-167
lines changed

11 files changed

+132
-167
lines changed

.github/workflows/elixir.yml

Lines changed: 3 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ env:
66

77
# https://github.com/elixir-lang/elixir/blob/master/lib/elixir/pages/compatibility-and-deprecations.md
88
jobs:
9-
elixir_1_13:
9+
elixir_1_15:
1010
runs-on: ubuntu-latest
1111
name: OTP ${{matrix.otp}} / Elixir ${{matrix.elixir}}
1212
strategy:
1313
matrix:
14-
otp: [22.x, 23.x, 24.x]
15-
elixir: [1.13.x]
14+
otp: [26.x]
15+
elixir: [1.15.x]
1616
services:
1717
postgres:
1818
image: postgres:14
@@ -45,82 +45,3 @@ jobs:
4545
# - run: mix compile --warnings-as-errors
4646
- run: mix format --dry-run --check-formatted
4747
- run: mix test
48-
49-
elixir_1_12:
50-
runs-on: ubuntu-latest
51-
name: OTP ${{matrix.otp}} / Elixir ${{matrix.elixir}}
52-
strategy:
53-
matrix:
54-
otp: [22.x, 23.x, 24.x]
55-
elixir: [1.12.x]
56-
services:
57-
postgres:
58-
image: postgres:14
59-
env:
60-
POSTGRES_USER: postgres
61-
POSTGRES_PASSWORD: postgres
62-
POSTGRES_DB: ex_audit_test
63-
ports:
64-
- 5432:5432
65-
options: >-
66-
--health-cmd pg_isready
67-
--health-interval 10s
68-
--health-timeout 5s
69-
--health-retries 5
70-
steps:
71-
- uses: actions/checkout@v2
72-
- uses: erlef/setup-beam@v1
73-
with:
74-
otp-version: ${{matrix.otp}}
75-
elixir-version: ${{matrix.elixir}}
76-
- uses: actions/cache@v3
77-
with:
78-
path: |
79-
deps
80-
_build
81-
key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
82-
restore-keys: |
83-
${{ runner.os }}-mix-
84-
- run: mix do deps.get
85-
# - run: mix compile --warnings-as-errors
86-
- run: mix format --dry-run --check-formatted
87-
- run: mix test
88-
89-
elixir_1_11:
90-
runs-on: ubuntu-latest
91-
name: OTP ${{matrix.otp}} / Elixir ${{matrix.elixir}}
92-
strategy:
93-
matrix:
94-
otp: [21.x, 22.x, 23.x, 24.x]
95-
elixir: [1.11.x]
96-
services:
97-
postgres:
98-
image: postgres:14
99-
env:
100-
POSTGRES_USER: postgres
101-
POSTGRES_PASSWORD: postgres
102-
POSTGRES_DB: ex_audit_test
103-
ports:
104-
- 5432:5432
105-
options: >-
106-
--health-cmd pg_isready
107-
--health-interval 10s
108-
--health-timeout 5s
109-
--health-retries 5
110-
steps:
111-
- uses: actions/checkout@v2
112-
- uses: erlef/setup-beam@v1
113-
with:
114-
otp-version: ${{matrix.otp}}
115-
elixir-version: ${{matrix.elixir}}
116-
- uses: actions/cache@v3
117-
with:
118-
path: |
119-
deps
120-
_build
121-
key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
122-
restore-keys: |
123-
${{ runner.os }}-mix-
124-
- run: mix do deps.get
125-
# - run: mix compile --warnings-as-errors
126-
- run: mix test

.tool-versions

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
elixir 1.15.2-otp-26
2+
erlang 26.0.2
3+
nodejs 15.10.0

config/test.exs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ config :logger, level: :info
1313

1414
config :ex_audit,
1515
ecto_repos: [ExAudit.Test.Repo],
16-
version_schema: ExAudit.Test.Version,
17-
tracked_schemas: [
18-
ExAudit.Test.User,
19-
ExAudit.Test.BlogPost,
20-
ExAudit.Test.BlogPost.Section,
21-
ExAudit.Test.Comment
22-
],
16+
ecto_repos_schemas: %{
17+
ExAudit.Test.Repo => %{
18+
version_schema: ExAudit.Test.Version,
19+
tracked_schemas: [
20+
ExAudit.Test.User,
21+
ExAudit.Test.BlogPost,
22+
ExAudit.Test.BlogPost.Section,
23+
ExAudit.Test.Comment
24+
]
25+
}
26+
},
2327
primitive_structs: [
2428
Date
2529
]

lib/repo/queryable.ex

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
defmodule ExAudit.Queryable do
22
require Logger
3-
43
import Ecto.Query, only: [from: 2, where: 3]
54

6-
defp version_schema() do
7-
Application.get_env(:ex_audit, :version_schema)
8-
end
9-
10-
@compile {:inline, version_schema: 0}
11-
125
def update_all(module, queryable, updates, opts) do
136
Ecto.Repo.Queryable.update_all(module, queryable, updates, opts)
147
end
@@ -20,7 +13,7 @@ defmodule ExAudit.Queryable do
2013
def history(module, struct, opts) do
2114
query =
2215
from(
23-
v in version_schema(),
16+
v in version_schema(module),
2417
order_by: [desc: :recorded_at]
2518
)
2619

@@ -64,7 +57,7 @@ defmodule ExAudit.Queryable do
6457

6558
versions ++
6659
[
67-
struct(version_schema(), %{
60+
struct(version_schema(module), %{
6861
id: oldest_id
6962
})
7063
|> Map.put(:original, empty_map_to_nil(oldest_struct))
@@ -74,13 +67,13 @@ defmodule ExAudit.Queryable do
7467
end
7568
end
7669

77-
def history_query(%{id: id, __struct__: struct}) do
70+
def history_query(module, %{id: id, __struct__: struct}) do
7871
from(
79-
v in version_schema(),
80-
where: v.entity_id == ^id,
81-
where: v.entity_schema == ^struct,
82-
order_by: [desc: :recorded_at]
83-
)
72+
v in version_schema(module),
73+
where: v.entity_id == ^id,
74+
where: v.entity_schema == ^struct,
75+
order_by: [desc: :recorded_at]
76+
)
8477
end
8578

8679
@drop_fields [:__meta__, :__struct__]
@@ -92,7 +85,7 @@ defmodule ExAudit.Queryable do
9285

9386
query =
9487
from(
95-
v in version_schema(),
88+
v in version_schema(module),
9689
where: v.entity_id == ^version.entity_id,
9790
where: v.entity_schema == ^version.entity_schema,
9891
where: v.recorded_at >= ^version.recorded_at,
@@ -148,7 +141,7 @@ defmodule ExAudit.Queryable do
148141
_ -> res
149142
end
150143
else
151-
Logger.warn([
144+
Logger.warning([
152145
"Can't revert ",
153146
inspect(version),
154147
" because the entity would still be deleted"
@@ -185,4 +178,8 @@ defmodule ExAudit.Queryable do
185178
defp reverse_action(:updated), do: :updated
186179
defp reverse_action(:created), do: :deleted
187180
defp reverse_action(:deleted), do: :created
181+
182+
defp version_schema(repo_module) do
183+
Application.get_env(:ex_audit, :ecto_repos_schemas) |> get_in([repo_module, :version_schema])
184+
end
188185
end

lib/repo/repo.ex

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ defmodule ExAudit.Repo do
7474
end
7575
```
7676
"""
77-
def tracked?(struct_or_changeset) do
78-
tracked_schemas = Application.get_env(:ex_audit, :tracked_schemas, [])
77+
@compile {:inline, tracked?: 2}
78+
79+
defp tracked?(repo_module, struct_or_changeset) do
80+
tracked_schemas = ExAudit.Tracking.tracked_schemas(repo_module)
7981

8082
schema =
8183
case struct_or_changeset do
@@ -89,14 +91,12 @@ defmodule ExAudit.Repo do
8991
schema in tracked_schemas
9092
end
9193

92-
@compile {:inline, tracked?: 1}
93-
94-
defoverridable(tracked?: 1)
94+
defoverridable(tracked?: 2)
9595

9696
def insert(struct, opts) do
9797
repo = get_dynamic_repo()
9898

99-
if tracked?(struct) do
99+
if tracked?(__MODULE__, struct) do
100100
ExAudit.Schema.insert(
101101
__MODULE__,
102102
repo,
@@ -111,7 +111,7 @@ defmodule ExAudit.Repo do
111111
def update(struct, opts) do
112112
repo = get_dynamic_repo()
113113

114-
if tracked?(struct) do
114+
if tracked?(__MODULE__, struct) do
115115
ExAudit.Schema.update(
116116
__MODULE__,
117117
repo,
@@ -126,7 +126,7 @@ defmodule ExAudit.Repo do
126126
def insert_or_update(changeset, opts) do
127127
repo = get_dynamic_repo()
128128

129-
if tracked?(changeset) do
129+
if tracked?(__MODULE__, changeset) do
130130
ExAudit.Schema.insert_or_update(
131131
__MODULE__,
132132
repo,
@@ -141,7 +141,7 @@ defmodule ExAudit.Repo do
141141
def delete(struct, opts) do
142142
repo = get_dynamic_repo()
143143

144-
if tracked?(struct) do
144+
if tracked?(__MODULE__, struct) do
145145
ExAudit.Schema.delete(
146146
__MODULE__,
147147
repo,
@@ -156,7 +156,7 @@ defmodule ExAudit.Repo do
156156
def insert!(struct, opts) do
157157
repo = get_dynamic_repo()
158158

159-
if tracked?(struct) do
159+
if tracked?(__MODULE__, struct) do
160160
ExAudit.Schema.insert!(
161161
__MODULE__,
162162
repo,
@@ -171,7 +171,7 @@ defmodule ExAudit.Repo do
171171
def update!(struct, opts) do
172172
repo = get_dynamic_repo()
173173

174-
if tracked?(struct) do
174+
if tracked?(__MODULE__, struct) do
175175
ExAudit.Schema.update!(
176176
__MODULE__,
177177
repo,
@@ -186,7 +186,7 @@ defmodule ExAudit.Repo do
186186
def insert_or_update!(changeset, opts) do
187187
repo = get_dynamic_repo()
188188

189-
if tracked?(changeset) do
189+
if tracked?(__MODULE__, changeset) do
190190
ExAudit.Schema.insert_or_update!(
191191
__MODULE__,
192192
repo,
@@ -201,7 +201,7 @@ defmodule ExAudit.Repo do
201201
def delete!(struct, opts) do
202202
repo = get_dynamic_repo()
203203

204-
if tracked?(struct) do
204+
if tracked?(__MODULE__, struct) do
205205
ExAudit.Schema.delete!(
206206
__MODULE__,
207207
repo,
@@ -226,7 +226,7 @@ defmodule ExAudit.Repo do
226226
end
227227

228228
def history_query(struct) do
229-
ExAudit.Queryable.history_query(struct)
229+
ExAudit.Queryable.history_query(__MODULE__, struct)
230230
end
231231
end
232232
end

0 commit comments

Comments
 (0)