amnesia icon indicating copy to clipboard operation
amnesia copied to clipboard

Amnesia.Table.transform

Open ttyerlsol opened this issue 11 years ago • 6 comments

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 avatar Aug 26 '14 21:08 ttyerlsol

@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

bradleyd avatar Feb 16 '16 21:02 bradleyd

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)

jmerriweather avatar Jul 07 '16 00:07 jmerriweather

have you guys figured out how to do it properly? any hints will be highly appreciated

akurkin avatar Aug 03 '16 07:08 akurkin

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)

jmerriweather avatar Aug 12 '16 01:08 jmerriweather

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

jmerriweather avatar Aug 12 '16 03:08 jmerriweather

@bradleyd I've noticed that if your data doesn't match your deftable it will not put it into a struct

jmerriweather avatar Aug 12 '16 03:08 jmerriweather