-
Notifications
You must be signed in to change notification settings - Fork 41
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = | ||
|
@@ -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 | ||
|
@@ -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 | ||
opts = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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" | ||
|
There was a problem hiding this comment.
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).