2019-11-09 03:27:46 +00:00
defmodule Frenzy.Repo.Migrations.FeedsConvertPipelineStagesToPipelines do
use Ecto.Migration
alias Frenzy.Repo
import Ecto.Query
def up do
2019-11-09 03:33:24 +00:00
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
}
2019-11-09 03:27:46 +00:00
)
2019-11-09 03:33:24 +00:00
)
|> 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
2019-11-09 03:27:46 +00:00
2019-11-09 03:33:24 +00:00
stages =
pipeline_data
|> Enum . sort_by ( & &1 . index )
|> Enum . map ( fn data ->
%{
module_name : data . module_name ,
options : data . options
}
end )
2019-11-09 03:27:46 +00:00
2019-11-09 03:33:24 +00:00
{ 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 ] )
2019-11-09 03:27:46 +00:00
2019-11-09 03:33:24 +00:00
feed_query = from ( f in " feeds " , where : f . id == ^ feed_id )
Repo . update_all ( feed_query , set : [ pipeline_id : pipeline_id ] )
end )
2019-11-09 03:27:46 +00:00
end
def down do
# we don't have a down migration for this, since we're not modifying any schemas, we're just converting the existing data from the pipeline_stages table to the new pipelines table and updating the corresponding feeds
end
end