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

Amnesia.Table.transform #25

Open
ttyerlsol opened this issue Aug 26, 2014 · 6 comments
Open

Amnesia.Table.transform #25

ttyerlsol opened this issue Aug 26, 2014 · 6 comments

Comments

@ttyerlsol
Copy link

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.

@bradleyd
Copy link

@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

@jmerriweather
Copy link

jmerriweather commented Jul 7, 2016

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)

@akurkin
Copy link

akurkin commented Aug 3, 2016

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

@jmerriweather
Copy link

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
Copy link

jmerriweather commented Aug 12, 2016

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants