Skip to content

Commit 9b239bf

Browse files
authored
Merge pull request #10 from sheharyarn/bugfix/mnesia-cyclic-exits
Fix Mnesia's Cyclic Exits
2 parents e1f9893 + 357e078 commit 9b239bf

File tree

7 files changed

+40
-16
lines changed

7 files changed

+40
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Add `:memento` to your list of dependencies in your Mix file:
6363

6464
```elixir
6565
def deps do
66-
[{:memento, "~> 0.2.1"}]
66+
[{:memento, "~> 0.3.0"}]
6767
end
6868
```
6969

lib/memento/mnesia.ex

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ defmodule Memento.Mnesia do
1414

1515
@doc "Call an Mnesia function"
1616
defmacro call(method, arguments \\ []) do
17+
quote(bind_quoted: [fun: method, args: arguments]) do
18+
apply(:mnesia, fun, args)
19+
end
20+
end
21+
22+
23+
24+
@doc """
25+
Call an Mnesia function and catch any exits
26+
27+
Should ONLY be used with transaction methods, because catching
28+
exits inside transactions seriously impacts the performance of
29+
Mnesia.
30+
31+
Reference: https://github.com/sheharyarn/memento/issues/2
32+
"""
33+
defmacro call_and_catch(method, arguments \\ []) do
1734
quote(bind_quoted: [fun: method, args: arguments]) do
1835
require Memento.Error
1936

lib/memento/schema.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ defmodule Memento.Schema do
6666
end
6767

6868
:create_schema
69-
|> Memento.Mnesia.call([nodes])
69+
|> Memento.Mnesia.call_and_catch([nodes])
7070
|> Memento.Mnesia.handle_result
7171
end
7272

@@ -83,7 +83,7 @@ defmodule Memento.Schema do
8383
@spec delete(list(node)) :: :ok | {:error, any}
8484
def delete(nodes) do
8585
:delete_schema
86-
|> Memento.Mnesia.call([nodes])
86+
|> Memento.Mnesia.call_and_catch([nodes])
8787
|> Memento.Mnesia.handle_result
8888
end
8989

@@ -96,7 +96,7 @@ defmodule Memento.Schema do
9696
@spec info() :: :ok
9797
def info do
9898
:schema
99-
|> Memento.Mnesia.call
99+
|> Memento.Mnesia.call_and_catch
100100
|> Memento.Mnesia.handle_result
101101
end
102102

@@ -109,7 +109,7 @@ defmodule Memento.Schema do
109109
@spec info(Memento.Table.name) :: :ok
110110
def info(table) do
111111
:schema
112-
|> Memento.Mnesia.call([table])
112+
|> Memento.Mnesia.call_and_catch([table])
113113
|> Memento.Mnesia.handle_result
114114
end
115115

lib/memento/transaction.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ defmodule Memento.Transaction do
6868
@spec execute(fun, retries) :: {:ok, any} | {:error, any}
6969
def execute(function, retries \\ :infinity) do
7070
:transaction
71-
|> Memento.Mnesia.call([function, retries])
71+
|> Memento.Mnesia.call_and_catch([function, retries])
7272
|> Memento.Mnesia.handle_result
7373
end
7474

@@ -102,7 +102,7 @@ defmodule Memento.Transaction do
102102
@spec execute_sync(fun, retries) :: {:ok, any} | {:error, any}
103103
def execute_sync(function, retries \\ :infinity) do
104104
:sync_transaction
105-
|> Memento.Mnesia.call([function, retries])
105+
|> Memento.Mnesia.call_and_catch([function, retries])
106106
|> Memento.Mnesia.handle_result
107107
end
108108

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule Memento.Mixfile do
33

44
@app :memento
55
@name "Memento"
6-
@version "0.2.1"
6+
@version "0.3.0"
77
@github "https://github.com/sheharyarn/#{@app}"
88
@author "Sheharyar Naseer"
99
@license "MIT"

test/memento/mnesia_test.exs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ defmodule Memento.Tests.Mnesia do
77

88

99
describe "#call" do
10+
test "delegates method calls to the mnesia module" do
11+
assert :yes == Mnesia.call(:system_info, [:is_running])
12+
end
13+
end
14+
15+
16+
17+
describe "#call_and_catch" do
1018
setup do
1119
Support.Mnesia.stop
1220
:ok
@@ -16,15 +24,15 @@ defmodule Memento.Tests.Mnesia do
1624
@func :system_info
1725
@args [:is_running]
1826
test "delegates method calls to the mnesia module" do
19-
assert :no == Mnesia.call(@func, @args)
27+
assert :no == Mnesia.call_and_catch(@func, @args)
2028
end
2129

2230

2331
@func :schema
2432
@args []
2533
test "re-raises mnesia exits as memento exceptions" do
2634
assert_raise(MnesiaException, ~r/not running/i, fn ->
27-
Mnesia.call(@func, @args)
35+
Mnesia.call_and_catch(@func, @args)
2836
end)
2937
end
3038

@@ -33,7 +41,7 @@ defmodule Memento.Tests.Mnesia do
3341
@args [Tables.User, :all]
3442
test "prints descriptions of the error" do
3543
assert_raise(MnesiaException, ~r/tried to perform op on non-existing/i, fn ->
36-
Mnesia.call(@func, @args)
44+
Mnesia.call_and_catch(@func, @args)
3745
end)
3846
end
3947
end

test/support/support.ex

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ defmodule Memento.Support do
2929
end
3030

3131

32+
def transaction(fun) when is_function(fun) do
33+
Memento.Transaction.execute(fun)
34+
end
35+
3236
def transaction({module, method, args})
3337
when is_atom(module) and is_atom(method) do
3438
transaction(fn ->
@@ -43,10 +47,5 @@ defmodule Memento.Support do
4347
end)
4448
end
4549

46-
47-
def transaction(fun) when is_function(fun) do
48-
Memento.Transaction.execute(fun)
49-
end
50-
5150
end
5251
end

0 commit comments

Comments
 (0)