Created
January 18, 2017 13:19
-
-
Save pareeohnos/2b23604b43ad8866da0ed463996dca13 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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