diff --git a/lib/memento/errors/error.ex b/lib/memento/errors/error.ex index cd3fe73..b041a1b 100644 --- a/lib/memento/errors/error.ex +++ b/lib/memento/errors/error.ex @@ -50,6 +50,7 @@ defmodule Memento.Error do defp do_normalize(reason) do case reason do + # Mnesia Error Codes :no_transaction -> %NoTransactionError{message: "Not inside a Memento Transaction"} @@ -59,9 +60,12 @@ defmodule Memento.Error do {:already_exists, resource} -> %AlreadyExistsError{message: "#{inspect(resource)} already exists"} + + # Custom Error Code - Not Part of Mnesia {:autoincrement, message} -> %InvalidOperationError{message: "Autoincrement #{message}"} + # Don't need custom errors for the rest, fallback to MnesiaException # and raise with Mnesia's description of the error error -> diff --git a/lib/memento/table/table.ex b/lib/memento/table/table.ex index 09e17ad..c317eb6 100644 --- a/lib/memento/table/table.ex +++ b/lib/memento/table/table.ex @@ -218,6 +218,18 @@ defmodule Memento.Table do + + @doc "Same as `delete/1`, but raises error on failure." + @spec delete!(name) :: :ok | no_return + def delete!(table) do + table + |> delete + |> handle_for_bang! + end + + + + @doc """ Returns all table information. diff --git a/test/memento/table/table_test.exs b/test/memento/table/table_test.exs index b799146..31acc3a 100644 --- a/test/memento/table/table_test.exs +++ b/test/memento/table/table_test.exs @@ -153,11 +153,31 @@ defmodule Memento.Tests.Table do test "deletes an mnesia table from memento definition" do assert {:atomic, :ok} = :mnesia.create_table(@table, []) - assert :ok = Memento.Table.delete(@table) + assert :ok = Memento.Table.delete!(@table) end test "returns :error if the table does not exists" do + assert_raise(Memento.DoesNotExistError, ~r/does not exist/i, fn -> + Memento.Table.delete!(@table) + end) + end + end + + + + + describe "#delete!" do + @table Tables.User + + + test "deletes an mnesia table from memento definition" do + assert {:atomic, :ok} = :mnesia.create_table(@table, []) + assert :ok = Memento.Table.delete!(@table) + end + + + test "raises errors if the table does not exists" do assert {:error, {:no_exists, _}} = Memento.Table.delete(@table) end end