11defmodule CodeCorps.Repo.Migrations.CreateTaskList do
22 use Ecto.Migration
3- import Ecto.Changeset
4- import Ecto.Query
5-
6- alias CodeCorps.Project
7- alias CodeCorps.Repo
8- alias CodeCorps.Task
9- alias CodeCorps.TaskList
103
114 def change do
125 create table ( :task_lists ) do
@@ -23,44 +16,5 @@ defmodule CodeCorps.Repo.Migrations.CreateTaskList do
2316 add :task_list_id , references ( :task_lists , on_delete: :nothing )
2417 add :order , :integer
2518 end
26-
27- flush
28-
29- Application . ensure_all_started :timex
30- migrate_existing ( )
31- end
32-
33- def migrate_existing ( ) do
34- Project
35- |> preload ( :task_lists )
36- |> Repo . all ( )
37- |> Enum . each ( & handle_project_migration / 1 )
38- end
39-
40- defp handle_project_migration ( project ) do
41- cond do
42- project . task_lists != [ ] ->
43- IO . puts "Task lists already exist for #{ project . title } , skipping migration."
44- true ->
45- IO . puts "Generating default task lists for #{ project . title } ."
46-
47- { :ok , project } = Project . changeset ( project , % { } )
48- |> put_assoc ( :task_lists , TaskList . default_task_lists ( ) )
49- |> Repo . update
50-
51- add_existing_tasks_to_inbox ( project , hd ( project . task_lists ) )
52- end
53- end
54-
55- defp add_existing_tasks_to_inbox ( project , task_list ) do
56- Task
57- |> CodeCorps.Helpers.Query . project_filter ( % { project_id: project . id } )
58- |> Repo . all ( )
59- |> Enum . each ( & assign_task_to_inbox ( & 1 , task_list ) )
60- end
61-
62- defp assign_task_to_inbox ( task , task_list ) do
63- Task . changeset ( task , % { task_list_id: task_list . id } )
64- |> Repo . update ( )
6519 end
6620end
0 commit comments