From b4196f6bb716b7b46c4abd19574e4a22564cdf0d Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Mon, 27 Nov 2017 14:38:22 -0600 Subject: [PATCH 1/2] Add support for arbitrary Mnesia arguments The previous engine "type" parameters are checked to maintain backwards compatibility --- lib/ecto_mnesia/storage/migrator.ex | 39 ++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 9 deletions(-) mode change 100644 => 100755 lib/ecto_mnesia/storage/migrator.ex diff --git a/lib/ecto_mnesia/storage/migrator.ex b/lib/ecto_mnesia/storage/migrator.ex old mode 100644 new mode 100755 index bf85438..e8f67e6 --- a/lib/ecto_mnesia/storage/migrator.ex +++ b/lib/ecto_mnesia/storage/migrator.ex @@ -9,7 +9,7 @@ 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 = @@ -17,13 +17,13 @@ defmodule EctoMnesia.Storage.Migrator do |> 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 = + case engine do + nil -> + [{:type, :ordered_set}] + type when type in [:set, :ordered_set, :bag] -> + [{:type, type}] + opts when is_list(opts) -> + opts + end + config = conf(repo) - tab_def = [{:attributes, attributes}, {config[:storage_type], [config[:host]]}, {:type, get_engine(type)}] + + storage_opts = + cond do + opts[:disc_copies] != nil -> + [] + opts[:ram_copies] != nil -> + [] + opts[:disc_only_copies] != nil -> + [] + :else -> + [{config[:storage_type], [config[:host]]}] + end + + + 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" From 47b7edc4766f86d6cf112aed3d29b45e8c08413f Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Mon, 4 Dec 2017 18:12:03 +0000 Subject: [PATCH 2/2] Restore default to :ordered_set --- lib/ecto_mnesia/storage/migrator.ex | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/ecto_mnesia/storage/migrator.ex b/lib/ecto_mnesia/storage/migrator.ex index e8f67e6..be4784d 100755 --- a/lib/ecto_mnesia/storage/migrator.ex +++ b/lib/ecto_mnesia/storage/migrator.ex @@ -174,7 +174,11 @@ defmodule EctoMnesia.Storage.Migrator do type when type in [:set, :ordered_set, :bag] -> [{:type, type}] opts when is_list(opts) -> - opts + if opts[:type] == nil do + [type: :ordered_set] ++ opts + else + opts + end end config = conf(repo)