-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
56 additions
and
56 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
defmodule CodeCorps.Repo.Seeds.AddDefaultTasksToProjects do | ||
import Ecto.Changeset | ||
import Ecto.Query | ||
|
||
alias CodeCorps.Project | ||
alias CodeCorps.Repo | ||
alias CodeCorps.Task | ||
alias CodeCorps.TaskList | ||
|
||
def migrate_existing() do | ||
Project | ||
|> preload(:task_lists) | ||
|> Repo.all() | ||
|> Enum.each(&handle_project_migration/1) | ||
end | ||
|
||
defp handle_project_migration(project) do | ||
cond do | ||
project.task_lists != [] -> | ||
IO.puts "Task lists already exist for #{project.title}, skipping migration." | ||
true -> | ||
IO.puts "Generating default task lists for #{project.title}." | ||
|
||
{:ok, project} = Project.changeset(project, %{}) | ||
|> put_assoc(:task_lists, TaskList.default_task_lists()) | ||
|> Repo.update | ||
|
||
add_existing_tasks_to_inbox(project, hd(project.task_lists)) | ||
end | ||
end | ||
|
||
defp add_existing_tasks_to_inbox(project, task_list) do | ||
Task | ||
|> CodeCorps.Helpers.Query.project_filter(%{ project_id: project.id }) | ||
|> Repo.all() | ||
|> Enum.each(&assign_task_to_inbox(&1, task_list)) | ||
end | ||
|
||
defp assign_task_to_inbox(task, task_list) do | ||
Task.changeset(task, %{ task_list_id: task_list.id }) | ||
|> Repo.update() | ||
end | ||
end | ||
|
||
CodeCorps.Repo.Seeds.AddDefaultTasksToProjects.migrate_existing() |
This file contains 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
This file contains 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
This file contains 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
This file contains 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