Skip to content

Instantly share code, notes, and snippets.

@pareeohnos
Created January 18, 2017 13:19
Show Gist options
  • Save pareeohnos/2b23604b43ad8866da0ed463996dca13 to your computer and use it in GitHub Desktop.
Save pareeohnos/2b23604b43ad8866da0ed463996dca13 to your computer and use it in GitHub Desktop.
def handle_events(transformations, _from, state) do
Repo.transaction(fn ->
case process_db_transformations(transformations) do
{ :error, cs } ->
Repo.rollback(cs)
resp -> resp
end
end)
{ :noreply, [], state }
end
def process_db_transformations([]), do: :ok
def process_db_transformations([ transformation | transformations ]) do
perform_transformation(transformation)
process_db_transformations(transformations)
end
def perform_transformation(%DBTransformation{ action: :insert } = transformation) do
case Repo.insert(transformation.changeset) do
{ :error, changeset } = error ->
Logger.error "Unable to insert #{to_string(transformation.changeset.data.__struct__)} record: #{inspect(validation_errors(changeset))}"
error
{ :ok, record } = success ->
success
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment