Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for arbitrary Mnesia arguments #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions lib/ecto_mnesia/storage/migrator.ex
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ defmodule EctoMnesia.Storage.Migrator do

@doc false
# Tables
def execute(repo, {:create, %Ecto.Migration.Table{name: table, engine: type}, instructions}, _opts) do
def execute(repo, {:create, %Ecto.Migration.Table{name: table, engine: engine}, instructions}, _opts) do
ensure_pk_table!(repo)

table_attrs =
instructions
|> Enum.reduce([], &reduce_fields(&1, &2, [], :skip))
|> Enum.uniq()

case do_create_table(repo, table, type, table_attrs) do
case do_create_table(repo, table, engine, table_attrs) do
:ok -> :ok
:already_exists -> raise "Table #{table} already exists"
end
end

def execute(repo, {:create_if_not_exists, %Ecto.Migration.Table{name: table, engine: type}, instructions}, _opts) do
def execute(repo, {:create_if_not_exists, %Ecto.Migration.Table{name: table, engine: engine}, instructions}, _opts) do
ensure_pk_table!(repo)

table_attrs =
Expand All @@ -40,7 +40,7 @@ defmodule EctoMnesia.Storage.Migrator do
|> Enum.reduce(table_attrs, &reduce_fields(&1, &2, [], :skip))
|> Enum.uniq()

case do_create_table(repo, table, type, new_table_attrs) do
case do_create_table(repo, table, engine, new_table_attrs) do
:ok -> :ok
:already_exists -> :ok
end
Expand Down Expand Up @@ -166,9 +166,33 @@ defmodule EctoMnesia.Storage.Migrator do
end

# Helpers
defp do_create_table(repo, table, type, attributes) do
defp do_create_table(repo, table, engine, attributes) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function is too complex (CC is 10, max is 9).

opts =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no trailing white-space at the end of a line.

case engine do
nil ->
[{:type, :ordered_set}]
type when type in [:set, :ordered_set, :bag] ->
[{:type, type}]
opts when is_list(opts) ->
opts
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no trailing white-space at the end of a line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no trailing white-space at the end of a line.

config = conf(repo)
tab_def = [{:attributes, attributes}, {config[:storage_type], [config[:host]]}, {:type, get_engine(type)}]

storage_opts =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no trailing white-space at the end of a line.

cond do
opts[:disc_copies] != nil ->
[]
opts[:ram_copies] != nil ->
[]
opts[:disc_only_copies] != nil ->
[]
:else ->
[{config[:storage_type], [config[:host]]}]
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no trailing white-space at the end of a line.


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no trailing white-space at the end of a line.

tab_def = [{:attributes, attributes}] ++ opts ++ storage_opts
case Mnesia.create_table(table, tab_def) do
{:atomic, :ok} ->
Mnesia.wait_for_tables([table], 1_000)
Expand All @@ -178,9 +202,6 @@ defmodule EctoMnesia.Storage.Migrator do
end
end

defp get_engine(nil), do: :ordered_set
defp get_engine(type) when is_atom(type), do: type

defp reduce_fields({:remove, field}, fields, table_fields, on_not_found) do
if on_not_found == :raise and !field_exists?(table_fields, field) do
raise "Field #{field} not found"
Expand Down