Amnesia.Table.transform
When I run Amnesia.Table.transform/3 in the iex shell the update works fine. However when I put it into a Mix.Tasks module it doesn't. Any hints on using Amnesia.Table.transform/3 in a Task ?
The same issue occurs when I put a :mnesia.transform_table in the run/1 function of the Task.
@ttyerlsol can you give an example of your function to transform the data? We are running into issues using the transform (with ignore) and our data is converted back to records and not the struct that Amnesia allows.
Thanks
I'd also like to know how to do this, is it as simple as the following?
Amnesia.Table.transform(Database.Table, [:id, :name, :new_attribute], fn(old) ->
%{old | new_attribute: "default value"}
end)
have you guys figured out how to do it properly? any hints will be highly appreciated
I believe i've worked this out, needs to be the following:
Amnesia.Table.transform(Database.Table, [:id, :name, :new_attribute], fn(old) ->
old |> Tuple.append(new_attribute_value)
end)
here are my exrm release tasks:
def transform_table(full_table_name, attribute_list, attribute_values_list) do
{:ok, _} = Application.ensure_all_started(:app)
Amnesia.Table.transform(full_table_name, attribute_list, fn(existing_values) ->
existing_values_as_list = existing_values
|> Tuple.to_list
existing_values_as_list ++ attribute_values_list
|> List.to_tuple
end)
IO.puts "Done"
:init.stop()
end
def transform_table_remove_attribute_at(full_table_name, full_attribute_list, delete_attribute_index) do
{:ok, _} = Application.ensure_all_started(:app)
Amnesia.Table.transform(full_table_name, full_attribute_list, fn(existing_values) ->
IO.puts("Before: #{inspect existing_values}")
# add an additional 1 to 'delete_attribute_index' as the table name is included in the 'existing_values' tuple
final_response = existing_values |> Tuple.delete_at(delete_attribute_index + 1)
IO.puts("Final: #{inspect final_response}")
final_response
end)
IO.puts "Done"
:init.stop()
end
@bradleyd I've noticed that if your data doesn't match your deftable it will not put it into a struct