-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathqueryable.ex
179 lines (139 loc) · 4.3 KB
/
queryable.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
defmodule ExAudit.Queryable do
require Logger
defp version_schema() do
Application.get_env(:ex_audit, :version_schema)
end
@compile {:inline, version_schema: 0}
def update_all(module, queryable, updates, opts) do
Ecto.Repo.Queryable.update_all(module, queryable, updates, opts)
end
def delete_all(module, queryable, opts) do
Ecto.Repo.Queryable.delete_all(module, queryable, opts)
end
def history(module, struct, opts) do
import Ecto.Query
query =
from(
v in version_schema(),
order_by: [desc: :recorded_at]
)
# TODO what do when we get a query
query =
case struct do
# %Ecto.Query{from: struct} ->
# from v in query,
# where: v.entity_id == subquery(from q in struct, select: q.id),
# where: v.entity_schema == ^struct
%{__struct__: struct, id: id} when nil not in [struct, id] ->
from(
v in query,
where: v.entity_id == ^id,
where: v.entity_schema == ^struct
)
end
versions = Ecto.Repo.Queryable.all(module, query, opts)
if Keyword.get(opts, :render_struct, false) do
{versions, oldest_struct} =
versions
|> Enum.map_reduce(struct, fn version, new_struct ->
old_struct = _revert(version, new_struct)
version =
version
|> Map.put(:original, empty_map_to_nil(new_struct))
|> Map.put(:first, false)
{version, old_struct}
end)
{versions, oldest_id} =
versions
|> Enum.map_reduce(nil, fn version, id ->
{%{version | id: id}, version.id}
end)
versions ++
[
struct(version_schema(), %{
id: oldest_id
})
|> Map.put(:original, empty_map_to_nil(oldest_struct))
]
else
versions
end
end
@drop_fields [:__meta__, :__struct__]
def revert(module, version, opts) do
import Ecto.Query
# get the history of the entity after this version
query =
from(
v in version_schema(),
where: v.entity_id == ^version.entity_id,
where: v.entity_schema == ^version.entity_schema,
where: v.recorded_at >= ^version.recorded_at,
order_by: [desc: :recorded_at]
)
versions = module.all(query)
# get the referenced struct as it exists now
struct = module.one(from(s in version.entity_schema, where: s.id == ^version.entity_id))
result = Enum.reduce(versions, struct, &_revert/2)
result = empty_map_to_nil(result)
schema = version.entity_schema
drop_from_params = @drop_fields ++ schema.__schema__(:associations)
{action, changeset} =
case {struct, result} do
{nil, %{}} ->
{:insert, schema.changeset(struct(schema, %{}), Map.drop(result, drop_from_params))}
{%{}, nil} ->
{:delete, struct}
{nil, nil} ->
{nil, nil}
_ ->
struct =
case Keyword.get(opts, :preload) do
nil -> struct
[] -> struct
preloads when is_list(preloads) -> module.preload(struct, preloads)
end
{:update, schema.changeset(struct, Map.drop(result, drop_from_params))}
end
opts =
Keyword.update(opts, :ex_audit_custom, [rollback: true], fn custom ->
[{:rollback, true} | custom]
end)
if action do
res = apply(module, action, [changeset, opts])
case action do
:delete -> {:ok, nil}
_ -> res
end
else
Logger.warn([
"Can't revert ",
inspect(version),
" because the entity would still be deleted"
])
{:ok, nil}
end
end
defp empty_map_to_nil(map) do
if map |> Map.keys() |> length() == 0 do
nil
else
map
end
end
defp _revert(version, struct) do
apply_change(reverse_action(version.action), ExAudit.Diff.reverse(version.patch), struct)
end
defp apply_change(:updated, patch, to) do
ExAudit.Patch.patch(to, patch)
end
defp apply_change(:deleted, _patch, _to) do
%{}
end
defp apply_change(:created, patch, _to) do
ExAudit.Patch.patch(%{}, patch)
end
defp reverse_action(:updated), do: :updated
defp reverse_action(:created), do: :deleted
defp reverse_action(:deleted), do: :created
end