diff --git a/priv/repo/migrations/20191108033910_feeds_convert_pipeline_stages_to_pipelines.exs b/priv/repo/migrations/20191108033910_feeds_convert_pipeline_stages_to_pipelines.exs index a9fdb78..a8ce71d 100644 --- a/priv/repo/migrations/20191108033910_feeds_convert_pipeline_stages_to_pipelines.exs +++ b/priv/repo/migrations/20191108033910_feeds_convert_pipeline_stages_to_pipelines.exs @@ -4,58 +4,56 @@ defmodule Frenzy.Repo.Migrations.FeedsConvertPipelineStagesToPipelines do import Ecto.Query def up do - pipeline_entries = - Repo.all( - from(stage in "pipeline_stages", - join: feed in "feeds", - on: stage.feed_id == feed.id, - join: group in "groups", - on: feed.group_id == group.id, - select: %{ - index: stage.index, - module_name: stage.module_name, - options: stage.options, - feed_id: feed.id, - feed_title: feed.title, - user_id: group.user_id - } - ) + Repo.all( + from(stage in "pipeline_stages", + join: feed in "feeds", + on: stage.feed_id == feed.id, + join: group in "groups", + on: feed.group_id == group.id, + select: %{ + index: stage.index, + module_name: stage.module_name, + options: stage.options, + feed_id: feed.id, + feed_title: feed.title, + user_id: group.user_id + } ) - |> Enum.group_by(fn data -> data.feed_id end) - |> Enum.map(fn {feed_id, [first | _] = pipeline_data} -> - feed_title = first.feed_title - user_id = first.user_id + ) + |> Enum.group_by(fn data -> data.feed_id end) + |> Enum.map(fn {feed_id, [first | _] = pipeline_data} -> + feed_title = first.feed_title + user_id = first.user_id - stages = - pipeline_data - |> Enum.sort_by(& &1.index) - |> Enum.map(fn data -> - %{ - module_name: data.module_name, - options: data.options - } - end) + stages = + pipeline_data + |> Enum.sort_by(& &1.index) + |> Enum.map(fn data -> + %{ + module_name: data.module_name, + options: data.options + } + end) - {feed_id, - [ - name: - if(feed_title, - do: "#{feed_title} Pipeline", - else: "Unknown Feed Pipeline (feed ID #{feed_id})" - ), - stages: stages, - user_id: user_id, - inserted_at: NaiveDateTime.utc_now(), - updated_at: NaiveDateTime.utc_now() - ]} - end) - |> Enum.each(fn {feed_id, pipeline_entry} -> - {_, [%{id: pipeline_id}]} = - Repo.insert_all("pipelines", [pipeline_entry], returning: [:id]) + {feed_id, + [ + name: + if(feed_title, + do: "#{feed_title} Pipeline", + else: "Unknown Feed Pipeline (feed ID #{feed_id})" + ), + stages: stages, + user_id: user_id, + inserted_at: NaiveDateTime.utc_now(), + updated_at: NaiveDateTime.utc_now() + ]} + end) + |> Enum.each(fn {feed_id, pipeline_entry} -> + {_, [%{id: pipeline_id}]} = Repo.insert_all("pipelines", [pipeline_entry], returning: [:id]) - feed_query = from(f in "feeds", where: f.id == ^feed_id) - Repo.update_all(feed_query, set: [pipeline_id: pipeline_id]) - end) + feed_query = from(f in "feeds", where: f.id == ^feed_id) + Repo.update_all(feed_query, set: [pipeline_id: pipeline_id]) + end) end def down do