Skip to content

Commit

Permalink
Merge pull request #2 from Kry10-NZ/fix-ecto-3.8
Browse files Browse the repository at this point in the history
Update Ecto.Repo calls to work with Ecto 3.8
  • Loading branch information
simoncocking authored Jul 12, 2022
2 parents e70c463 + 4a445f8 commit 404f174
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 47 deletions.
2 changes: 1 addition & 1 deletion lib/repo/queryable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule ExAudit.Queryable do
)
end

versions = Ecto.Repo.Queryable.all(module, query, opts)
versions = Ecto.Repo.Queryable.all(module, query, Ecto.Repo.Supervisor.tuplet(module, opts))

if Keyword.get(opts, :render_struct, false) do
{versions, oldest_struct} =
Expand Down
54 changes: 32 additions & 22 deletions lib/repo/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,113 +94,125 @@ defmodule ExAudit.Repo do
defoverridable(tracked?: 1)

def insert(struct, opts) do
repo = get_dynamic_repo()

if tracked?(struct) do
ExAudit.Schema.insert(
__MODULE__,
get_dynamic_repo(),
repo,
struct,
opts
Ecto.Repo.Supervisor.tuplet(repo, prepare_opts(:insert, opts))
)
else
super(struct, opts)
end
end

def update(struct, opts) do
repo = get_dynamic_repo()

if tracked?(struct) do
ExAudit.Schema.update(
__MODULE__,
get_dynamic_repo(),
repo,
struct,
opts
Ecto.Repo.Supervisor.tuplet(repo, prepare_opts(:update, opts))
)
else
super(struct, opts)
end
end

def insert_or_update(changeset, opts) do
repo = get_dynamic_repo()

if tracked?(changeset) do
ExAudit.Schema.insert_or_update(
__MODULE__,
get_dynamic_repo(),
repo,
changeset,
opts
Ecto.Repo.Supervisor.tuplet(repo, prepare_opts(:insert_or_update, opts))
)
else
super(changeset, opts)
end
end

def delete(struct, opts) do
repo = get_dynamic_repo()

if tracked?(struct) do
ExAudit.Schema.delete(
__MODULE__,
get_dynamic_repo(),
repo,
struct,
opts
Ecto.Repo.Supervisor.tuplet(repo, prepare_opts(:delete, opts))
)
else
super(struct, opts)
end
end

def insert!(struct, opts) do
repo = get_dynamic_repo()

if tracked?(struct) do
ExAudit.Schema.insert!(
__MODULE__,
get_dynamic_repo(),
repo,
struct,
opts
Ecto.Repo.Supervisor.tuplet(repo, prepare_opts(:insert, opts))
)
else
super(struct, opts)
end
end

def update!(struct, opts) do
repo = get_dynamic_repo()

if tracked?(struct) do
ExAudit.Schema.update!(
__MODULE__,
get_dynamic_repo(),
repo,
struct,
opts
Ecto.Repo.Supervisor.tuplet(repo, prepare_opts(:update, opts))
)
else
super(struct, opts)
end
end

def insert_or_update!(changeset, opts) do
repo = get_dynamic_repo()

if tracked?(changeset) do
ExAudit.Schema.insert_or_update!(
__MODULE__,
get_dynamic_repo(),
repo,
changeset,
opts
Ecto.Repo.Supervisor.tuplet(repo, prepare_opts(:insert_or_update, opts))
)
else
super(changeset, opts)
end
end

def delete!(struct, opts) do
repo = get_dynamic_repo()

if tracked?(struct) do
ExAudit.Schema.delete!(
__MODULE__,
get_dynamic_repo(),
repo,
struct,
opts
Ecto.Repo.Supervisor.tuplet(repo, prepare_opts(:delete, opts))
)
else
super(struct, opts)
end
end

def default_options(_operation), do: []

defoverridable(default_options: 1)

defoverridable(child_spec: 1)

# additional functions
Expand Down Expand Up @@ -237,6 +249,4 @@ defmodule ExAudit.Repo do
"""
@callback revert(version :: struct, opts :: list) ::
{:ok, struct} | {:error, changeset :: Ecto.Changeset.t()}

@callback default_options(operation :: atom) :: keyword
end
36 changes: 18 additions & 18 deletions lib/repo/schema.ex
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
defmodule ExAudit.Schema do
def insert_all(module, name, schema_or_source, entries, opts) do
def insert_all(module, name, schema_or_source, entries, tuplet = {_adapter_meta, opts}) do
# TODO!
opts = augment_opts(opts)
Ecto.Repo.Schema.insert_all(module, name, schema_or_source, entries, opts)
Ecto.Repo.Schema.insert_all(module, name, schema_or_source, entries, tuplet)
end

def insert(module, name, struct, opts) do
def insert(module, name, struct, tuplet = {_adapter_meta, opts}) do
opts = augment_opts(opts)

augment_transaction(module, fn ->
result = Ecto.Repo.Schema.insert(module, name, struct, opts)
result = Ecto.Repo.Schema.insert(module, name, struct, tuplet)

case result do
{:ok, resulting_struct} ->
Expand All @@ -23,11 +23,11 @@ defmodule ExAudit.Schema do
end)
end

def update(module, name, struct, opts) do
def update(module, name, struct, tuplet = {_adapter_meta, opts}) do
opts = augment_opts(opts)

augment_transaction(module, fn ->
result = Ecto.Repo.Schema.update(module, name, struct, opts)
result = Ecto.Repo.Schema.update(module, name, struct, tuplet)

case result do
{:ok, resulting_struct} ->
Expand All @@ -41,11 +41,11 @@ defmodule ExAudit.Schema do
end)
end

def insert_or_update(module, name, changeset, opts) do
def insert_or_update(module, name, changeset, tuplet = {_adapter_meta, opts}) do
opts = augment_opts(opts)

augment_transaction(module, fn ->
result = Ecto.Repo.Schema.insert_or_update(module, name, changeset, opts)
result = Ecto.Repo.Schema.insert_or_update(module, name, changeset, tuplet)

case result do
{:ok, resulting_struct} ->
Expand All @@ -60,12 +60,12 @@ defmodule ExAudit.Schema do
end)
end

def delete(module, name, struct, opts) do
def delete(module, name, struct, tuplet = {_adapter_meta, opts}) do
opts = augment_opts(opts)

augment_transaction(module, fn ->
ExAudit.Tracking.track_assoc_deletion(module, struct, opts)
result = Ecto.Repo.Schema.delete(module, name, struct, opts)
result = Ecto.Repo.Schema.delete(module, name, struct, tuplet)

case result do
{:ok, resulting_struct} ->
Expand All @@ -79,41 +79,41 @@ defmodule ExAudit.Schema do
end)
end

def insert!(module, name, struct, opts) do
def insert!(module, name, struct, tuplet = {_adapter_meta, opts}) do
opts = augment_opts(opts)

augment_transaction(
module,
fn ->
result = Ecto.Repo.Schema.insert!(module, name, struct, opts)
result = Ecto.Repo.Schema.insert!(module, name, struct, tuplet)
ExAudit.Tracking.track_change(module, :created, struct, result, opts)
result
end,
true
)
end

def update!(module, name, struct, opts) do
def update!(module, name, struct, tuplet = {_adapter_meta, opts}) do
opts = augment_opts(opts)

augment_transaction(
module,
fn ->
result = Ecto.Repo.Schema.update!(module, name, struct, opts)
result = Ecto.Repo.Schema.update!(module, name, struct, tuplet)
ExAudit.Tracking.track_change(module, :updated, struct, result, opts)
result
end,
true
)
end

def insert_or_update!(module, name, changeset, opts) do
def insert_or_update!(module, name, changeset, tuplet = {_adapter_meta, opts}) do
opts = augment_opts(opts)

augment_transaction(
module,
fn ->
result = Ecto.Repo.Schema.insert_or_update!(module, name, changeset, opts)
result = Ecto.Repo.Schema.insert_or_update!(module, name, changeset, tuplet)
state = if changeset.data.__meta__.state == :loaded, do: :updated, else: :created
ExAudit.Tracking.track_change(module, state, changeset, result, opts)
result
Expand All @@ -122,14 +122,14 @@ defmodule ExAudit.Schema do
)
end

def delete!(module, name, struct, opts) do
def delete!(module, name, struct, tuplet = {_adapter_meta, opts}) do
opts = augment_opts(opts)

augment_transaction(
module,
fn ->
ExAudit.Tracking.track_assoc_deletion(module, struct, opts)
result = Ecto.Repo.Schema.delete!(module, name, struct, opts)
result = Ecto.Repo.Schema.delete!(module, name, struct, tuplet)
ExAudit.Tracking.track_change(module, :deleted, struct, result, opts)
result
end,
Expand Down
12 changes: 6 additions & 6 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
%{
"certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "805abd97539caf89ec6d4732c91e62ba9da0cda51ac462380bbd28ee697a8c42"},
"connection": {:hex, :connection, "1.1.0", "ff2a49c4b75b6fb3e674bfc5536451607270aac754ffd1bdfe175abe4a6d7a68", [:mix], [], "hexpm", "722c1eb0a418fbe91ba7bd59a47e28008a189d47e37e0e7bb85585a016b2869c"},
"db_connection": {:hex, :db_connection, "2.4.0", "d04b1b73795dae60cead94189f1b8a51cc9e1f911c234cc23074017c43c031e5", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ad416c21ad9f61b3103d254a71b63696ecadb6a917b36f563921e0de00d7d7c8"},
"db_connection": {:hex, :db_connection, "2.4.2", "f92e79aff2375299a16bcb069a14ee8615c3414863a6fef93156aee8e86c2ff3", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4fe53ca91b99f55ea249693a0229356a08f4d1a7931d8ffa79289b145fe83668"},
"decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"},
"earmark": {:hex, :earmark, "1.4.3", "364ca2e9710f6bff494117dbbd53880d84bebb692dafc3a78eb50aa3183f2bfd", [:mix], [], "hexpm", "8cf8a291ebf1c7b9539e3cddb19e9cef066c2441b1640f13c34c1d3cfc825fec"},
"earmark_parser": {:hex, :earmark_parser, "1.4.13", "0c98163e7d04a15feb62000e1a891489feb29f3d10cb57d4f845c405852bbef8", [:mix], [], "hexpm", "d602c26af3a0af43d2f2645613f65841657ad6efc9f0e361c3b6c06b578214ba"},
"ecto": {:hex, :ecto, "3.6.2", "efdf52acfc4ce29249bab5417415bd50abd62db7b0603b8bab0d7b996548c2bc", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "efad6dfb04e6f986b8a3047822b0f826d9affe8e4ebdd2aeedbfcb14fd48884e"},
"ecto_sql": {:hex, :ecto_sql, "3.6.2", "9526b5f691701a5181427634c30655ac33d11e17e4069eff3ae1176c764e0ba3", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.6.2", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.4.0 or ~> 0.5.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "5ec9d7e6f742ea39b63aceaea9ac1d1773d574ea40df5a53ef8afbd9242fdb6b"},
"ecto": {:hex, :ecto, "3.8.3", "5e681d35bc2cbb46dcca1e2675837c7d666316e5ada14eca6c9c609b6232817c", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "af92dd7815967bcaea0daaaccf31c3b23165432b1c7a475d84144efbc703d105"},
"ecto_sql": {:hex, :ecto_sql, "3.8.2", "d7d44bc8d45ba9c85485952710c80408632a7336eb811b045e791718d11ddb5b", [:mix], [{:db_connection, "~> 2.5 or ~> 2.4.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.8.1", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0 or ~> 0.16.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7b9b03d64360d6cc05dc263500a43c11740b5fd4552244c27efad358e98c75b3"},
"ex_doc": {:hex, :ex_doc, "0.24.2", "e4c26603830c1a2286dae45f4412a4d1980e1e89dc779fcd0181ed1d5a05c8d9", [:mix], [{:earmark_parser, "~> 1.4.0", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "e134e1d9e821b8d9e4244687fb2ace58d479b67b282de5158333b0d57c6fb7da"},
"excoveralls": {:hex, :excoveralls, "0.12.3", "2142be7cb978a3ae78385487edda6d1aff0e482ffc6123877bb7270a8ffbcfe0", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "568a3e616c264283f5dea5b020783ae40eef3f7ee2163f7a67cbd7b35bcadada"},
"file_system": {:hex, :file_system, "0.2.8", "f632bd287927a1eed2b718f22af727c5aeaccc9a98d8c2bd7bff709e851dc986", [:mix], [], "hexpm", "97a3b6f8d63ef53bd0113070102db2ce05352ecf0d25390eb8d747c2bde98bca"},
"hackney": {:hex, :hackney, "1.15.2", "07e33c794f8f8964ee86cebec1a8ed88db5070e52e904b8f12209773c1036085", [:rebar3], [{:certifi, "2.5.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.5", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "e0100f8ef7d1124222c11ad362c857d3df7cb5f4204054f9f0f4a728666591fc"},
"idna": {:hex, :idna, "6.0.0", "689c46cbcdf3524c44d5f3dde8001f364cd7608a99556d8fbd8239a5798d4c10", [:rebar3], [{:unicode_util_compat, "0.4.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "4bdd305eb64e18b0273864920695cb18d7a2021f31a11b9c5fbcd9a253f936e2"},
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
"jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"},
"makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"},
"makeup_elixir": {:hex, :makeup_elixir, "0.15.1", "b5888c880d17d1cc3e598f05cdb5b5a91b7b17ac4eaf5f297cb697663a1094dd", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "db68c173234b07ab2a07f645a5acdc117b9f99d69ebf521821d89690ae6c6ec8"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
Expand All @@ -21,8 +21,8 @@
"mix_test_watch": {:hex, :mix_test_watch, "1.0.2", "34900184cbbbc6b6ed616ed3a8ea9b791f9fd2088419352a6d3200525637f785", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}], "hexpm", "47ac558d8b06f684773972c6d04fcc15590abdb97aeb7666da19fcbfdc441a07"},
"nimble_parsec": {:hex, :nimble_parsec, "1.1.0", "3a6fca1550363552e54c216debb6a9e95bd8d32348938e13de5eda962c0d7f89", [:mix], [], "hexpm", "08eb32d66b706e913ff748f11694b17981c0b04a33ef470e33e11b3d3ac8f54b"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"},
"postgrex": {:hex, :postgrex, "0.15.9", "46f8fe6f25711aeb861c4d0ae09780facfdf3adbd2fb5594ead61504dd489bda", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "610719103e4cb2223d4ab78f9f0f3e720320eeca6011415ab4137ddef730adee"},
"postgrex": {:hex, :postgrex, "0.16.3", "fac79a81a9a234b11c44235a4494d8565303fa4b9147acf57e48978a074971db", [:mix], [{:connection, "~> 1.1", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "aeaae1d2d1322da4e5fe90d241b0a564ce03a3add09d7270fb85362166194590"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.5", "6eaf7ad16cb568bb01753dbbd7a95ff8b91c7979482b95f38443fe2c8852a79b", [:make, :mix, :rebar3], [], "hexpm", "13104d7897e38ed7f044c4de953a6c28597d1c952075eb2e328bc6d6f2bfc496"},
"telemetry": {:hex, :telemetry, "0.4.3", "a06428a514bdbc63293cd9a6263aad00ddeb66f608163bdec7c8995784080818", [:rebar3], [], "hexpm", "eb72b8365ffda5bed68a620d1da88525e326cb82a75ee61354fc24b844768041"},
"telemetry": {:hex, :telemetry, "1.1.0", "a589817034a27eab11144ad24d5c0f9fab1f58173274b1e9bae7074af9cbee51", [:rebar3], [], "hexpm", "b727b2a1f75614774cff2d7565b64d0dfa5bd52ba517f16543e6fc7efcc0df48"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm", "1d1848c40487cdb0b30e8ed975e34e025860c02e419cb615d255849f3427439d"},
}

0 comments on commit 404f174

Please sign in to comment.